{"id":1124879,"date":"2025-01-08T19:45:56","date_gmt":"2025-01-08T11:45:56","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1124879.html"},"modified":"2025-01-08T19:45:59","modified_gmt":"2025-01-08T11:45:59","slug":"python%e5%a6%82%e4%bd%95%e8%a7%a3%e5%86%b3%e8%b4%ad%e4%b9%b0%e4%b8%9c%e8%a5%bf%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1124879.html","title":{"rendered":"python\u5982\u4f55\u89e3\u51b3\u8d2d\u4e70\u4e1c\u897f\u65b9\u6848"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25085731\/66744876-2199-4dc0-b20c-0da50adc41fc.webp\" alt=\"python\u5982\u4f55\u89e3\u51b3\u8d2d\u4e70\u4e1c\u897f\u65b9\u6848\" \/><\/p>\n<p><p> <strong>Python\u5982\u4f55\u89e3\u51b3\u8d2d\u4e70\u4e1c\u897f\u65b9\u6848<\/strong><\/p>\n<\/p>\n<p><p><strong>Python\u53ef\u4ee5\u901a\u8fc7\u81ea\u52a8\u5316\u811a\u672c\u3001\u6570\u636e\u5206\u6790\u3001\u51b3\u7b56\u6811\u6a21\u578b\u3001\u63a8\u8350\u7b97\u6cd5\u3001\u7528\u6237\u884c\u4e3a\u5206\u6790\u6765\u89e3\u51b3\u8d2d\u4e70\u4e1c\u897f\u65b9\u6848\u3002<\/strong> \u5728\u8fd9\u4e9b\u65b9\u6cd5\u4e2d\uff0c<strong>\u81ea\u52a8\u5316\u811a\u672c<\/strong>\u662f\u975e\u5e38\u5b9e\u7528\u7684\u5de5\u5177\uff0c\u5b83\u53ef\u4ee5\u5e2e\u52a9\u7528\u6237\u5728\u591a\u4e2a\u7535\u5546\u5e73\u53f0\u4e0a\u81ea\u52a8\u641c\u7d22\u5e76\u6bd4\u8f83\u5546\u54c1\u4ef7\u683c\uff0c\u627e\u5230\u6700\u4f73\u8d2d\u4e70\u9009\u9879\u3002\u901a\u8fc7\u81ea\u52a8\u5316\u811a\u672c\uff0c\u6211\u4eec\u53ef\u4ee5\u8282\u7701\u5927\u91cf\u7684\u65f6\u95f4\u548c\u7cbe\u529b\uff0c\u540c\u65f6\u786e\u4fdd\u83b7\u5f97\u6700\u4f18\u60e0\u7684\u4ef7\u683c\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u81ea\u52a8\u5316\u811a\u672c<\/h3>\n<\/p>\n<p><p>\u81ea\u52a8\u5316\u811a\u672c\u662f\u6307\u901a\u8fc7\u7f16\u5199\u4ee3\u7801\u6765\u6a21\u62df\u7528\u6237\u64cd\u4f5c\uff0c\u4ece\u800c\u5b9e\u73b0\u81ea\u52a8\u5316\u4efb\u52a1\u7684\u8fc7\u7a0b\u3002\u5728\u8d2d\u4e70\u4e1c\u897f\u65f6\uff0c\u81ea\u52a8\u5316\u811a\u672c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u81ea\u52a8\u641c\u7d22\u5546\u54c1\u3001\u6bd4\u8f83\u4ef7\u683c\u3001\u83b7\u53d6\u5546\u54c1\u8bc4\u4ef7\u7b49\u3002Python\u7684Selenium\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u7528\u6765\u7f16\u5199\u81ea\u52a8\u5316\u811a\u672c\u3002<\/p>\n<\/p>\n<p><h4>1. Selenium\u7684\u57fa\u672c\u4f7f\u7528<\/h4>\n<\/p>\n<p><p>Selenium\u662f\u4e00\u4e2a\u7528\u4e8e\u81ea\u52a8\u5316\u6d4f\u89c8\u5668\u64cd\u4f5c\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u6a21\u62df\u7528\u6237\u5728\u6d4f\u89c8\u5668\u4e0a\u7684\u5404\u79cd\u64cd\u4f5c\uff0c\u5982\u70b9\u51fb\u3001\u8f93\u5165\u3001\u6eda\u52a8\u7b49\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Selenium\u5728\u67d0\u7535\u5546\u5e73\u53f0\u4e0a\u641c\u7d22\u5546\u54c1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from selenium import webdriver<\/p>\n<p>from selenium.webdriver.common.keys import Keys<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aChrome\u6d4f\u89c8\u5668\u5b9e\u4f8b<\/strong><\/h2>\n<p>driver = webdriver.Chrome()<\/p>\n<h2><strong>\u6253\u5f00\u7535\u5546\u5e73\u53f0<\/strong><\/h2>\n<p>driver.get(&quot;https:\/\/www.example.com&quot;)<\/p>\n<h2><strong>\u627e\u5230\u641c\u7d22\u6846\u5e76\u8f93\u5165\u5546\u54c1\u540d\u79f0<\/strong><\/h2>\n<p>search_box = driver.find_element_by_name(&quot;q&quot;)<\/p>\n<p>search_box.send_keys(&quot;laptop&quot;)<\/p>\n<p>search_box.send_keys(Keys.RETURN)<\/p>\n<h2><strong>\u83b7\u53d6\u641c\u7d22\u7ed3\u679c<\/strong><\/h2>\n<p>results = driver.find_elements_by_class_name(&quot;result-item&quot;)<\/p>\n<p>for result in results:<\/p>\n<p>    print(result.text)<\/p>\n<h2><strong>\u5173\u95ed\u6d4f\u89c8\u5668<\/strong><\/h2>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u81ea\u52a8\u5316\u811a\u672c\u7684\u9ad8\u7ea7\u5e94\u7528<\/h4>\n<\/p>\n<p><p>\u9664\u4e86\u7b80\u5355\u7684\u641c\u7d22\u64cd\u4f5c\uff0c\u81ea\u52a8\u5316\u811a\u672c\u8fd8\u53ef\u4ee5\u6267\u884c\u66f4\u590d\u6742\u7684\u4efb\u52a1\uff0c\u5982\u81ea\u52a8\u767b\u5f55\u3001\u81ea\u52a8\u4e0b\u5355\u7b49\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u811a\u672c\uff0c\u5728\u591a\u4e2a\u7535\u5546\u5e73\u53f0\u4e0a\u641c\u7d22\u540c\u4e00\u5546\u54c1\uff0c\u5e76\u6bd4\u8f83\u5176\u4ef7\u683c\uff0c\u4ece\u800c\u627e\u5230\u6700\u4f18\u60e0\u7684\u8d2d\u4e70\u9009\u9879\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def search_product(driver, url, product_name):<\/p>\n<p>    driver.get(url)<\/p>\n<p>    search_box = driver.find_element_by_name(&quot;q&quot;)<\/p>\n<p>    search_box.send_keys(product_name)<\/p>\n<p>    search_box.send_keys(Keys.RETURN)<\/p>\n<p>    results = driver.find_elements_by_class_name(&quot;result-item&quot;)<\/p>\n<p>    return [result.text for result in results]<\/p>\n<p>driver = webdriver.Chrome()<\/p>\n<p>platforms = [<\/p>\n<p>    &quot;https:\/\/www.example1.com&quot;,<\/p>\n<p>    &quot;https:\/\/www.example2.com&quot;,<\/p>\n<p>    &quot;https:\/\/www.example3.com&quot;<\/p>\n<p>]<\/p>\n<p>product_name = &quot;laptop&quot;<\/p>\n<p>all_results = []<\/p>\n<p>for platform in platforms:<\/p>\n<p>    all_results.extend(search_product(driver, platform, product_name))<\/p>\n<h2><strong>\u6bd4\u8f83\u4ef7\u683c\u5e76\u627e\u5230\u6700\u4f18\u60e0\u7684\u9009\u9879<\/strong><\/h2>\n<p>best_option = min(all_results, key=lambda x: float(x.split(&#39;$&#39;)[-1]))<\/p>\n<p>print(f&quot;\u6700\u4f18\u60e0\u7684\u9009\u9879\u662f\uff1a{best_option}&quot;)<\/p>\n<p>driver.quit()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u6570\u636e\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u5206\u6790\u662f\u901a\u8fc7\u5bf9\u5927\u91cf\u6570\u636e\u8fdb\u884c\u5904\u7406\u548c\u5206\u6790\uff0c\u627e\u51fa\u5176\u4e2d\u7684\u89c4\u5f8b\u548c\u6a21\u5f0f\uff0c\u4ece\u800c\u4e3a\u51b3\u7b56\u63d0\u4f9b\u4f9d\u636e\u3002\u5728\u8d2d\u4e70\u4e1c\u897f\u65f6\uff0c\u901a\u8fc7\u5bf9\u5546\u54c1\u7684\u5386\u53f2\u4ef7\u683c\u3001\u7528\u6237\u8bc4\u4ef7\u7b49\u6570\u636e\u8fdb\u884c\u5206\u6790\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u505a\u51fa\u66f4\u660e\u667a\u7684\u8d2d\u4e70\u51b3\u7b56\u3002<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u548c\u6e05\u6d17\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u5546\u54c1\u7684\u5386\u53f2\u4ef7\u683c\u548c\u7528\u6237\u8bc4\u4ef7\u7b49\u6570\u636e\u3002\u8fd9\u4e9b\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u7535\u5546\u5e73\u53f0\u7684API\u6216\u8005\u7f51\u9875\u722c\u866b\u6765\u83b7\u53d6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Python\u7684requests\u5e93\u548cBeautifulSoup\u5e93\u83b7\u53d6\u67d0\u7535\u5546\u5e73\u53f0\u4e0a\u7684\u5546\u54c1\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import requests<\/p>\n<p>from bs4 import BeautifulSoup<\/p>\n<p>def get_product_data(url):<\/p>\n<p>    response = requests.get(url)<\/p>\n<p>    soup = BeautifulSoup(response.content, &#39;html.parser&#39;)<\/p>\n<p>    product_data = {<\/p>\n<p>        &#39;name&#39;: soup.find(&#39;h1&#39;, class_=&#39;product-title&#39;).text,<\/p>\n<p>        &#39;price&#39;: float(soup.find(&#39;span&#39;, class_=&#39;product-price&#39;).text.strip(&#39;$&#39;)),<\/p>\n<p>        &#39;reviews&#39;: [review.text for review in soup.find_all(&#39;div&#39;, class_=&#39;review-content&#39;)]<\/p>\n<p>    }<\/p>\n<p>    return product_data<\/p>\n<p>url = &quot;https:\/\/www.example.com\/product\/12345&quot;<\/p>\n<p>product_data = get_product_data(url)<\/p>\n<p>print(product_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5206\u6790\u548c\u53ef\u89c6\u5316\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u83b7\u53d6\u6570\u636e\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Pandas\u3001Matplotlib\u7b49\u5e93\u5bf9\u6570\u636e\u8fdb\u884c\u5206\u6790\u548c\u53ef\u89c6\u5316\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u5206\u6790\u5546\u54c1\u7684\u5386\u53f2\u4ef7\u683c\u8d70\u52bf\uff0c\u5224\u65ad\u4f55\u65f6\u662f\u8d2d\u4e70\u8be5\u5546\u54c1\u7684\u6700\u4f73\u65f6\u673a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import matplotlib.pyplot as plt<\/p>\n<h2><strong>\u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u83b7\u53d6\u4e86\u67d0\u5546\u54c1\u7684\u5386\u53f2\u4ef7\u683c\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;date&#39;: [&#39;2023-01-01&#39;, &#39;2023-02-01&#39;, &#39;2023-03-01&#39;, &#39;2023-04-01&#39;],<\/p>\n<p>    &#39;price&#39;: [1000, 950, 900, 850]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5206\u6790\u4ef7\u683c\u8d70\u52bf<\/strong><\/h2>\n<p>plt.plot(df[&#39;date&#39;], df[&#39;price&#39;])<\/p>\n<p>plt.xlabel(&#39;Date&#39;)<\/p>\n<p>plt.ylabel(&#39;Price&#39;)<\/p>\n<p>plt.title(&#39;Price Trend&#39;)<\/p>\n<p>plt.show()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u6837\u7684\u5206\u6790\u548c\u53ef\u89c6\u5316\uff0c\u6211\u4eec\u53ef\u4ee5\u76f4\u89c2\u5730\u770b\u5230\u5546\u54c1\u4ef7\u683c\u7684\u53d8\u5316\u8d8b\u52bf\uff0c\u4ece\u800c\u505a\u51fa\u66f4\u660e\u667a\u7684\u8d2d\u4e70\u51b3\u7b56\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u51b3\u7b56\u6811\u6a21\u578b<\/h3>\n<\/p>\n<p><p>\u51b3\u7b56\u6811\u662f\u4e00\u79cd\u5e38\u7528\u7684<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u7b97\u6cd5\uff0c\u53ef\u4ee5\u901a\u8fc7\u5bf9\u7279\u5f81\u7684\u9009\u62e9\u548c\u5206\u88c2\uff0c\u6784\u5efa\u4e00\u4e2a\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u4ece\u800c\u5bf9\u65b0\u7684\u6837\u672c\u8fdb\u884c\u9884\u6d4b\u3002\u5728\u8d2d\u4e70\u4e1c\u897f\u65f6\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u6784\u5efa\u51b3\u7b56\u6811\u6a21\u578b\uff0c\u9884\u6d4b\u67d0\u5546\u54c1\u7684\u4ef7\u683c\u8d70\u52bf\u6216\u7528\u6237\u6ee1\u610f\u5ea6\uff0c\u4ece\u800c\u505a\u51fa\u66f4\u597d\u7684\u8d2d\u4e70\u51b3\u7b56\u3002<\/p>\n<\/p>\n<p><h4>1. \u6784\u5efa\u51b3\u7b56\u6811\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u51c6\u5907\u8bad\u7ec3\u6570\u636e\uff0c\u8fd9\u4e9b\u6570\u636e\u5305\u62ec\u5546\u54c1\u7684\u5404\u9879\u7279\u5f81\uff08\u5982\u4ef7\u683c\u3001\u8bc4\u4ef7\u3001\u9500\u91cf\u7b49\uff09\u4ee5\u53ca\u76ee\u6807\u53d8\u91cf\uff08\u5982\u7528\u6237\u6ee1\u610f\u5ea6\uff09\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Scikit-learn\u5e93\u6784\u5efa\u51b3\u7b56\u6811\u6a21\u578b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.tree import DecisionTreeRegressor<\/p>\n<p>from sklearn.model_selection import tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>n_test_split<\/p>\n<h2><strong>\u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u83b7\u53d6\u4e86\u67d0\u5546\u54c1\u7684\u5386\u53f2\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;price&#39;: [1000, 950, 900, 850],<\/p>\n<p>    &#39;rating&#39;: [4.5, 4.6, 4.7, 4.8],<\/p>\n<p>    &#39;sales&#39;: [500, 600, 700, 800],<\/p>\n<p>    &#39;satisfaction&#39;: [90, 92, 94, 96]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u6784\u5efa\u7279\u5f81\u77e9\u9635\u548c\u76ee\u6807\u53d8\u91cf<\/strong><\/h2>\n<p>X = df[[&#39;price&#39;, &#39;rating&#39;, &#39;sales&#39;]]<\/p>\n<p>y = df[&#39;satisfaction&#39;]<\/p>\n<h2><strong>\u5212\u5206\u8bad\u7ec3\u96c6\u548c\u6d4b\u8bd5\u96c6<\/strong><\/h2>\n<p>X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)<\/p>\n<h2><strong>\u6784\u5efa\u51b3\u7b56\u6811\u6a21\u578b<\/strong><\/h2>\n<p>model = DecisionTreeRegressor()<\/p>\n<p>model.fit(X_train, y_train)<\/p>\n<h2><strong>\u9884\u6d4b\u7528\u6237\u6ee1\u610f\u5ea6<\/strong><\/h2>\n<p>y_pred = model.predict(X_test)<\/p>\n<p>print(y_pred)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u4f18\u5316\u548c\u8bc4\u4f30\u6a21\u578b<\/h4>\n<\/p>\n<p><p>\u6784\u5efa\u6a21\u578b\u540e\uff0c\u6211\u4eec\u9700\u8981\u5bf9\u6a21\u578b\u8fdb\u884c\u4f18\u5316\u548c\u8bc4\u4f30\uff0c\u4ee5\u786e\u4fdd\u5176\u9884\u6d4b\u6548\u679c\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ea4\u53c9\u9a8c\u8bc1\u3001\u7f51\u683c\u641c\u7d22\u7b49\u65b9\u6cd5\u5bf9\u6a21\u578b\u8fdb\u884c\u8c03\u4f18\uff0c\u5e76\u4f7f\u7528\u5747\u65b9\u8bef\u5dee\uff08MSE\uff09\u3001R\u65b9\u7b49\u6307\u6807\u5bf9\u6a21\u578b\u8fdb\u884c\u8bc4\u4f30\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.metrics import mean_squared_error, r2_score<\/p>\n<p>from sklearn.model_selection import GridSearchCV<\/p>\n<h2><strong>\u8bc4\u4f30\u6a21\u578b<\/strong><\/h2>\n<p>mse = mean_squared_error(y_test, y_pred)<\/p>\n<p>r2 = r2_score(y_test, y_pred)<\/p>\n<p>print(f&quot;MSE: {mse}, R2: {r2}&quot;)<\/p>\n<h2><strong>\u8c03\u4f18\u6a21\u578b<\/strong><\/h2>\n<p>param_grid = {&#39;max_depth&#39;: [None, 10, 20, 30], &#39;min_samples_split&#39;: [2, 5, 10]}<\/p>\n<p>grid_search = GridSearchCV(DecisionTreeRegressor(), param_grid, cv=5)<\/p>\n<p>grid_search.fit(X_train, y_train)<\/p>\n<p>print(f&quot;Best parameters: {grid_search.best_params_}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4f18\u5316\u548c\u8bc4\u4f30\u6a21\u578b\uff0c\u6211\u4eec\u53ef\u4ee5\u63d0\u9ad8\u5176\u9884\u6d4b\u6548\u679c\uff0c\u4ece\u800c\u66f4\u51c6\u786e\u5730\u9884\u6d4b\u5546\u54c1\u7684\u4ef7\u683c\u8d70\u52bf\u6216\u7528\u6237\u6ee1\u610f\u5ea6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u63a8\u8350\u7b97\u6cd5<\/h3>\n<\/p>\n<p><p>\u63a8\u8350\u7b97\u6cd5\u662f\u4e00\u79cd\u901a\u8fc7\u5206\u6790\u7528\u6237\u884c\u4e3a\u548c\u504f\u597d\uff0c\u5411\u7528\u6237\u63a8\u8350\u53ef\u80fd\u611f\u5174\u8da3\u7684\u5546\u54c1\u7684\u6280\u672f\u3002\u5728\u8d2d\u4e70\u4e1c\u897f\u65f6\uff0c\u63a8\u8350\u7b97\u6cd5\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u53d1\u73b0\u4e00\u4e9b\u53ef\u80fd\u4f1a\u559c\u6b22\u4f46\u5c1a\u672a\u53d1\u73b0\u7684\u5546\u54c1\uff0c\u4ece\u800c\u63d0\u9ad8\u8d2d\u4e70\u7684\u6ee1\u610f\u5ea6\u3002<\/p>\n<\/p>\n<p><h4>1. \u57fa\u4e8e\u534f\u540c\u8fc7\u6ee4\u7684\u63a8\u8350\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>\u534f\u540c\u8fc7\u6ee4\u662f\u4e00\u79cd\u5e38\u7528\u7684\u63a8\u8350\u7b97\u6cd5\uff0c\u901a\u8fc7\u5206\u6790\u7528\u6237\u4e4b\u95f4\u7684\u76f8\u4f3c\u6027\uff0c\u5411\u7528\u6237\u63a8\u8350\u5176\u4ed6\u76f8\u4f3c\u7528\u6237\u559c\u6b22\u7684\u5546\u54c1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Surprise\u5e93\u5b9e\u73b0\u57fa\u4e8e\u534f\u540c\u8fc7\u6ee4\u7684\u63a8\u8350\u7b97\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from surprise import Dataset, Reader, SVD<\/p>\n<p>from surprise.model_selection import train_test_split<\/p>\n<h2><strong>\u51c6\u5907\u6570\u636e<\/strong><\/h2>\n<p>data = Dataset.load_builtin(&#39;ml-100k&#39;)<\/p>\n<p>trainset, testset = train_test_split(data, test_size=0.2)<\/p>\n<h2><strong>\u6784\u5efa\u548c\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model = SVD()<\/p>\n<p>model.fit(trainset)<\/p>\n<h2><strong>\u9884\u6d4b\u8bc4\u5206<\/strong><\/h2>\n<p>predictions = model.test(testset)<\/p>\n<h2><strong>\u8bc4\u4f30\u6a21\u578b<\/strong><\/h2>\n<p>accuracy.rmse(predictions)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u57fa\u4e8e\u5185\u5bb9\u7684\u63a8\u8350\u7b97\u6cd5<\/h4>\n<\/p>\n<p><p>\u5185\u5bb9\u63a8\u8350\u7b97\u6cd5\u662f\u901a\u8fc7\u5206\u6790\u5546\u54c1\u7684\u5185\u5bb9\u7279\u5f81\uff0c\u5411\u7528\u6237\u63a8\u8350\u4e0e\u5176\u5386\u53f2\u8d2d\u4e70\u8bb0\u5f55\u6216\u6d4f\u89c8\u8bb0\u5f55\u76f8\u4f3c\u7684\u5546\u54c1\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Scikit-learn\u5e93\u5b9e\u73b0\u57fa\u4e8e\u5185\u5bb9\u7684\u63a8\u8350\u7b97\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.feature_extraction.text import TfidfVectorizer<\/p>\n<p>from sklearn.metrics.pairwise import cosine_similarity<\/p>\n<h2><strong>\u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u83b7\u53d6\u4e86\u5546\u54c1\u7684\u63cf\u8ff0\u6570\u636e<\/strong><\/h2>\n<p>descriptions = [<\/p>\n<p>    &quot;This is a high-quality laptop with a powerful processor and large storage.&quot;,<\/p>\n<p>    &quot;A budget-friendly laptop with decent performance and good battery life.&quot;,<\/p>\n<p>    &quot;An ultra-thin laptop with a sleek design and high-resolution display.&quot;,<\/p>\n<p>    &quot;A gaming laptop with a powerful GPU and excellent cooling system.&quot;<\/p>\n<p>]<\/p>\n<h2><strong>\u6784\u5efaTF-IDF\u77e9\u9635<\/strong><\/h2>\n<p>vectorizer = TfidfVectorizer()<\/p>\n<p>tfidf_matrix = vectorizer.fit_transform(descriptions)<\/p>\n<h2><strong>\u8ba1\u7b97\u76f8\u4f3c\u5ea6<\/strong><\/h2>\n<p>similarity_matrix = cosine_similarity(tfidf_matrix)<\/p>\n<h2><strong>\u63a8\u8350\u76f8\u4f3c\u5546\u54c1<\/strong><\/h2>\n<p>def recommend_similar(product_index, top_n=3):<\/p>\n<p>    similar_indices = similarity_matrix[product_index].argsort()[-top_n-1:-1][::-1]<\/p>\n<p>    return similar_indices<\/p>\n<p>print(recommend_similar(0))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u63a8\u8350\u7b97\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u5411\u7528\u6237\u63a8\u8350\u4e00\u4e9b\u4ed6\u4eec\u53ef\u80fd\u4f1a\u559c\u6b22\u7684\u5546\u54c1\uff0c\u4ece\u800c\u63d0\u9ad8\u8d2d\u4e70\u7684\u6ee1\u610f\u5ea6\u548c\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u7528\u6237\u884c\u4e3a\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u7528\u6237\u884c\u4e3a\u5206\u6790\u662f\u901a\u8fc7\u5bf9\u7528\u6237\u7684\u6d4f\u89c8\u3001\u70b9\u51fb\u3001\u8d2d\u4e70\u7b49\u884c\u4e3a\u8fdb\u884c\u5206\u6790\uff0c\u627e\u51fa\u5176\u4e2d\u7684\u89c4\u5f8b\u548c\u6a21\u5f0f\uff0c\u4ece\u800c\u4e3a\u51b3\u7b56\u63d0\u4f9b\u4f9d\u636e\u3002\u5728\u8d2d\u4e70\u4e1c\u897f\u65f6\uff0c\u901a\u8fc7\u5206\u6790\u7528\u6237\u7684\u884c\u4e3a\u6570\u636e\uff0c\u53ef\u4ee5\u5e2e\u52a9\u6211\u4eec\u66f4\u597d\u5730\u7406\u89e3\u7528\u6237\u7684\u9700\u6c42\u548c\u504f\u597d\uff0c\u4ece\u800c\u63d0\u4f9b\u66f4\u4e2a\u6027\u5316\u7684\u63a8\u8350\u548c\u670d\u52a1\u3002<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u548c\u5206\u6790\u7528\u6237\u884c\u4e3a\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u6211\u4eec\u9700\u8981\u83b7\u53d6\u7528\u6237\u7684\u884c\u4e3a\u6570\u636e\uff0c\u8fd9\u4e9b\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u7f51\u7ad9\u7684\u65e5\u5fd7\u3001\u7b2c\u4e09\u65b9\u5206\u6790\u5de5\u5177\u7b49\u9014\u5f84\u83b7\u53d6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Pandas\u5e93\u5206\u6790\u7528\u6237\u7684\u6d4f\u89c8\u884c\u4e3a\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u83b7\u53d6\u4e86\u7528\u6237\u7684\u6d4f\u89c8\u884c\u4e3a\u6570\u636e<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;user_id&#39;: [1, 1, 2, 2, 3, 3, 3],<\/p>\n<p>    &#39;product_id&#39;: [101, 102, 101, 103, 102, 104, 105],<\/p>\n<p>    &#39;action&#39;: [&#39;view&#39;, &#39;view&#39;, &#39;view&#39;, &#39;view&#39;, &#39;view&#39;, &#39;view&#39;, &#39;view&#39;],<\/p>\n<p>    &#39;timestamp&#39;: [&#39;2023-01-01 10:00:00&#39;, &#39;2023-01-01 10:05:00&#39;, &#39;2023-01-02 11:00:00&#39;, &#39;2023-01-02 11:10:00&#39;, &#39;2023-01-03 12:00:00&#39;, &#39;2023-01-03 12:15:00&#39;, &#39;2023-01-03 12:20:00&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5206\u6790\u7528\u6237\u7684\u6d4f\u89c8\u884c\u4e3a<\/strong><\/h2>\n<p>view_counts = df.groupby(&#39;product_id&#39;).size().reset_index(name=&#39;view_count&#39;)<\/p>\n<p>print(view_counts)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u63d0\u53d6\u7528\u6237\u504f\u597d\u548c\u5174\u8da3<\/h4>\n<\/p>\n<p><p>\u901a\u8fc7\u5206\u6790\u7528\u6237\u7684\u884c\u4e3a\u6570\u636e\uff0c\u53ef\u4ee5\u63d0\u53d6\u51fa\u7528\u6237\u7684\u504f\u597d\u548c\u5174\u8da3\uff0c\u4ece\u800c\u4e3a\u63a8\u8350\u7b97\u6cd5\u63d0\u4f9b\u4f9d\u636e\u3002\u4f8b\u5982\uff0c\u53ef\u4ee5\u901a\u8fc7\u5206\u6790\u7528\u6237\u7684\u6d4f\u89c8\u8bb0\u5f55\uff0c\u627e\u5230\u7528\u6237\u6700\u5e38\u6d4f\u89c8\u7684\u5546\u54c1\u7c7b\u522b\uff0c\u4ece\u800c\u5411\u7528\u6237\u63a8\u8350\u66f4\u591a\u76f8\u540c\u7c7b\u522b\u7684\u5546\u54c1\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6211\u4eec\u5df2\u7ecf\u83b7\u53d6\u4e86\u7528\u6237\u7684\u6d4f\u89c8\u8bb0\u5f55\u6570\u636e<\/p>\n<p>data = {<\/p>\n<p>    &#39;user_id&#39;: [1, 1, 2, 2, 3, 3, 3],<\/p>\n<p>    &#39;category&#39;: [&#39;laptop&#39;, &#39;laptop&#39;, &#39;phone&#39;, &#39;phone&#39;, &#39;laptop&#39;, &#39;tablet&#39;, &#39;tablet&#39;],<\/p>\n<p>    &#39;timestamp&#39;: [&#39;2023-01-01 10:00:00&#39;, &#39;2023-01-01 10:05:00&#39;, &#39;2023-01-02 11:00:00&#39;, &#39;2023-01-02 11:10:00&#39;, &#39;2023-01-03 12:00:00&#39;, &#39;2023-01-03 12:15:00&#39;, &#39;2023-01-03 12:20:00&#39;]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u63d0\u53d6\u7528\u6237\u7684\u504f\u597d\u548c\u5174\u8da3<\/strong><\/h2>\n<p>user_preferences = df.groupby([&#39;user_id&#39;, &#39;category&#39;]).size().reset_index(name=&#39;view_count&#39;)<\/p>\n<p>print(user_preferences)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u7528\u6237\u884c\u4e3a\u5206\u6790\uff0c\u6211\u4eec\u53ef\u4ee5\u66f4\u597d\u5730\u7406\u89e3\u7528\u6237\u7684\u9700\u6c42\u548c\u504f\u597d\uff0c\u4ece\u800c\u4e3a\u5176\u63d0\u4f9b\u66f4\u4e2a\u6027\u5316\u7684\u63a8\u8350\u548c\u670d\u52a1\uff0c\u63d0\u9ad8\u8d2d\u4e70\u7684\u6ee1\u610f\u5ea6\u548c\u4f53\u9a8c\u3002<\/p>\n<\/p>\n<p><p>\u603b\u4e4b\uff0cPython\u53ef\u4ee5\u901a\u8fc7\u81ea\u52a8\u5316\u811a\u672c\u3001\u6570\u636e\u5206\u6790\u3001\u51b3\u7b56\u6811\u6a21\u578b\u3001\u63a8\u8350\u7b97\u6cd5\u3001\u7528\u6237\u884c\u4e3a\u5206\u6790\u7b49\u591a\u79cd\u65b9\u6cd5\u6765\u89e3\u51b3\u8d2d\u4e70\u4e1c\u897f\u65b9\u6848\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u5e94\u7528\u573a\u666f\uff0c\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ece\u800c\u5b9e\u73b0\u66f4\u667a\u80fd\u3001\u66f4\u9ad8\u6548\u7684\u8d2d\u4e70\u51b3\u7b56\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6765\u4f18\u5316\u8d2d\u4e70\u65b9\u6848\uff1f<\/strong><br \/>Python\u53ef\u4ee5\u901a\u8fc7\u7f16\u5199\u7b97\u6cd5\u6765\u4f18\u5316\u8d2d\u4e70\u65b9\u6848\uff0c\u6bd4\u5982\u4f7f\u7528\u7ebf\u6027\u89c4\u5212\u6216\u52a8\u6001\u89c4\u5212\u7b49\u65b9\u6cd5\u3002\u5229\u7528\u5e93\u5982NumPy\u548cSciPy\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u6c42\u89e3\u8d44\u6e90\u5206\u914d\u95ee\u9898\uff0c\u5e2e\u52a9\u7528\u6237\u627e\u5230\u6700\u5177\u6027\u4ef7\u6bd4\u7684\u8d2d\u4e70\u65b9\u5f0f\u3002\u6b64\u5916\uff0cpandas\u5e93\u80fd\u591f\u5e2e\u52a9\u7528\u6237\u5904\u7406\u548c\u5206\u6790\u6570\u636e\uff0c\u4ee5\u4fbf\u66f4\u597d\u5730\u505a\u51fa\u8d2d\u4e70\u51b3\u7b56\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u5904\u7406\u5546\u54c1\u4ef7\u683c\u548c\u6570\u91cf\u7684\u6570\u636e\uff1f<\/strong><br \/>\u7528\u6237\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u8f7b\u677e\u5904\u7406\u5546\u54c1\u4ef7\u683c\u548c\u6570\u91cf\u7684\u6570\u636e\u3002\u901a\u8fc7\u521b\u5efaDataFrame\uff0c\u7528\u6237\u53ef\u4ee5\u5c06\u5546\u54c1\u4fe1\u606f\u6574\u5408\u5728\u4e00\u8d77\uff0c\u8fdb\u884c\u6570\u636e\u6e05\u6d17\u548c\u8f6c\u6362\u3002\u501f\u52a9\u6570\u636e\u5206\u6790\u529f\u80fd\uff0c\u7528\u6237\u53ef\u4ee5\u5feb\u901f\u627e\u5230\u4ef7\u683c\u6700\u4f4e\u7684\u5546\u54c1\u6216\u8ba1\u7b97\u603b\u82b1\u8d39\uff0c\u5e2e\u52a9\u505a\u51fa\u66f4\u660e\u667a\u7684\u8d2d\u4e70\u9009\u62e9\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u54ea\u4e9b\u5e93\u9002\u5408\u8fdb\u884c\u8d2d\u4e70\u65b9\u6848\u7684\u6a21\u62df\u548c\u5206\u6790\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u8d2d\u4e70\u65b9\u6848\u7684\u6a21\u62df\u548c\u5206\u6790\u65f6\uff0c\u7528\u6237\u53ef\u4ee5\u4f7f\u7528\u591a\u4e2aPython\u5e93\u3002\u9664\u4e86pandas\u5916\uff0cNumPy\u975e\u5e38\u9002\u5408\u8fdb\u884c\u6570\u503c\u8ba1\u7b97\uff0cMatplotlib\u548cSeaborn\u53ef\u4ee5\u7528\u4e8e\u6570\u636e\u53ef\u89c6\u5316\uff0c\u5e2e\u52a9\u7528\u6237\u66f4\u76f4\u89c2\u5730\u7406\u89e3\u8d2d\u4e70\u65b9\u6848\u7684\u6548\u679c\u3002\u6b64\u5916\uff0cScikit-learn\u53ef\u4ee5\u7528\u4e8e\u673a\u5668\u5b66\u4e60\uff0c\u8fdb\u4e00\u6b65\u63d0\u5347\u5206\u6790\u7684\u6df1\u5ea6\u548c\u51c6\u786e\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5982\u4f55\u89e3\u51b3\u8d2d\u4e70\u4e1c\u897f\u65b9\u6848 Python\u53ef\u4ee5\u901a\u8fc7\u81ea\u52a8\u5316\u811a\u672c\u3001\u6570\u636e\u5206\u6790\u3001\u51b3\u7b56\u6811\u6a21\u578b\u3001\u63a8\u8350\u7b97\u6cd5\u3001\u7528\u6237\u884c\u4e3a\u5206\u6790 [&hellip;]","protected":false},"author":3,"featured_media":1124890,"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\/1124879"}],"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=1124879"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1124879\/revisions"}],"predecessor-version":[{"id":1124895,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1124879\/revisions\/1124895"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1124890"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1124879"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1124879"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1124879"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}