{"id":1019775,"date":"2024-12-27T13:04:22","date_gmt":"2024-12-27T05:04:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1019775.html"},"modified":"2024-12-27T13:04:24","modified_gmt":"2024-12-27T05:04:24","slug":"python%e5%a6%82%e4%bd%95%e4%bd%bf%e7%94%a8%e6%95%b0%e5%ad%a6%e5%ba%93","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1019775.html","title":{"rendered":"python\u5982\u4f55\u4f7f\u7528\u6570\u5b66\u5e93"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25162202\/3b8ba47c-8917-48b3-a940-8c85c4874a5b.webp\" alt=\"python\u5982\u4f55\u4f7f\u7528\u6570\u5b66\u5e93\" \/><\/p>\n<p><p> \u5728Python\u4e2d\u4f7f\u7528\u6570\u5b66\u5e93\u65f6\uff0c<strong>\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165Python\u7684\u5185\u7f6e\u6a21\u5757math\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4ee5\u53ca\u4f7f\u7528SymPy\u5e93\u6765\u5b9e\u73b0\u5404\u79cd\u6570\u5b66\u8ba1\u7b97<\/strong>\u3002<strong>math\u6a21\u5757\u63d0\u4f9b\u4e86\u57fa\u672c\u7684\u6570\u5b66\u51fd\u6570\u548c\u5e38\u91cf<\/strong>\uff0c\u5982\u5e73\u65b9\u6839\u3001\u5bf9\u6570\u3001\u6b63\u5f26\u548c\u4f59\u5f26\uff1b<strong>NumPy\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u5e93\uff0c\u7528\u4e8e\u5904\u7406\u6570\u7ec4\u548c\u77e9\u9635\u8fd0\u7b97<\/strong>\uff0c\u5e76\u4e14\u5177\u6709\u8bb8\u591a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u9ad8\u7ea7\u529f\u80fd\uff1b<strong>SymPy\u5219\u7528\u4e8e\u7b26\u53f7\u6570\u5b66\u8ba1\u7b97<\/strong>\uff0c\u5982\u5fae\u79ef\u5206\u548c\u4ee3\u6570\u8868\u8fbe\u5f0f\u7684\u89e3\u6790\u3002\u4e0b\u9762\u6211\u4eec\u5c06\u8be6\u7ec6\u8ba8\u8bba\u5982\u4f55\u4f7f\u7528\u8fd9\u4e9b\u5e93\u6765\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><p>\u4e00\u3001MATH\u6a21\u5757<\/p>\n<\/p>\n<p><p>math\u6a21\u5757\u662fPython\u7684\u6807\u51c6\u5e93\u4e4b\u4e00\uff0c\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u57fa\u672c\u7684\u6570\u5b66\u51fd\u6570\u548c\u5e38\u91cf\u3002\u5b83\u4e3b\u8981\u7528\u4e8e\u5b9e\u73b0\u4e00\u4e9b\u57fa\u7840\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u5bf9\u4e8e\u7b80\u5355\u7684\u6570\u5b66\u4efb\u52a1\uff0cmath\u6a21\u5757\u662f\u4e00\u4e2a\u65b9\u4fbf\u7684\u9009\u62e9\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5e38\u7528\u51fd\u6570<\/li>\n<\/ol>\n<p><p>math\u6a21\u5757\u4e2d\u5305\u542b\u4e86\u4e00\u4e9b\u5e38\u7528\u7684\u6570\u5b66\u51fd\u6570\u3002\u6bd4\u5982\uff0c<code>math.sqrt(x)<\/code>\u7528\u4e8e\u8ba1\u7b97x\u7684\u5e73\u65b9\u6839\uff1b<code>math.log(x, base)<\/code>\u7528\u4e8e\u8ba1\u7b97\u4ee5base\u4e3a\u5e95x\u7684\u5bf9\u6570\uff0c\u5982\u679c\u4e0d\u6307\u5b9abase\uff0c\u5219\u9ed8\u8ba4\u4e3a\u81ea\u7136\u5bf9\u6570\uff1b<code>math.sin(x)<\/code>\u3001<code>math.cos(x)<\/code>\u548c<code>math.tan(x)<\/code>\u7528\u4e8e\u8ba1\u7b97\u6b63\u5f26\u3001\u4f59\u5f26\u548c\u6b63\u5207\u7b49\u4e09\u89d2\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import math<\/p>\n<h2><strong>\u8ba1\u7b97\u5e73\u65b9\u6839<\/strong><\/h2>\n<p>sqrt_value = math.sqrt(16)<\/p>\n<h2><strong>\u8ba1\u7b97\u81ea\u7136\u5bf9\u6570<\/strong><\/h2>\n<p>log_value = math.log(10)<\/p>\n<h2><strong>\u8ba1\u7b97\u6b63\u5f26<\/strong><\/h2>\n<p>sin_value = math.sin(math.pi \/ 2)<\/p>\n<p>print(f&quot;Square root: {sqrt_value}, Log: {log_value}, Sine: {sin_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5e38\u91cf<\/li>\n<\/ol>\n<p><p>math\u6a21\u5757\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u6570\u5b66\u5e38\u91cf\uff0c\u5982<code>math.pi<\/code>\u8868\u793a\u5706\u5468\u7387\u03c0\uff0c<code>math.e<\/code>\u8868\u793a\u81ea\u7136\u5bf9\u6570\u7684\u5e95e\u3002\u901a\u8fc7\u4f7f\u7528\u8fd9\u4e9b\u5e38\u91cf\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8fdb\u884c\u5404\u79cd\u6570\u5b66\u8ba1\u7b97\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528\u5e38\u91cfpi\u8ba1\u7b97\u5706\u7684\u9762\u79ef<\/p>\n<p>radius = 5<\/p>\n<p>area = math.pi * radius2<\/p>\n<p>print(f&quot;Area of circle: {area}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001NUMPY\u5e93<\/p>\n<\/p>\n<p><p>NumPy\u662f\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5f00\u6e90\u5e93\uff0c\u63d0\u4f9b\u4e86\u652f\u6301\u5927\u578b\u591a\u7ef4\u6570\u7ec4\u548c\u77e9\u9635\u7684\u8fd0\u7b97\u3002\u5b83\u8fd8\u5305\u542b\u4e86\u5927\u91cf\u7684\u6570\u5b66\u51fd\u6570\uff0c\u7528\u4e8e\u5bf9\u6570\u7ec4\u8fdb\u884c\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6570\u7ec4\u64cd\u4f5c<\/li>\n<\/ol>\n<p><p>NumPy\u6700\u5f3a\u5927\u7684\u7279\u6027\u4e4b\u4e00\u662f\u5176\u5bf9\u6570\u7ec4\u7684\u9ad8\u6548\u64cd\u4f5c\u3002\u53ef\u4ee5\u8f7b\u677e\u5730\u521b\u5efa\u6570\u7ec4\uff0c\u5e76\u6267\u884c\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\u3002NumPy\u7684\u6570\u7ec4\u6bd4Python\u7684\u5217\u8868\u66f4\u52a0\u9ad8\u6548\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u5927\u89c4\u6a21\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_1d = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u521b\u5efa\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>array_2d = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u6570\u7ec4\u52a0\u6cd5<\/strong><\/h2>\n<p>sum_array = array_1d + array_1d<\/p>\n<p>print(f&quot;Sum of arrays: {sum_array}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u6570\u5b66\u51fd\u6570<\/li>\n<\/ol>\n<p><p>NumPy\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7528\u4e8e\u6570\u7ec4\u64cd\u4f5c\u7684\u6570\u5b66\u51fd\u6570\u3002\u6bd4\u5982\uff0c<code>np.sum()<\/code>\u7528\u4e8e\u8ba1\u7b97\u6570\u7ec4\u7684\u5143\u7d20\u548c\uff0c<code>np.mean()<\/code>\u7528\u4e8e\u8ba1\u7b97\u6570\u7ec4\u7684\u5e73\u5747\u503c\uff0c<code>np.std()<\/code>\u7528\u4e8e\u8ba1\u7b97\u6570\u7ec4\u7684\u6807\u51c6\u5dee\uff0c\u7b49\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8ba1\u7b97\u6570\u7ec4\u7684\u5143\u7d20\u548c<\/p>\n<p>sum_value = np.sum(array_1d)<\/p>\n<h2><strong>\u8ba1\u7b97\u6570\u7ec4\u7684\u5e73\u5747\u503c<\/strong><\/h2>\n<p>mean_value = np.mean(array_1d)<\/p>\n<h2><strong>\u8ba1\u7b97\u6570\u7ec4\u7684\u6807\u51c6\u5dee<\/strong><\/h2>\n<p>std_value = np.std(array_1d)<\/p>\n<p>print(f&quot;Sum: {sum_value}, Mean: {mean_value}, Standard Deviation: {std_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001SYMPY\u5e93<\/p>\n<\/p>\n<p><p>SymPy\u662f\u4e00\u4e2a\u7528\u4e8e\u7b26\u53f7\u6570\u5b66\u8ba1\u7b97\u7684Python\u5e93\uff0c\u9002\u5408\u9700\u8981\u5904\u7406\u7b26\u53f7\u8868\u8fbe\u5f0f\u548c\u8fdb\u884c\u7b26\u53f7\u8ba1\u7b97\u7684\u573a\u5408\u3002\u5b83\u80fd\u591f\u6267\u884c\u4ee3\u6570\u8fd0\u7b97\u3001\u5fae\u79ef\u5206\u3001\u65b9\u7a0b\u6c42\u89e3\u7b49\u4efb\u52a1\u3002<\/p>\n<\/p>\n<ol>\n<li>\u7b26\u53f7\u8fd0\u7b97<\/li>\n<\/ol>\n<p><p>SymPy\u7684\u4e00\u4e2a\u91cd\u8981\u7279\u6027\u662f\u7b26\u53f7\u8fd0\u7b97\u3002\u901a\u8fc7\u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf\uff0c\u53ef\u4ee5\u5bf9\u4ee3\u6570\u8868\u8fbe\u5f0f\u8fdb\u884c\u5404\u79cd\u8fd0\u7b97\uff0c\u6bd4\u5982\u7b80\u5316\u3001\u5c55\u5f00\u3001\u56e0\u5f0f\u5206\u89e3\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import symbols, expand, simplify<\/p>\n<h2><strong>\u5b9a\u4e49\u7b26\u53f7\u53d8\u91cf<\/strong><\/h2>\n<p>x, y = symbols(&#39;x y&#39;)<\/p>\n<h2><strong>\u5c55\u5f00\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>expanded_expr = expand((x + y)2)<\/p>\n<h2><strong>\u7b80\u5316\u8868\u8fbe\u5f0f<\/strong><\/h2>\n<p>simplified_expr = simplify(x&lt;strong&gt;2 + 2*x*y + y&lt;\/strong&gt;2)<\/p>\n<p>print(f&quot;Expanded: {expanded_expr}, Simplified: {simplified_expr}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u5fae\u79ef\u5206\u548c\u65b9\u7a0b\u6c42\u89e3<\/li>\n<\/ol>\n<p><p>SymPy\u8fd8\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u5fae\u79ef\u5206\u8fd0\u7b97\u548c\u6c42\u89e3\u65b9\u7a0b\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u8ba1\u7b97\u51fd\u6570\u7684\u5bfc\u6570\u3001\u79ef\u5206\uff0c\u4ee5\u53ca\u6c42\u89e3\u4ee3\u6570\u65b9\u7a0b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sympy import diff, integrate, solve<\/p>\n<h2><strong>\u8ba1\u7b97\u5bfc\u6570<\/strong><\/h2>\n<p>derivative = diff(x2 + x, x)<\/p>\n<h2><strong>\u8ba1\u7b97\u4e0d\u5b9a\u79ef\u5206<\/strong><\/h2>\n<p>integral = integrate(x2 + x, x)<\/p>\n<h2><strong>\u89e3\u65b9\u7a0b<\/strong><\/h2>\n<p>solution = solve(x2 - 4, x)<\/p>\n<p>print(f&quot;Derivative: {derivative}, Integral: {integral}, Solution: {solution}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528SCIPY\u8fdb\u884c\u9ad8\u7ea7\u6570\u5b66\u8fd0\u7b97<\/p>\n<\/p>\n<p><p>SciPy\u662f\u57fa\u4e8eNumPy\u7684\u4e00\u4e2a\u7528\u4e8e\u79d1\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u5b83\u63d0\u4f9b\u4e86\u66f4\u591a\u9ad8\u7ea7\u7684\u6570\u5b66\u51fd\u6570\u548c\u7b97\u6cd5\uff0c\u9002\u7528\u4e8e\u9700\u8981\u590d\u6742\u6570\u5b66\u8fd0\u7b97\u7684\u573a\u5408\u3002<\/p>\n<\/p>\n<ol>\n<li>\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97<\/li>\n<\/ol>\n<p><p>SciPy\u7684\u7ebf\u6027\u4ee3\u6570\u6a21\u5757<code>scipy.linalg<\/code>\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u7ebf\u6027\u4ee3\u6570\u8fd0\u7b97\uff0c\u6bd4\u5982\u77e9\u9635\u5206\u89e3\u3001\u7279\u5f81\u503c\u8ba1\u7b97\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import scipy.linalg as la<\/p>\n<h2><strong>\u5b9a\u4e49\u77e9\u9635<\/strong><\/h2>\n<p>matrix = np.array([[1, 2], [3, 4]])<\/p>\n<h2><strong>\u8ba1\u7b97\u77e9\u9635\u7684\u884c\u5217\u5f0f<\/strong><\/h2>\n<p>determinant = la.det(matrix)<\/p>\n<h2><strong>\u8ba1\u7b97\u77e9\u9635\u7684\u9006<\/strong><\/h2>\n<p>inverse = la.inv(matrix)<\/p>\n<p>print(f&quot;Determinant: {determinant}, Inverse: {inverse}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u4f18\u5316\u548c\u65b9\u7a0b\u6c42\u89e3<\/li>\n<\/ol>\n<p><p>SciPy\u8fd8\u63d0\u4f9b\u4e86\u7528\u4e8e\u4f18\u5316\u548c\u65b9\u7a0b\u6c42\u89e3\u7684\u6a21\u5757\uff0c\u53ef\u4ee5\u7528\u4e8e\u6c42\u89e3\u975e\u7ebf\u6027\u65b9\u7a0b\u7ec4\u3001\u6267\u884c\u6700\u5c0f\u5316\u7b49\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from scipy.optimize import minimize<\/p>\n<h2><strong>\u5b9a\u4e49\u76ee\u6807\u51fd\u6570<\/strong><\/h2>\n<p>def objective_function(x):<\/p>\n<p>    return x2 + x + 2<\/p>\n<h2><strong>\u6267\u884c\u4f18\u5316<\/strong><\/h2>\n<p>result = minimize(objective_function, x0=0)<\/p>\n<p>optimal_value = result.x<\/p>\n<p>print(f&quot;Optimal Value: {optimal_value}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001MATPLOTLIB\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316<\/p>\n<\/p>\n<p><p>\u867d\u7136Matplotlib\u5e76\u4e0d\u662f\u4e13\u95e8\u7528\u4e8e\u6570\u5b66\u8ba1\u7b97\u7684\u5e93\uff0c\u4f46\u5b83\u53ef\u4ee5\u7528\u4e8e\u53ef\u89c6\u5316\u6570\u5b66\u51fd\u6570\u548c\u6570\u636e\u3002\u901a\u8fc7\u56fe\u5f62\u5316\u8868\u793a\uff0c\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u6570\u5b66\u8fd0\u7b97\u7684\u7ed3\u679c\u3002<\/p>\n<\/p>\n<ol>\n<li>\u7ed8\u5236\u57fa\u672c\u56fe\u5f62<\/li>\n<\/ol>\n<p><p>Matplotlib\u53ef\u4ee5\u7528\u6765\u7ed8\u5236\u5404\u79cd\u7c7b\u578b\u7684\u56fe\u5f62\uff0c\u6bd4\u5982\u6298\u7ebf\u56fe\u3001\u6563\u70b9\u56fe\u3001\u76f4\u65b9\u56fe\u7b49\u3002\u901a\u8fc7\u8fd9\u4e9b\u56fe\u5f62\uff0c\u53ef\u4ee5\u76f4\u89c2\u5730\u89c2\u5bdf\u6570\u636e\u7684\u5206\u5e03\u548c\u53d8\u5316\u8d8b\u52bf\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5b9a\u4e49\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-10, 10, 100)<\/p>\n<p>y = x2<\/p>\n<h2><strong>\u7ed8\u5236\u6298\u7ebf\u56fe<\/strong><\/h2>\n<p>plt.plot(x, y, label=&#39;y = x^2&#39;)<\/p>\n<p>plt.xlabel(&#39;x&#39;)<\/p>\n<p>plt.ylabel(&#39;y&#39;)<\/p>\n<p>plt.title(&#39;Plot of y = x^2&#39;)<\/p>\n<p>plt.legend()<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u9ad8\u7ea7\u56fe\u5f62\u529f\u80fd<\/li>\n<\/ol>\n<p><p>Matplotlib\u8fd8\u63d0\u4f9b\u4e86\u4e00\u4e9b\u9ad8\u7ea7\u56fe\u5f62\u529f\u80fd\uff0c\u6bd4\u59823D\u7ed8\u56fe\u3001\u70ed\u56fe\u7b49\uff0c\u53ef\u4ee5\u7528\u4e8e\u5c55\u793a\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u548c\u6570\u5b66\u51fd\u6570\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from mpl_toolkits.mplot3d import Axes3D<\/p>\n<h2><strong>\u521b\u5efa3D\u6570\u636e<\/strong><\/h2>\n<p>x = np.linspace(-5, 5, 100)<\/p>\n<p>y = np.linspace(-5, 5, 100)<\/p>\n<p>x, y = np.meshgrid(x, y)<\/p>\n<p>z = np.sin(np.sqrt(x&lt;strong&gt;2 + y&lt;\/strong&gt;2))<\/p>\n<h2><strong>\u7ed8\u52363D\u66f2\u9762<\/strong><\/h2>\n<p>fig = plt.figure()<\/p>\n<p>ax = fig.add_subplot(111, projection=&#39;3d&#39;)<\/p>\n<p>ax.plot_surface(x, y, z, cmap=&#39;viridis&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001PANDAS\u7528\u4e8e\u6570\u636e\u5904\u7406\u548c\u5206\u6790<\/p>\n<\/p>\n<p><p>\u867d\u7136Pandas\u4e3b\u8981\u7528\u4e8e\u6570\u636e\u5904\u7406\u548c\u5206\u6790\uff0c\u4f46\u5b83\u4e5f\u652f\u6301\u4e00\u4e9b\u57fa\u672c\u7684\u6570\u5b66\u8fd0\u7b97\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u6570\u636e\u6846\u548c\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<ol>\n<li>\u6570\u636e\u6846\u8fd0\u7b97<\/li>\n<\/ol>\n<p><p>Pandas\u7684\u6570\u636e\u6846\u7c7b\u4f3c\u4e8e\u6570\u636e\u5e93\u4e2d\u7684\u8868\u683c\uff0c\u53ef\u4ee5\u7528\u4e8e\u5b58\u50a8\u548c\u64cd\u4f5c\u7ed3\u6784\u5316\u6570\u636e\u3002\u652f\u6301\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\uff0c\u6bd4\u5982\u52a0\u51cf\u4e58\u9664\u3001\u7edf\u8ba1\u5206\u6790\u7b49\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;A&#39;: [1, 2, 3], &#39;B&#39;: [4, 5, 6]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8ba1\u7b97\u5217\u7684\u548c<\/strong><\/h2>\n<p>sum_A = df[&#39;A&#39;].sum()<\/p>\n<h2><strong>\u8ba1\u7b97\u5217\u7684\u5e73\u5747\u503c<\/strong><\/h2>\n<p>mean_B = df[&#39;B&#39;].mean()<\/p>\n<p>print(f&quot;Sum of A: {sum_A}, Mean of B: {mean_B}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u65f6\u95f4\u5e8f\u5217\u5206\u6790<\/li>\n<\/ol>\n<p><p>Pandas\u8fd8\u652f\u6301\u65f6\u95f4\u5e8f\u5217\u6570\u636e\u7684\u5904\u7406\uff0c\u53ef\u4ee5\u8fdb\u884c\u65f6\u95f4\u5e8f\u5217\u7684\u6ed1\u52a8\u7a97\u53e3\u8ba1\u7b97\u3001\u6307\u6570\u5e73\u6ed1\u7b49\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u521b\u5efa\u65f6\u95f4\u5e8f\u5217<\/p>\n<p>dates = pd.date_range(&#39;20230101&#39;, periods=6)<\/p>\n<p>ts = pd.Series([1, 2, 3, 4, 5, 6], index=dates)<\/p>\n<h2><strong>\u6ed1\u52a8\u7a97\u53e3\u5e73\u5747\u503c<\/strong><\/h2>\n<p>rolling_mean = ts.rolling(window=3).mean()<\/p>\n<p>print(f&quot;Rolling Mean:\\n{rolling_mean}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528SCIKIT-LEARN\u8fdb\u884c<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a><\/p>\n<\/p>\n<p><p>Scikit-learn\u662f\u4e00\u4e2a\u7528\u4e8e\u673a\u5668\u5b66\u4e60\u7684Python\u5e93\uff0c\u63d0\u4f9b\u4e86\u4e00\u7cfb\u5217\u7528\u4e8e\u5206\u7c7b\u3001\u56de\u5f52\u3001\u805a\u7c7b\u7b49\u4efb\u52a1\u7684\u673a\u5668\u5b66\u4e60\u7b97\u6cd5\u3002<\/p>\n<\/p>\n<ol>\n<li>\u7ebf\u6027\u56de\u5f52<\/li>\n<\/ol>\n<p><p>Scikit-learn\u7684\u7ebf\u6027\u6a21\u578b\u6a21\u5757<code>sklearn.linear_model<\/code>\u53ef\u4ee5\u7528\u4e8e\u6267\u884c\u7ebf\u6027\u56de\u5f52\u5206\u6790\uff0c\u9002\u7528\u4e8e\u7814\u7a76\u81ea\u53d8\u91cf\u548c\u56e0\u53d8\u91cf\u4e4b\u95f4\u7684\u7ebf\u6027\u5173\u7cfb\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.linear_model import LinearRegression<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>X = np.array([[1], [2], [3], [4], [5]])<\/p>\n<p>y = np.array([1, 2, 3, 4, 5])<\/p>\n<h2><strong>\u521b\u5efa\u5e76\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = LinearRegression()<\/p>\n<p>model.fit(X, y)<\/p>\n<h2><strong>\u9884\u6d4b<\/strong><\/h2>\n<p>predictions = model.predict(X)<\/p>\n<p>print(f&quot;Predictions: {predictions}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li>\u805a\u7c7b\u5206\u6790<\/li>\n<\/ol>\n<p><p>Scikit-learn\u7684\u805a\u7c7b\u6a21\u5757<code>sklearn.cluster<\/code>\u63d0\u4f9b\u4e86\u5404\u79cd\u805a\u7c7b\u7b97\u6cd5\uff0c\u6bd4\u5982K\u5747\u503c\u805a\u7c7b\uff0c\u53ef\u4ee5\u7528\u4e8e\u5c06\u6570\u636e\u5206\u4e3a\u591a\u4e2a\u7c7b\u522b\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.cluster import KMeans<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e<\/strong><\/h2>\n<p>data = np.array([[1, 2], [1, 4], [1, 0],<\/p>\n<p>                 [4, 2], [4, 4], [4, 0]])<\/p>\n<h2><strong>\u6267\u884cK\u5747\u503c\u805a\u7c7b<\/strong><\/h2>\n<p>kmeans = KMeans(n_clusters=2, random_state=0)<\/p>\n<p>kmeans.fit(data)<\/p>\n<h2><strong>\u83b7\u53d6\u805a\u7c7b\u7ed3\u679c<\/strong><\/h2>\n<p>labels = kmeans.labels_<\/p>\n<p>print(f&quot;Cluster Labels: {labels}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u4e9bPython\u6570\u5b66\u5e93\u548c\u5de5\u5177\uff0c\u60a8\u53ef\u4ee5\u6267\u884c\u5404\u79cd\u6570\u5b66\u8fd0\u7b97\u548c\u6570\u636e\u5206\u6790\u4efb\u52a1\u3002\u65e0\u8bba\u662f\u57fa\u672c\u7684\u6570\u5b66\u8ba1\u7b97\u3001\u9ad8\u7ea7\u7684\u79d1\u5b66\u8ba1\u7b97\uff0c\u8fd8\u662f\u6570\u636e\u53ef\u89c6\u5316\u548c\u673a\u5668\u5b66\u4e60\uff0cPython\u90fd\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u529f\u80fd\u548c\u4fbf\u6377\u7684\u64cd\u4f5c\u3002\u5bf9\u4e8e\u9700\u8981\u8fdb\u884c\u590d\u6742\u6570\u5b66\u8fd0\u7b97\u548c\u5206\u6790\u7684\u573a\u5408\uff0c\u8fd9\u4e9b\u5e93\u548c\u5de5\u5177\u90fd\u662f\u4e0d\u53ef\u6216\u7f3a\u7684\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5728Python\u4e2d\u5982\u4f55\u5bfc\u5165\u6570\u5b66\u5e93\uff1f<\/strong><br \/>\u8981\u4f7f\u7528Python\u7684\u6570\u5b66\u5e93\uff0c\u60a8\u9700\u8981\u5728\u4ee3\u7801\u7684\u5f00\u5934\u5bfc\u5165\u5b83\u3002\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u8bed\u53e5\uff1a<code>import math<\/code>\u3002\u5bfc\u5165\u540e\uff0c\u60a8\u5c31\u53ef\u4ee5\u8bbf\u95ee\u5e93\u4e2d\u7684\u5404\u79cd\u6570\u5b66\u51fd\u6570\u548c\u5e38\u91cf\uff0c\u4f8b\u5982<code>math.pi<\/code>\u548c<code>math.sqrt()<\/code>\u7b49\u3002<\/p>\n<p><strong>Python\u6570\u5b66\u5e93\u63d0\u4f9b\u4e86\u54ea\u4e9b\u5e38\u7528\u7684\u6570\u5b66\u51fd\u6570\uff1f<\/strong><br \/>Python\u7684\u6570\u5b66\u5e93\u5305\u542b\u4e86\u8bb8\u591a\u5e38\u7528\u7684\u6570\u5b66\u51fd\u6570\uff0c\u5982\u4e09\u89d2\u51fd\u6570\uff08<code>math.sin()<\/code>\u3001<code>math.cos()<\/code>\u3001<code>math.tan()<\/code>\uff09\u3001\u5bf9\u6570\u51fd\u6570\uff08<code>math.log()<\/code>\uff09\u3001\u5e73\u65b9\u6839\uff08<code>math.sqrt()<\/code>\uff09\u4ee5\u53ca\u5e38\u6570\uff08\u5982<code>math.pi<\/code>\u3001<code>math.e<\/code>\uff09\u3002\u8fd9\u4e9b\u51fd\u6570\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u8fdb\u884c\u590d\u6742\u7684\u6570\u5b66\u8ba1\u7b97\u3002<\/p>\n<p><strong>\u5982\u4f55\u4f7f\u7528Python\u6570\u5b66\u5e93\u8fdb\u884c\u7edf\u8ba1\u8ba1\u7b97\uff1f<\/strong><br \/>\u867d\u7136Python\u7684\u6570\u5b66\u5e93\u4e3b\u8981\u4fa7\u91cd\u4e8e\u57fa\u7840\u6570\u5b66\u8ba1\u7b97\uff0c\u4f46\u60a8\u53ef\u4ee5\u7ed3\u5408\u4f7f\u7528<code>math<\/code>\u5e93\u548c\u5176\u4ed6\u5e93\uff08\u5982<code>statistics<\/code>\u6216<code>numpy<\/code>\uff09\u6765\u5b8c\u6210\u7edf\u8ba1\u8ba1\u7b97\u3002\u6bd4\u5982\uff0c\u4f7f\u7528<code>numpy.mean()<\/code>\u53ef\u4ee5\u8ba1\u7b97\u5e73\u5747\u503c\uff0c<code>numpy.std()<\/code>\u53ef\u4ee5\u8ba1\u7b97\u6807\u51c6\u5dee\uff0c\u8fd9\u4e9b\u529f\u80fd\u589e\u5f3a\u4e86Python\u5728\u6570\u636e\u5206\u6790\u548c\u7edf\u8ba1\u9886\u57df\u7684\u5e94\u7528\u80fd\u529b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\u4f7f\u7528\u6570\u5b66\u5e93\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bfc\u5165Python\u7684\u5185\u7f6e\u6a21\u5757math\u3001\u4f7f\u7528NumPy\u5e93\u3001\u4ee5\u53ca\u4f7f\u7528SymPy [&hellip;]","protected":false},"author":3,"featured_media":1019778,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019775"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1019775"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019775\/revisions"}],"predecessor-version":[{"id":1019779,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1019775\/revisions\/1019779"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1019778"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1019775"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1019775"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1019775"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}