{"id":1487,"date":"2025-02-09T10:04:09","date_gmt":"2025-02-09T02:04:09","guid":{"rendered":"https:\/\/www.laixuexila.com\/?p=1487"},"modified":"2025-02-09T10:04:10","modified_gmt":"2025-02-09T02:04:10","slug":"python-%e5%b8%b8%e7%94%a8%e5%ba%93%e4%b8%8e%e5%ba%94%e7%94%a8%ef%bc%9anumpy%e3%80%81pandas%e3%80%81matplotlib%e3%80%81flask-%e5%ae%9e%e6%88%98%e6%95%99%e7%a8%8b","status":"publish","type":"post","link":"https:\/\/www.laixuexila.com\/index.php\/2025\/02\/09\/python-%e5%b8%b8%e7%94%a8%e5%ba%93%e4%b8%8e%e5%ba%94%e7%94%a8%ef%bc%9anumpy%e3%80%81pandas%e3%80%81matplotlib%e3%80%81flask-%e5%ae%9e%e6%88%98%e6%95%99%e7%a8%8b\/","title":{"rendered":"Python \u5e38\u7528\u5e93\u4e0e\u5e94\u7528\uff1aNumPy\u3001Pandas\u3001Matplotlib\u3001Flask \u5b9e\u6218\u6559\u7a0b"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">\u672c\u6559\u7a0b\u5168\u9762\u4ecb\u7ecd Python \u5e38\u7528\u5e93\u4e0e\u5e94\u7528\uff0c\u5305\u62ec NumPy \u6570\u7ec4\u64cd\u4f5c\u3001Pandas \u6570\u636e\u6846\u64cd\u4f5c\u4e0e\u6570\u636e\u6e05\u6d17\u3001Matplotlib \u548c Seaborn \u6570\u636e\u53ef\u89c6\u5316\u6280\u5de7\u3001Flask Web \u5f00\u53d1\u57fa\u7840\uff0c\u4ee5\u53ca scikit-learn \u673a\u5668\u5b66\u4e60\u5165\u95e8\u3002\u901a\u8fc7\u5b9e\u4f8b\u5b66\u4e60\uff0c\u638c\u63e1\u5982\u4f55\u9ad8\u6548\u5904\u7406\u6570\u636e\u3001\u7ed8\u5236\u7cbe\u7f8e\u56fe\u8868\u3001\u5f00\u53d1 Web \u5e94\u7528\uff0c\u5e76\u5e94\u7528\u673a\u5668\u5b66\u4e60\u6280\u672f\u8fdb\u884c\u6570\u636e\u5206\u6790\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u8be6\u7ec6\u6559\u7a0b\u5185\u5bb9\uff1aPython \u5e38\u7528\u5e93\u4e0e\u5e94\u7528<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">1. <strong>NumPy \u4e0e Pandas<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u7ec4\u64cd\u4f5c\uff1aNumPy \u6570\u7ec4\u4e0e\u5411\u91cf\u5316\u64cd\u4f5c<\/strong><br>NumPy \u662f Python \u4e2d\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u6838\u5fc3\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u7ec4\u5904\u7406\u529f\u80fd\u3002\u5411\u91cf\u5316\u64cd\u4f5c\u5141\u8bb8\u60a8\u5bf9\u6574\u4e2a\u6570\u7ec4\u8fdb\u884c\u6279\u91cf\u5904\u7406\uff0c\u63d0\u9ad8\u8fd0\u7b97\u6548\u7387\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import numpy as np\n\n  # \u521b\u5efa NumPy \u6570\u7ec4\n  arr = np.array(&#91;1, 2, 3, 4, 5])\n\n  # \u5411\u91cf\u5316\u64cd\u4f5c\uff1a\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u52a0\u500d\n  arr_doubled = arr * 2\n  print(arr_doubled)  # \u8f93\u51fa: &#91;2 4 6 8 10]<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u5904\u7406\uff1aPandas \u6570\u636e\u6846\u4e0e\u6570\u636e\u64cd\u4f5c<\/strong><br>Pandas \u662f\u7528\u4e8e\u6570\u636e\u5206\u6790\u548c\u64cd\u4f5c\u7684\u9ad8\u6548\u5de5\u5177\uff0c\u5c24\u5176\u9002\u7528\u4e8e\u8868\u683c\u6570\u636e\uff08\u5982 CSV \u6587\u4ef6\uff09\u3002\u60a8\u53ef\u4ee5\u4f7f\u7528 Pandas \u6765\u8bfb\u53d6\u3001\u6e05\u6d17\u548c\u5206\u6790\u6570\u636e\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  import pandas as pd\n\n  # \u4ece CSV \u6587\u4ef6\u8bfb\u53d6\u6570\u636e\n  df = pd.read_csv('data.csv')\n\n  # \u663e\u793a\u6570\u636e\u524d\u4e94\u884c\n  print(df.head())\n\n  # \u6570\u636e\u6e05\u6d17\uff1a\u53bb\u6389\u7f3a\u5931\u503c\n  df_cleaned = df.dropna()\n\n  # \u6570\u636e\u9009\u62e9\uff1a\u9009\u62e9\u7279\u5b9a\u5217\n  df_selected = df&#91;&#91;'col1', 'col2']]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">2. <strong>Matplotlib \u4e0e Seaborn<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u53ef\u89c6\u5316\uff1a\u56fe\u8868\u521b\u5efa\u4e0e\u6837\u5f0f\u5b9a\u5236<\/strong><br>Matplotlib \u662f Python \u4e2d\u6700\u5e38\u7528\u7684\u6570\u636e\u53ef\u89c6\u5316\u5e93\uff0c\u53ef\u4ee5\u521b\u5efa\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u8868\uff08\u5982\u6298\u7ebf\u56fe\u3001\u67f1\u72b6\u56fe\u3001\u997c\u56fe\u7b49\uff09\u3002Seaborn \u5728 Matplotlib \u57fa\u7840\u4e0a\u8fdb\u884c\u4e86\u5c01\u88c5\uff0c\u4f7f\u5f97\u56fe\u8868\u66f4\u52a0\u7f8e\u89c2\u3002<\/li>\n\n\n\n<li><strong>Matplotlib \u57fa\u672c\u56fe\u5f62\u7ed8\u5236<\/strong><br>\u793a\u4f8b\uff1a <code>import matplotlib.pyplot as plt # \u521b\u5efa\u7b80\u5355\u7684\u6298\u7ebf\u56fe x = [1, 2, 3, 4, 5] y = [2, 3, 5, 7, 11] plt.plot(x, y) plt.title(\"\u7b80\u5355\u6298\u7ebf\u56fe\") plt.xlabel(\"X\u8f74\") plt.ylabel(\"Y\u8f74\") plt.show()<\/code><\/li>\n\n\n\n<li><strong>Seaborn \u9ad8\u7ea7\u6570\u636e\u53ef\u89c6\u5316<\/strong><br>Seaborn \u63d0\u4f9b\u4e86\u66f4\u591a\u7f8e\u89c2\u7684\u56fe\u8868\u548c\u65b9\u4fbf\u7684\u6570\u636e\u53ef\u89c6\u5316\u529f\u80fd\uff0c\u652f\u6301\u7ed8\u5236\u70ed\u56fe\u3001\u7bb1\u578b\u56fe\u3001\u5206\u5e03\u56fe\u7b49\u3002 \u793a\u4f8b\uff1a <code>import seaborn as sns import pandas as pd # \u521b\u5efa\u4e00\u4e2a\u793a\u4f8b\u6570\u636e\u6846 df = pd.DataFrame({ 'x': [1, 2, 3, 4, 5], 'y': [2, 3, 5, 7, 11], 'category': ['A', 'B', 'A', 'B', 'A'] }) # \u7ed8\u5236\u5e26\u6709\u7c7b\u522b\u7684\u6563\u70b9\u56fe sns.scatterplot(data=df, x='x', y='y', hue='category') plt.title(\"Seaborn \u6563\u70b9\u56fe\") plt.show()<\/code><\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">3. <strong>Web \u5f00\u53d1<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Flask \u5165\u95e8\uff1a\u521b\u5efa\u7b80\u5355\u7684 Web \u5e94\u7528<\/strong><br>Flask \u662f\u4e00\u4e2a\u8f7b\u91cf\u7ea7\u7684 Python Web \u6846\u67b6\uff0c\u9002\u7528\u4e8e\u5feb\u901f\u5f00\u53d1\u5c0f\u578b Web \u5e94\u7528\u3002\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa Web \u8def\u7531\u3001\u5904\u7406\u8bf7\u6c42\u548c\u54cd\u5e94\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  from flask import Flask\n\n  app = Flask(__name__)\n\n  @app.route('\/')\n  def home():\n      return \"Hello, Flask!\"\n\n  if __name__ == \"__main__\":\n      app.run(debug=True)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u4ee5\u4e0a\u4ee3\u7801\u521b\u5efa\u4e86\u4e00\u4e2a\u7b80\u5355\u7684 Web \u5e94\u7528\uff0c\u5728\u6839\u8def\u7531\u8bbf\u95ee\u65f6\u8fd4\u56de &#8220;Hello, Flask!&#8221;\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Django \u7b80\u4ecb\uff1a\u521b\u5efa\u52a8\u6001 Web \u5e94\u7528<\/strong><br>Django \u662f\u4e00\u4e2a\u529f\u80fd\u66f4\u5f3a\u5927\u7684 Python Web \u6846\u67b6\uff0c\u9002\u7528\u4e8e\u5f00\u53d1\u590d\u6742\u7684 Web \u5e94\u7528\u3002\u5b83\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5e93\u652f\u6301\u3001\u6a21\u677f\u5f15\u64ce\u548c\u540e\u53f0\u7ba1\u7406\u754c\u9762\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  # \u521b\u5efa\u4e00\u4e2a Django \u9879\u76ee\n  django-admin startproject mysite\n\n  # \u8fdb\u5165\u9879\u76ee\u76ee\u5f55\u5e76\u521b\u5efa\u5e94\u7528\n  python manage.py startapp myapp<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">4. <strong>\u673a\u5668\u5b66\u4e60\u57fa\u7840<\/strong><\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528 scikit-learn \u5e93\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3<\/strong><br>scikit-learn \u662f\u4e00\u4e2a\u6d41\u884c\u7684\u673a\u5668\u5b66\u4e60\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u7b97\u6cd5\u548c\u5de5\u5177\uff0c\u9002\u5408\u8fdb\u884c\u6570\u636e\u9884\u5904\u7406\u3001\u7279\u5f81\u9009\u62e9\u548c\u6a21\u578b\u8bad\u7ec3\u3002 \u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  from sklearn.datasets import load_iris\n  from sklearn.model_selection import train_test_split\n  from sklearn.linear_model import LogisticRegression\n\n  # \u52a0\u8f7d Iris \u6570\u636e\u96c6\n  data = load_iris()\n  X = data.data\n  y = data.target\n\n  # \u5207\u5206\u6570\u636e\u96c6\n  X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)\n\n  # \u521b\u5efa\u5e76\u8bad\u7ec3\u6a21\u578b\n  model = LogisticRegression(max_iter=200)\n  model.fit(X_train, y_train)\n\n  # \u8bc4\u4f30\u6a21\u578b\n  print(\"\u51c6\u786e\u7387\uff1a\", model.score(X_test, y_test))<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6570\u636e\u9884\u5904\u7406\u4e0e\u7279\u5f81\u9009\u62e9<\/strong><br>\u673a\u5668\u5b66\u4e60\u4e2d\uff0c\u6570\u636e\u9884\u5904\u7406\u548c\u7279\u5f81\u9009\u62e9\u662f\u975e\u5e38\u91cd\u8981\u7684\u6b65\u9aa4\uff0c\u53ef\u4ee5\u663e\u8457\u63d0\u9ad8\u6a21\u578b\u7684\u6548\u679c\u3002 \u793a\u4f8b\uff1a\u4f7f\u7528 <code>StandardScaler<\/code> \u5bf9\u6570\u636e\u8fdb\u884c\u6807\u51c6\u5316\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  from sklearn.preprocessing import StandardScaler\n\n  # \u6570\u636e\u6807\u51c6\u5316\n  scaler = StandardScaler()\n  X_scaled = scaler.fit_transform(X_train)<\/code><\/pre>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u7b80\u5355\u7684\u76d1\u7763\u5b66\u4e60\u4e0e\u975e\u76d1\u7763\u5b66\u4e60<\/strong><\/li>\n\n\n\n<li><strong>\u76d1\u7763\u5b66\u4e60<\/strong>\uff1a\u4f7f\u7528\u5e26\u6807\u7b7e\u7684\u6570\u636e\u8fdb\u884c\u8bad\u7ec3\uff0c\u4f8b\u5982\u5206\u7c7b\u548c\u56de\u5f52\u95ee\u9898\u3002<\/li>\n\n\n\n<li><strong>\u975e\u76d1\u7763\u5b66\u4e60<\/strong>\uff1a\u4f7f\u7528\u65e0\u6807\u7b7e\u6570\u636e\u8fdb\u884c\u8bad\u7ec3\uff0c\u4f8b\u5982\u805a\u7c7b\u3002 \u793a\u4f8b\uff1a\u4f7f\u7528 <code>KMeans<\/code> \u8fdb\u884c\u805a\u7c7b\uff08\u975e\u76d1\u7763\u5b66\u4e60\uff09\u3002<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>  from sklearn.cluster import KMeans\n\n  # \u4f7f\u7528 KMeans \u805a\u7c7b\u7b97\u6cd5\n  kmeans = KMeans(n_clusters=3)\n  kmeans.fit(X_train)\n  print(\"\u805a\u7c7b\u4e2d\u5fc3\uff1a\", kmeans.cluster_centers_)<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">\u603b\u7ed3<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">\u901a\u8fc7\u672c\u6559\u7a0b\uff0c\u60a8\u5c06\u638c\u63e1 Python \u5e38\u7528\u7684\u51e0\u5927\u5e93\u53ca\u5176\u5e94\u7528\uff0c\u5305\u62ec\u5982\u4f55\u4f7f\u7528 NumPy \u548c Pandas \u8fdb\u884c\u6570\u636e\u5904\u7406\u4e0e\u5206\u6790\uff0c\u5982\u4f55\u4f7f\u7528 Matplotlib \u548c Seaborn \u521b\u5efa\u6f02\u4eae\u7684\u6570\u636e\u53ef\u89c6\u5316\u56fe\u8868\uff0c\u5982\u4f55\u4f7f\u7528 Flask \u5f00\u53d1 Web \u5e94\u7528\uff0c\u53ca\u5982\u4f55\u4f7f\u7528 scikit-learn \u8fdb\u884c\u673a\u5668\u5b66\u4e60\u6a21\u578b\u8bad\u7ec3\u4e0e\u6570\u636e\u9884\u5904\u7406\u3002\u901a\u8fc7\u5b9e\u4f8b\u8bb2\u89e3\uff0c\u60a8\u4e0d\u4ec5\u80fd\u5feb\u901f\u4e0a\u624b\u8fd9\u4e9b\u5e93\uff0c\u8fd8\u80fd\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u5f97\u5fc3\u5e94\u624b\u5730\u5e94\u7528\u5b83\u4eec\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6559\u7a0b\u5168\u9762\u4ecb\u7ecd Python \u5e38\u7528\u5e93\u4e0e\u5e94\u7528\uff0c\u5305\u62ec NumPy \u6570\u7ec4\u64cd\u4f5c\u3001Pandas \u6570\u636e\u6846\u64cd\u4f5c\u4e0e\u6570\u636e\u6e05\u6d17\u3001M [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[54],"tags":[],"class_list":["post-1487","post","type-post","status-publish","format-standard","hentry","category-python"],"_links":{"self":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1487","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/comments?post=1487"}],"version-history":[{"count":1,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1487\/revisions"}],"predecessor-version":[{"id":1488,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/posts\/1487\/revisions\/1488"}],"wp:attachment":[{"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/media?parent=1487"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/categories?post=1487"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.laixuexila.com\/index.php\/wp-json\/wp\/v2\/tags?post=1487"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}