<p>?</p><p/><h1>人工智能之核心技術(shù) 深度學(xué)習(xí)</h1><p/><p>第八章 數(shù)據(jù)預(yù)處理與增強(qiáng)</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/30827302-de8b0d870ef6d08b.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p>
<h1>前言:數(shù)據(jù)預(yù)處理與增強(qiáng) —— 模型成功的基石</h1><blockquote><p><strong>“Garbage in, garbage out.”</strong>
再?gòu)?qiáng)大的模型,若輸入的是臟亂、不平衡或信息貧乏的數(shù)據(jù),也難以發(fā)揮性能。
本章將系統(tǒng)講解如何將原始數(shù)據(jù)轉(zhuǎn)化為<strong>高質(zhì)量、高信息量、適合訓(xùn)練</strong>的格式。</p></blockquote>
<p><strong>一、數(shù)據(jù)預(yù)處理(Data Preprocessing)</strong></p><p>目標(biāo):<strong>清洗 + 轉(zhuǎn)換 + 平衡</strong>,為模型提供“干凈燃料”。</p><p><strong>1.1 缺失值處理</strong></p><p><strong>常見策略:</strong></p><p/><p/><blockquote><p>???<strong>注意</strong>:避免在測(cè)試集上使用訓(xùn)練集以外的統(tǒng)計(jì)量!</p></blockquote>
<p><strong>1.2 異常值檢測(cè)與修正</strong></p><p><strong>檢測(cè)方法:</strong></p><ul><li><p>??<strong>Z-Score</strong>:?視為異常(假設(shè)正態(tài)分布)</p></li><li><p>??<strong>IQR(四分位距)</strong>:</p><p class="image-package"><img class="uploaded-img" src="https://upload-images.jianshu.io/upload_images/30827302-c8c26fcf42fdaa40.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" width="auto" height="auto"/></p></li><li><p>??<strong>孤立森林(Isolation Forest)</strong>:無監(jiān)督異常檢測(cè)</p></li><li><p>??<strong>可視化</strong>:箱線圖、散點(diǎn)圖</p></li></ul><p><strong>處理策略:</strong></p><ul><li><p>??<strong>刪除</strong>:明顯錯(cuò)誤(如年齡=200)</p></li><li><p>??<strong>截?cái)啵╓insorizing)</strong>:將異常值設(shè)為邊界值</p></li><li><p>??<strong>分箱(Binning)</strong>:將連續(xù)值離散化</p></li></ul><pre>#?IQR?示例Q1?=?df['age'].quantile(0.25)Q3?=?df['age'].quantile(0.75)IQR?=?Q3?-?Q1lower_bound?=?Q1?-?1.5??IQRupper_bound?=?Q3?+?1.5??IQRdf?=?df[(df['age']?>=?lower_bound)?&?(df['age']?<=?upper_bound)]</pre>
<p><strong>1.3 數(shù)據(jù)歸一化與標(biāo)準(zhǔn)化</strong></p><p/><p/><blockquote><p>??<strong>深度學(xué)習(xí)推薦</strong>:</p><ul><li><p>? 圖像 →?<strong>歸一化到 [0,1] 或 [-1,1]</strong></p></li><li><p>? 其他數(shù)值特征 →?<strong>StandardScaler(Z-Score)</strong></p></li></ul></blockquote><pre>from?sklearn.preprocessing?import?StandardScaler,?MinMaxScaler#?標(biāo)準(zhǔn)化scaler?=?StandardScaler()X_train_scaled?=?scaler.fit_transform(X_train)X_test_scaled?=?scaler.transform(X_test)??#?注意:只用訓(xùn)練集?fit!#?圖像歸一化(PyTorch)transforms.ToTensor()??#?自動(dòng)將?[0,255]?→?[0,1]</pre>
<p><strong>1.4 類別不平衡問題</strong></p><p>當(dāng)某一類樣本遠(yuǎn)多于其他類(如欺詐檢測(cè)中 99% 正常,1% 欺詐),模型會(huì)偏向多數(shù)類。</p><p><strong>解決方案:</strong></p><pre>類別不平衡重采樣算法層面評(píng)估指標(biāo)過采樣:復(fù)制少數(shù)類欠采樣:刪除多數(shù)類SMOTE:合成新樣本代價(jià)敏感學(xué)習(xí)(class_weight)Focal?Loss不用?Accuracy!用?Precision/Recall/F1/AUC</pre><p><strong>(1)SMOTE(Synthetic Minority Oversampling Technique)</strong></p><ul><li><p>? 在少數(shù)類樣本間<strong>插值生成新樣本</strong></p></li><li><p>? 避免簡(jiǎn)單復(fù)制導(dǎo)致的過擬合</p></li></ul><pre>from?imblearn.over_sampling?import?SMOTEsmote?=?SMOTE(random_state=42)X_res,?y_res?=?smote.fit_resample(X_train,?y_train)</pre><p><strong>(2)代價(jià)敏感學(xué)習(xí)</strong></p><ul><li><p>? 給少數(shù)類更高損失權(quán)重</p></li><li><p>? PyTorch 示例:</p><pre>weights?=?torch.tensor([1.0,?10.0])??#?少數(shù)類權(quán)重=10criterion?=?nn.CrossEntropyLoss(weight=weights)</pre><p/></li></ul><blockquote><p>??<strong>最佳實(shí)踐</strong>:</p><ul><li><p>? 小數(shù)據(jù)集 →?<strong>SMOTE + 過采樣</strong></p></li><li><p>? 大數(shù)據(jù)集 →?<strong>欠采樣 + Focal Loss</strong></p></li></ul></blockquote>
<p><strong>二、數(shù)據(jù)增強(qiáng)(Data Augmentation)</strong></p><p>目標(biāo):<strong>人工擴(kuò)充數(shù)據(jù)集</strong>,提升模型泛化能力與魯棒性。</p><blockquote><p>???<strong>核心思想</strong>:對(duì)輸入做<strong>合理擾動(dòng)</strong>,標(biāo)簽不變!</p></blockquote>
<p><strong>2.1 圖像增強(qiáng)</strong></p><p><strong>常用技術(shù):</strong></p><p/><p/><p><strong>PyTorch 實(shí)現(xiàn)(</strong></p><pre>from?torchvision?import?transformstrain_transform?=?transforms.Compose([????transforms.RandomHorizontalFlip(p=0.5),??????#?隨機(jī)水平翻轉(zhuǎn)????transforms.RandomRotation(degrees=15),???????#?隨機(jī)旋轉(zhuǎn)?±15°????transforms.ColorJitter(brightness=0.2,?contrast=0.2),??#?色彩抖動(dòng)????transforms.RandomResizedCrop(size=224,?scale=(0.8,?1.0)),??#?隨機(jī)裁剪????transforms.ToTensor(),????transforms.Normalize(mean=[0.485,?0.456,?0.406],??????????????????????????std=[0.229,?0.224,?0.225])??#?ImageNet?標(biāo)準(zhǔn)化])#?測(cè)試時(shí)通常只做?Resize?+?Normalizetest_transform?=?transforms.Compose([????transforms.Resize(256),????transforms.CenterCrop(224),????transforms.ToTensor(),????transforms.Normalize(mean=[0.485,?0.456,?0.406],??????????????????????????std=[0.229,?0.224,?0.225])])</pre><p><strong>可視化示例:</strong></p><pre>原始圖像水平翻轉(zhuǎn)旋轉(zhuǎn)?15°隨機(jī)裁剪色彩抖動(dòng)增強(qiáng)后圖像集合</pre><blockquote><p>???<strong>注意</strong>:</p><ul><li><p>??<strong>分類任務(wù)</strong>:可大幅增強(qiáng)</p></li><li><p>??<strong>檢測(cè)/分割</strong>:需同步修改標(biāo)注(如 bounding box)</p></li></ul></blockquote>
<p><strong>2.2 文本增強(qiáng)</strong></p><p>文本是離散符號(hào),增強(qiáng)需保持語義一致性。</p><p><strong>常用方法:</strong></p><p/><p/><p><strong>代碼示例(使用?</strong></p><pre>import?nlpaug.augmenter.word?as?naw#?同義詞替換(基于?WordNet)aug?=?naw.SynonymAug(aug_src='wordnet')text?=?"This?movie?is?great!"augmented_text?=?aug.augment(text)print(augmented_text)??#?e.g.,?"This?film?is?wonderful!"#?回譯(需網(wǎng)絡(luò))#?aug?=?naw.BackTranslationAug(#?????from_model_name='facebook/wmt19-en-de',?#?????to_model_name='facebook/wmt19-de-en'#?)</pre><blockquote><p>???<strong>警告</strong>:</p><ul><li><p>? 避免改變關(guān)鍵實(shí)體(如“不治之癥” → “可治愈疾病”)</p></li><li><p>? 醫(yī)療、法律等敏感領(lǐng)域慎用!</p></li></ul></blockquote>
<p><strong>三、完整流程圖</strong></p><pre>原始數(shù)據(jù)預(yù)處理缺失值處理異常值處理歸一化/標(biāo)準(zhǔn)化類別平衡清洗后數(shù)據(jù)數(shù)據(jù)增強(qiáng)圖像增強(qiáng)文本增強(qiáng)最終數(shù)據(jù)集模型訓(xùn)練</pre>
<p><strong>四、配套實(shí)戰(zhàn)代碼(端到端)</strong></p><p><strong>場(chǎng)景:不平衡圖像分類(貓 vs 稀有動(dòng)物)</strong></p><pre>import?torchfrom?torch.utils.data?import?DataLoader,?WeightedRandomSamplerfrom?torchvision?import?datasets,?transformsfrom?sklearn.utils.class_weight?import?compute_class_weightimport?numpy?as?np#?1.?定義增強(qiáng)train_transform?=?transforms.Compose([????transforms.RandomHorizontalFlip(),????transforms.RandomRotation(10),????transforms.ColorJitter(0.1,?0.1,?0.1),????transforms.Resize((224,?224)),????transforms.ToTensor(),????transforms.Normalize(mean=[0.485,?0.456,?0.406],?std=[0.229,?0.224,?0.225])])#?2.?加載數(shù)據(jù)train_dataset?=?datasets.ImageFolder('data/train',?transform=train_transform)#?3.?處理類別不平衡:計(jì)算采樣權(quán)重labels?=?[sample[1]?for?sample?in?train_dataset.samples]class_weights?=?compute_class_weight('balanced',?classes=np.unique(labels),?y=labels)sample_weights?=?[class_weights[label]?for?label?in?labels]#?4.?使用?WeightedRandomSamplersampler?=?WeightedRandomSampler(sample_weights,?len(sample_weights))train_loader?=?DataLoader(train_dataset,?batch_size=32,?sampler=sampler)#?5.?模型訓(xùn)練(略)</pre><blockquote><p>??<strong>優(yōu)勢(shì)</strong>:</p><ul><li><p>? 增強(qiáng)提升泛化</p></li><li><p>? 加權(quán)采樣解決不平衡</p></li><li><p>? 標(biāo)準(zhǔn)化適配預(yù)訓(xùn)練模型</p></li></ul></blockquote>
<p><strong>五、總結(jié)與最佳實(shí)踐</strong></p><p/><p/><blockquote><p>???<strong>黃金法則</strong>:</p><ol><li><p>1.?<strong>先分析,再處理</strong>:可視化缺失、分布、類別比例</p></li><li><p>2.?<strong>驗(yàn)證集不增強(qiáng)</strong>:只對(duì)訓(xùn)練集做增強(qiáng)</p></li><li><p>3.?<strong>保持語義</strong>:增強(qiáng)不能改變標(biāo)簽含義</p></li><li><p>4.?<strong>記錄 pipeline</strong>:確保可復(fù)現(xiàn)</p></li></ol></blockquote>
<h1>資料關(guān)注</h1><p>公眾號(hào):咚咚王
gitee:https://gitee.com/wy18585051844/ai_learning</p><p/><p>《Python編程:從入門到實(shí)踐》
《利用Python進(jìn)行數(shù)據(jù)分析》
《算法導(dǎo)論中文第三版》
《概率論與數(shù)理統(tǒng)計(jì)(第四版) (盛驟) 》
《程序員的數(shù)學(xué)》
《線性代數(shù)應(yīng)該這樣學(xué)第3版》
《微積分和數(shù)學(xué)分析引論》
《(西瓜書)周志華-機(jī)器學(xué)習(xí)》
《TensorFlow機(jī)器學(xué)習(xí)實(shí)戰(zhàn)指南》
《Sklearn與TensorFlow機(jī)器學(xué)習(xí)實(shí)用指南》
《模式識(shí)別(第四版)》
《深度學(xué)習(xí) deep learning》伊恩·古德費(fèi)洛著 花書
《Python深度學(xué)習(xí)第二版(中文版)【純文本】 (登封大數(shù)據(jù) (Francois Choliet)) (Z-Library)》
《深入淺出神經(jīng)網(wǎng)絡(luò)與深度學(xué)習(xí)+(邁克爾·尼爾森(Michael+Nielsen)》
《自然語言處理綜論 第2版》
《Natural-Language-Processing-with-PyTorch》
《計(jì)算機(jī)視覺-算法與應(yīng)用(中文版)》
《Learning OpenCV 4》
《AIGC:智能創(chuàng)作時(shí)代》杜雨+&+張孜銘
《AIGC原理與實(shí)踐:零基礎(chǔ)學(xué)大語言模型、擴(kuò)散模型和多模態(tài)模型》
《從零構(gòu)建大語言模型(中文版)》
《實(shí)戰(zhàn)AI大模型》
《AI 3.0》</p><p>?</p><p/><p/><p/><p/><p/>