{"id":1182961,"date":"2025-01-15T19:09:15","date_gmt":"2025-01-15T11:09:15","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182961.html"},"modified":"2025-01-15T19:09:15","modified_gmt":"2025-01-15T11:09:15","slug":"python%e5%a6%82%e4%bd%95%e4%bf%ae%e6%94%b9%e8%be%93%e5%85%a5%e5%b1%82%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182961.html","title":{"rendered":"python\u5982\u4f55\u4fee\u6539\u8f93\u5165\u5c42\u6570\u636e"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25132851\/3d375971-81ea-4cfe-bb32-00493283e987.webp\" alt=\"python\u5982\u4f55\u4fee\u6539\u8f93\u5165\u5c42\u6570\u636e\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u4fee\u6539\u8f93\u5165\u5c42\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\uff0c\u5305\u62ec\u91cd\u65b0\u5b9a\u4e49\u8f93\u5165\u6570\u636e\u3001\u5bf9\u8f93\u5165\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3001\u4f7f\u7528\u6570\u636e\u589e\u5f3a\u6280\u672f\u7b49\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\uff1a<\/strong> \u91cd\u5b9a\u4e49\u8f93\u5165\u6570\u636e\u3001\u5bf9\u8f93\u5165\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3001\u4f7f\u7528\u6570\u636e\u589e\u5f3a\u6280\u672f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u91cd\u65b0\u5b9a\u4e49\u8f93\u5165\u6570\u636e<\/h3>\n<\/p>\n<p><p>\u91cd\u65b0\u5b9a\u4e49\u8f93\u5165\u6570\u636e\u662f\u6700\u76f4\u63a5\u7684\u65b9\u5f0f\uff0c\u9002\u7528\u4e8e\u4f60\u5df2\u7ecf\u660e\u786e\u77e5\u9053\u9700\u8981\u4fee\u6539\u7684\u6570\u636e\u5185\u5bb9\u548c\u683c\u5f0f\u3002\u53ef\u4ee5\u901a\u8fc7\u4ee5\u4e0b\u6b65\u9aa4\u8fdb\u884c\u91cd\u65b0\u5b9a\u4e49\uff1a<\/p>\n<\/p>\n<p><h4>1.1\u3001\u8bfb\u53d6\u5e76\u4fee\u6539\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u8bfb\u53d6\u539f\u59cb\u6570\u636e\uff0c\u7136\u540e\u6839\u636e\u9700\u8981\u8fdb\u884c\u4fee\u6539\u3002\u6bd4\u5982\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u4fee\u6539\u6570\u636e\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u4fee\u6539\u67d0\u4e00\u5217\u6570\u636e<\/strong><\/h2>\n<p>data[&#39;column_name&#39;] = data[&#39;column_name&#39;].apply(lambda x: x + 1)  # \u5047\u8bbe\u9700\u8981\u5bf9\u67d0\u5217\u6570\u636e\u52a01<\/p>\n<h2><strong>\u4fdd\u5b58\u4fee\u6539\u540e\u7684\u6570\u636e<\/strong><\/h2>\n<p>data.to_csv(&#39;modified_data.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2\u3001\u91cd\u65b0\u5b9a\u4e49\u8f93\u5165\u6570\u636e\u683c\u5f0f<\/h4>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u4fee\u6539\u8f93\u5165\u6570\u636e\u7684\u683c\u5f0f\uff0c\u53ef\u4ee5\u4f7f\u7528Numpy\u5e93\u8fdb\u884c\u64cd\u4f5c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<h2><strong>\u5047\u8bbe\u539f\u59cb\u6570\u636e\u4e3a\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u4fee\u6539\u6570\u636e\u683c\u5f0f\uff0c\u6bd4\u5982\u5c06\u5176\u8f6c\u6362\u4e3a\u4e09\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>modified_data = data.reshape((2, 3, 1))<\/p>\n<p>print(modified_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u5bf9\u8f93\u5165\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u8bb8\u591a<a href=\"https:\/\/docs.pingcode.com\/ask\/59192.html\" target=\"_blank\">\u673a\u5668\u5b66\u4e60<\/a>\u548c\u6df1\u5ea6\u5b66\u4e60\u4efb\u52a1\u4e2d\uff0c\u6570\u636e\u9884\u5904\u7406\u662f\u4e00\u4e2a\u91cd\u8981\u6b65\u9aa4\u3002\u9884\u5904\u7406\u53ef\u4ee5\u5305\u62ec\u6570\u636e\u6807\u51c6\u5316\u3001\u5f52\u4e00\u5316\u3001\u7f3a\u5931\u503c\u5904\u7406\u7b49\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u6570\u636e\u6807\u51c6\u5316\u548c\u5f52\u4e00\u5316<\/h4>\n<\/p>\n<p><p>\u6807\u51c6\u5316\u548c\u5f52\u4e00\u5316\u662f\u5e38\u89c1\u7684\u9884\u5904\u7406\u6b65\u9aa4\uff0c\u53ef\u4ee5\u4f7f\u7528Sklearn\u5e93\u4e2d\u7684\u5de5\u5177\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import StandardScaler, MinMaxScaler<\/p>\n<h2><strong>\u5047\u8bbe\u6570\u636e\u4e3a\u4e00\u4e2a\u4e8c\u7ef4\u6570\u7ec4<\/strong><\/h2>\n<p>data = np.array([[1, 2, 3], [4, 5, 6]])<\/p>\n<h2><strong>\u6807\u51c6\u5316\u6570\u636e<\/strong><\/h2>\n<p>scaler = StandardScaler()<\/p>\n<p>standardized_data = scaler.fit_transform(data)<\/p>\n<h2><strong>\u5f52\u4e00\u5316\u6570\u636e<\/strong><\/h2>\n<p>scaler = MinMaxScaler()<\/p>\n<p>normalized_data = scaler.fit_transform(data)<\/p>\n<p>print(&quot;Standardized Data:\\n&quot;, standardized_data)<\/p>\n<p>print(&quot;Normalized Data:\\n&quot;, normalized_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2\u3001\u7f3a\u5931\u503c\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u7f3a\u5931\u503c\u5904\u7406\u662f\u9884\u5904\u7406\u4e2d\u4e0d\u53ef\u5ffd\u89c6\u7684\u4e00\u90e8\u5206\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6CSV\u6587\u4ef6<\/p>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>data.fillna(data.mean(), inplace=True)  # \u4f7f\u7528\u5747\u503c\u586b\u5145\u7f3a\u5931\u503c<\/p>\n<h2><strong>\u5220\u9664\u542b\u6709\u7f3a\u5931\u503c\u7684\u884c<\/strong><\/h2>\n<p>data.dropna(inplace=True)<\/p>\n<p>print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u6570\u636e\u589e\u5f3a\u6280\u672f<\/h3>\n<\/p>\n<p><p>\u6570\u636e\u589e\u5f3a\u662f\u901a\u8fc7\u5bf9\u539f\u59cb\u6570\u636e\u8fdb\u884c\u5404\u79cd\u53d8\u6362\u6765\u751f\u6210\u65b0\u7684\u8bad\u7ec3\u6837\u672c\uff0c\u4ece\u800c\u63d0\u9ad8\u6a21\u578b\u7684\u6cdb\u5316\u80fd\u529b\u3002\u6570\u636e\u589e\u5f3a\u6280\u672f\u5728\u56fe\u50cf\u5904\u7406\u9886\u57df\u5c24\u4e3a\u5e38\u7528\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001\u56fe\u50cf\u6570\u636e\u589e\u5f3a<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528Keras\u5e93\u4e2d\u7684ImageDataGenerator\u8fdb\u884c\u56fe\u50cf\u6570\u636e\u589e\u5f3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aImageDataGenerator\u5bf9\u8c61<\/strong><\/h2>\n<p>datagen = ImageDataGenerator(<\/p>\n<p>    rotation_range=40,<\/p>\n<p>    width_shift_range=0.2,<\/p>\n<p>    height_shift_range=0.2,<\/p>\n<p>    shear_range=0.2,<\/p>\n<p>    zoom_range=0.2,<\/p>\n<p>    horizontal_flip=True,<\/p>\n<p>    fill_mode=&#39;nearest&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u5e76\u8f6c\u6362\u4e3a\u6570\u7ec4<\/strong><\/h2>\n<p>img = load_img(&#39;image.jpg&#39;)  # \u8fd9\u662f\u4e00\u4e2aPIL\u56fe\u50cf\u5bf9\u8c61<\/p>\n<p>x = img_to_array(img)  # \u8fd9\u662f\u4e00\u4e2aNumPy\u6570\u7ec4\uff0c\u5f62\u72b6\u4e3a(3, 150, 150)<\/p>\n<p>x = x.reshape((1,) + x.shape)  # \u8fd9\u662f\u4e00\u4e2a\u5f62\u72b6\u4e3a(1, 3, 150, 150)\u7684NumPy\u6570\u7ec4<\/p>\n<h2><strong>\u4f7f\u7528.flow()\u65b9\u6cd5\u751f\u6210\u589e\u5f3a\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>i = 0<\/p>\n<p>for batch in datagen.flow(x, batch_size=1, save_to_dir=&#39;preview&#39;, save_prefix=&#39;cat&#39;, save_format=&#39;jpeg&#39;):<\/p>\n<p>    i += 1<\/p>\n<p>    if i &gt; 20:<\/p>\n<p>        break  # \u751f\u621020\u5f20\u589e\u5f3a\u540e\u7684\u56fe\u50cf<\/p>\n<p>print(&quot;Data augmentation completed.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3.2\u3001\u6587\u672c\u6570\u636e\u589e\u5f3a<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6587\u672c\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528NLTK\u5e93\u8fdb\u884c\u6570\u636e\u589e\u5f3a\uff0c\u4f8b\u5982\u540c\u4e49\u8bcd\u66ff\u6362\u3001\u968f\u673a\u63d2\u5165\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import nltk<\/p>\n<p>from nltk.corpus import wordnet<\/p>\n<p>nltk.download(&#39;wordnet&#39;)<\/p>\n<p>def synonym_replacement(sentence):<\/p>\n<p>    words = sentence.split()<\/p>\n<p>    new_sentence = []<\/p>\n<p>    for word in words:<\/p>\n<p>        synonyms = wordnet.synsets(word)<\/p>\n<p>        if synonyms:<\/p>\n<p>            synonym = synonyms[0].lemmas()[0].name()<\/p>\n<p>            new_sentence.append(synonym)<\/p>\n<p>        else:<\/p>\n<p>            new_sentence.append(word)<\/p>\n<p>    return &#39; &#39;.join(new_sentence)<\/p>\n<p>sentence = &quot;This is a sample sentence for data augmentation.&quot;<\/p>\n<p>augmented_sentence = synonym_replacement(sentence)<\/p>\n<p>print(&quot;Original Sentence:&quot;, sentence)<\/p>\n<p>print(&quot;Augmented Sentence:&quot;, augmented_sentence)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5176\u4ed6\u6570\u636e\u4fee\u6539\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u8fd8\u6709\u5176\u4ed6\u4e00\u4e9b\u4fee\u6539\u8f93\u5165\u6570\u636e\u7684\u65b9\u5f0f\uff0c\u5982\u7279\u5f81\u9009\u62e9\u3001\u7279\u5f81\u63d0\u53d6\u7b49\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u7279\u5f81\u9009\u62e9<\/h4>\n<\/p>\n<p><p>\u7279\u5f81\u9009\u62e9\u662f\u901a\u8fc7\u9009\u62e9\u91cd\u8981\u7684\u7279\u5f81\u6765\u51cf\u5c11\u6570\u636e\u7ef4\u5ea6\uff0c\u53ef\u4ee5\u4f7f\u7528Sklearn\u5e93\u4e2d\u7684\u5de5\u5177\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.feature_selection import SelectKBest, chi2<\/p>\n<h2><strong>\u5047\u8bbe\u6570\u636e\u548c\u6807\u7b7e<\/strong><\/h2>\n<p>X = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<p>y = np.array([1, 0, 1])<\/p>\n<h2><strong>\u9009\u62e9\u6700\u91cd\u8981\u7684\u4e24\u4e2a\u7279\u5f81<\/strong><\/h2>\n<p>selector = SelectKBest(chi2, k=2)<\/p>\n<p>X_new = selector.fit_transform(X, y)<\/p>\n<p>print(&quot;Selected Features:\\n&quot;, X_new)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2\u3001\u7279\u5f81\u63d0\u53d6<\/h4>\n<\/p>\n<p><p>\u7279\u5f81\u63d0\u53d6\u662f\u901a\u8fc7\u63d0\u53d6\u6570\u636e\u4e2d\u7684\u6709\u7528\u4fe1\u606f\u6765\u8fdb\u884c\u6570\u636e\u4fee\u6539\uff0c\u53ef\u4ee5\u4f7f\u7528Sklearn\u5e93\u4e2d\u7684\u5de5\u5177\u8fdb\u884c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.decomposition import PCA<\/p>\n<h2><strong>\u5047\u8bbe\u6570\u636e<\/strong><\/h2>\n<p>data = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])<\/p>\n<h2><strong>\u4f7f\u7528PCA\u63d0\u53d6\u7279\u5f81<\/strong><\/h2>\n<p>pca = PCA(n_components=2)<\/p>\n<p>pca_data = pca.fit_transform(data)<\/p>\n<p>print(&quot;PCA Extracted Features:\\n&quot;, pca_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u7efc\u5408\u5e94\u7528<\/h3>\n<\/p>\n<p><p>\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\uff0c\u5e38\u5e38\u9700\u8981\u7efc\u5408\u5e94\u7528\u591a\u79cd\u65b9\u6cd5\u6765\u4fee\u6539\u8f93\u5165\u6570\u636e\uff0c\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u6027\u80fd\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u7efc\u5408\u5e94\u7528\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>5.1\u3001\u8bfb\u53d6\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u8bfb\u53d6\u6570\u636e\u5e76\u8fdb\u884c\u521d\u6b65\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>data = pd.read_csv(&#39;data.csv&#39;)<\/p>\n<h2><strong>\u67e5\u770b\u6570\u636e\u6982\u51b5<\/strong><\/h2>\n<p>print(data.describe())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2\u3001\u6570\u636e\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u8fdb\u884c\u6570\u636e\u9884\u5904\u7406\uff0c\u5305\u62ec\u7f3a\u5931\u503c\u5904\u7406\u3001\u6807\u51c6\u5316\u7b49\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.preprocessing import StandardScaler<\/p>\n<h2><strong>\u586b\u5145\u7f3a\u5931\u503c<\/strong><\/h2>\n<p>data.fillna(data.mean(), inplace=True)<\/p>\n<h2><strong>\u6807\u51c6\u5316\u6570\u636e<\/strong><\/h2>\n<p>scaler = StandardScaler()<\/p>\n<p>scaled_data = scaler.fit_transform(data)<\/p>\n<p>print(&quot;Preprocessed Data:\\n&quot;, scaled_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.3\u3001\u7279\u5f81\u9009\u62e9\u4e0e\u63d0\u53d6<\/h4>\n<\/p>\n<p><p>\u9009\u62e9\u91cd\u8981\u7279\u5f81\u5e76\u63d0\u53d6\u6709\u7528\u4fe1\u606f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from sklearn.feature_selection import SelectKBest, chi2<\/p>\n<p>from sklearn.decomposition import PCA<\/p>\n<h2><strong>\u9009\u62e9\u6700\u91cd\u8981\u7684\u4e24\u4e2a\u7279\u5f81<\/strong><\/h2>\n<p>selector = SelectKBest(chi2, k=2)<\/p>\n<p>selected_data = selector.fit_transform(scaled_data, data[&#39;target&#39;])<\/p>\n<h2><strong>\u4f7f\u7528PCA\u63d0\u53d6\u7279\u5f81<\/strong><\/h2>\n<p>pca = PCA(n_components=2)<\/p>\n<p>pca_data = pca.fit_transform(selected_data)<\/p>\n<p>print(&quot;Final Features:\\n&quot;, pca_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.4\u3001\u6570\u636e\u589e\u5f3a<\/h4>\n<\/p>\n<p><p>\u5bf9\u6570\u636e\u8fdb\u884c\u589e\u5f3a\uff0c\u751f\u6210\u66f4\u591a\u6837\u672c\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5047\u8bbe\u6709\u4e00\u4e2a\u51fd\u6570data_augmentation\uff0c\u7528\u4e8e\u6570\u636e\u589e\u5f3a<\/p>\n<p>def data_augmentation(data):<\/p>\n<p>    augmented_data = []<\/p>\n<p>    for sample in data:<\/p>\n<p>        augmented_sample = sample * np.random.uniform(0.9, 1.1)  # \u7b80\u5355\u7684\u4e58\u6cd5\u589e\u5f3a<\/p>\n<p>        augmented_data.append(augmented_sample)<\/p>\n<p>    return np.array(augmented_data)<\/p>\n<p>augmented_data = data_augmentation(pca_data)<\/p>\n<p>print(&quot;Augmented Data:\\n&quot;, augmented_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5b9e\u4f8b\u5206\u6790<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u597d\u5730\u7406\u89e3\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u4e0b\u9762\u5c06\u4ee5\u4e00\u4e2a\u5b9e\u9645\u9879\u76ee\u4e3a\u4f8b\uff0c\u8be6\u7ec6\u5206\u6790\u5982\u4f55\u4fee\u6539\u8f93\u5165\u5c42\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>6.1\u3001\u9879\u76ee\u80cc\u666f<\/h4>\n<\/p>\n<p><p>\u5047\u8bbe\u6211\u4eec\u6709\u4e00\u4e2a\u56fe\u50cf\u5206\u7c7b\u9879\u76ee\uff0c\u9700\u8981\u5bf9\u8f93\u5165\u56fe\u50cf\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u548c\u589e\u5f3a\uff0c\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u51c6\u786e\u6027\u3002<\/p>\n<\/p>\n<p><h4>6.2\u3001\u8bfb\u53d6\u6570\u636e<\/h4>\n<\/p>\n<p><p>\u9996\u5148\u8bfb\u53d6\u56fe\u50cf\u6570\u636e\uff0c\u5e76\u8fdb\u884c\u521d\u6b65\u5904\u7406\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from keras.preprocessing.image import load_img, img_to_array<\/p>\n<h2><strong>\u8bfb\u53d6\u56fe\u50cf\u6587\u4ef6<\/strong><\/h2>\n<p>image_folder = &#39;images&#39;<\/p>\n<p>image_files = [os.path.join(image_folder, f) for f in os.listdir(image_folder) if f.endswith(&#39;.jpg&#39;)]<\/p>\n<h2><strong>\u8f6c\u6362\u56fe\u50cf\u4e3a\u6570\u7ec4<\/strong><\/h2>\n<p>images = [img_to_array(load_img(img_file)) for img_file in image_files]<\/p>\n<p>print(&quot;Loaded Images:&quot;, len(images))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.3\u3001\u6570\u636e\u9884\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u8fdb\u884c\u56fe\u50cf\u6570\u636e\u7684\u9884\u5904\u7406\uff0c\u5305\u62ec\u5f52\u4e00\u5316\u548c\u6807\u51c6\u5316\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">images = np.array(images)<\/p>\n<h2><strong>\u5f52\u4e00\u5316<\/strong><\/h2>\n<p>images = images \/ 255.0<\/p>\n<h2><strong>\u6807\u51c6\u5316<\/strong><\/h2>\n<p>mean = np.mean(images, axis=0)<\/p>\n<p>std = np.std(images, axis=0)<\/p>\n<p>images = (images - mean) \/ std<\/p>\n<p>print(&quot;Preprocessed Images:\\n&quot;, images)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.4\u3001\u6570\u636e\u589e\u5f3a<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Keras\u7684ImageDataGenerator\u8fdb\u884c\u56fe\u50cf\u6570\u636e\u589e\u5f3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from keras.preprocessing.image import ImageDataGenerator<\/p>\n<h2><strong>\u521b\u5efaImageDataGenerator\u5bf9\u8c61<\/strong><\/h2>\n<p>datagen = ImageDataGenerator(<\/p>\n<p>    rotation_range=40,<\/p>\n<p>    width_shift_range=0.2,<\/p>\n<p>    height_shift_range=0.2,<\/p>\n<p>    shear_range=0.2,<\/p>\n<p>    zoom_range=0.2,<\/p>\n<p>    horizontal_flip=True,<\/p>\n<p>    fill_mode=&#39;nearest&#39;<\/p>\n<p>)<\/p>\n<h2><strong>\u751f\u6210\u589e\u5f3a\u540e\u7684\u56fe\u50cf<\/strong><\/h2>\n<p>augmented_images = []<\/p>\n<p>for img in images:<\/p>\n<p>    img = img.reshape((1,) + img.shape)<\/p>\n<p>    for batch in datagen.flow(img, batch_size=1):<\/p>\n<p>        augmented_images.append(batch[0])<\/p>\n<p>        if len(augmented_images) &gt;= len(images) * 2:  # \u751f\u6210\u4e24\u500d\u6570\u91cf\u7684\u56fe\u50cf<\/p>\n<p>            break<\/p>\n<p>print(&quot;Augmented Images:&quot;, len(augmented_images))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>6.5\u3001\u6a21\u578b\u8bad\u7ec3<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528\u5904\u7406\u548c\u589e\u5f3a\u540e\u7684\u6570\u636e\u8fdb\u884c\u6a21\u578b\u8bad\u7ec3\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from keras.models import Sequential<\/p>\n<p>from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense<\/p>\n<h2><strong>\u6784\u5efa\u7b80\u5355\u7684\u5377\u79ef\u795e\u7ecf\u7f51\u7edc\u6a21\u578b<\/strong><\/h2>\n<p>model = Sequential([<\/p>\n<p>    Conv2D(32, (3, 3), activation=&#39;relu&#39;, input_shape=(150, 150, 3)),<\/p>\n<p>    MaxPooling2D((2, 2)),<\/p>\n<p>    Conv2D(64, (3, 3), activation=&#39;relu&#39;),<\/p>\n<p>    MaxPooling2D((2, 2)),<\/p>\n<p>    Flatten(),<\/p>\n<p>    Dense(64, activation=&#39;relu&#39;),<\/p>\n<p>    Dense(10, activation=&#39;softmax&#39;)<\/p>\n<p>])<\/p>\n<h2><strong>\u7f16\u8bd1\u6a21\u578b<\/strong><\/h2>\n<p>model.compile(optimizer=&#39;adam&#39;, loss=&#39;categorical_crossentropy&#39;, metrics=[&#39;accuracy&#39;])<\/p>\n<h2><strong>\u5047\u8bbe\u6709\u6807\u7b7e\u6570\u636ey<\/strong><\/h2>\n<p>y = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])  # \u793a\u4f8b\u6807\u7b7e<\/p>\n<h2><strong>\u8bad\u7ec3\u6a21\u578b<\/strong><\/h2>\n<p>model.fit(np.array(augmented_images), y, epochs=10)<\/p>\n<p>print(&quot;Model tr<a href=\"https:\/\/docs.pingcode.com\/blog\/59162.html\" target=\"_blank\">AI<\/a>ning completed.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u6b65\u9aa4\uff0c\u6211\u4eec\u8be6\u7ec6\u4ecb\u7ecd\u4e86\u5982\u4f55\u5728Python\u4e2d\u4fee\u6539\u8f93\u5165\u5c42\u6570\u636e\uff0c\u5e76\u7ed3\u5408\u5b9e\u9645\u9879\u76ee\u8fdb\u884c\u4e86\u5b9e\u4f8b\u5206\u6790\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u80fd\u5e2e\u52a9\u4f60\u66f4\u597d\u5730\u7406\u89e3\u548c\u5e94\u7528\u8fd9\u4e9b\u6280\u672f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u5e76\u4fee\u6539\u8f93\u5165\u5c42\u6570\u636e\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528NumPy\u6216Pandas\u5e93\u6765\u8bfb\u53d6\u548c\u4fee\u6539\u6570\u636e\u3002NumPy\u9002\u5408\u5904\u7406\u6570\u503c\u578b\u6570\u636e\uff0c\u800cPandas\u5219\u66f4\u9002\u5408\u5904\u7406\u8868\u683c\u578b\u6570\u636e\u3002\u901a\u8fc7\u8fd9\u4e9b\u5e93\uff0c\u7528\u6237\u53ef\u4ee5\u8f7b\u677e\u5730\u52a0\u8f7d\u6570\u636e\u96c6\uff0c\u5229\u7528\u7d22\u5f15\u548c\u5207\u7247\u529f\u80fd\u6765\u4fee\u6539\u8f93\u5165\u5c42\u7684\u6570\u636e\u3002\u8fd9\u79cd\u65b9\u5f0f\u4f7f\u5f97\u6570\u636e\u6e05\u6d17\u548c\u5904\u7406\u53d8\u5f97\u66f4\u52a0\u9ad8\u6548\u3002<\/p>\n<p><strong>\u4fee\u6539\u8f93\u5165\u5c42\u6570\u636e\u5bf9\u6a21\u578b\u8bad\u7ec3\u6709\u4f55\u5f71\u54cd\uff1f<\/strong><br \/>\u8f93\u5165\u5c42\u6570\u636e\u7684\u4fee\u6539\u80fd\u591f\u76f4\u63a5\u5f71\u54cd\u6a21\u578b\u7684\u8bad\u7ec3\u6548\u679c\u3002\u6e05\u6d17\u548c\u9884\u5904\u7406\u8f93\u5165\u6570\u636e\uff0c\u786e\u4fdd\u5176\u8d28\u91cf\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6a21\u578b\u7684\u51c6\u786e\u6027\u548c\u6cdb\u5316\u80fd\u529b\u3002\u82e5\u6570\u636e\u4e2d\u5305\u542b\u5f02\u5e38\u503c\u6216\u7f3a\u5931\u503c\uff0c\u53ca\u65f6\u5904\u7406\u8fd9\u4e9b\u95ee\u9898\u5c06\u6709\u52a9\u4e8e\u63d0\u5347\u6a21\u578b\u7684\u8868\u73b0\u3002<\/p>\n<p><strong>\u6709\u54ea\u4e9b\u5e38\u89c1\u7684\u65b9\u6cd5\u53ef\u4ee5\u7528\u6765\u4fee\u6539\u8f93\u5165\u5c42\u6570\u636e\uff1f<\/strong><br \/>\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u6807\u51c6\u5316\u548c\u5f52\u4e00\u5316\uff0c\u65e8\u5728\u8c03\u6574\u6570\u636e\u7684\u8303\u56f4\u548c\u5206\u5e03\u3002\u6b64\u5916\uff0c\u586b\u5145\u7f3a\u5931\u503c\u3001\u53bb\u9664\u5f02\u5e38\u503c\u3001\u8fdb\u884c\u7279\u5f81\u9009\u62e9\u7b49\u4e5f\u662f\u5e38\u7528\u7684\u4fee\u6539\u65b9\u6cd5\u3002\u7528\u6237\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u7684\u9700\u6c42\u9009\u62e9\u6700\u9002\u5408\u7684\u6570\u636e\u5904\u7406\u65b9\u5f0f\uff0c\u4ee5\u4f18\u5316\u8f93\u5165\u5c42\u7684\u6570\u636e\u8d28\u91cf\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u4fee\u6539\u8f93\u5165\u5c42\u6570\u636e\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u6765\u5b9e\u73b0\uff0c\u5305\u62ec\u91cd\u65b0\u5b9a\u4e49\u8f93\u5165\u6570\u636e\u3001\u5bf9\u8f93\u5165\u6570\u636e\u8fdb\u884c\u9884\u5904\u7406\u3001\u4f7f\u7528\u6570\u636e\u589e [&hellip;]","protected":false},"author":3,"featured_media":0,"comment_status":"","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182961"}],"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=1182961"}],"version-history":[{"count":0,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182961\/revisions"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1182961"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1182961"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1182961"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}