<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          機(jī)器學(xué)習(xí)中必知必會(huì)的 3 種特征選取方法!

          共 4818字,需瀏覽 10分鐘

           ·

          2021-03-14 14:08

          隨著深度學(xué)習(xí)的蓬勃發(fā)展,越來越多的小伙伴開始嘗試搭建深層神經(jīng)網(wǎng)絡(luò)應(yīng)用于工作場(chǎng)景中,認(rèn)為只需要把數(shù)據(jù)放入模型中,調(diào)優(yōu)模型參數(shù)就可以讓模型利用自身機(jī)制來選擇重要特征,輸出較好的數(shù)據(jù)結(jié)果。

          在現(xiàn)實(shí)工作場(chǎng)景中,受限制數(shù)據(jù)和時(shí)間,這樣的做法其實(shí)并不可取,一方面大量數(shù)據(jù)輸入將導(dǎo)致模型訓(xùn)練周期增長(zhǎng),另一方面在當(dāng)前細(xì)分市場(chǎng)中,并非所有場(chǎng)景都有海量數(shù)據(jù),尋找海量數(shù)據(jù)中的重要特征迫在眉睫。

          本文我將教你三個(gè)選擇特征的方法,這是任何想從事數(shù)據(jù)科學(xué)領(lǐng)域的都應(yīng)該知道。本文的結(jié)構(gòu)如下:

          • 數(shù)據(jù)集加載和準(zhǔn)備
          • 方法1:從系數(shù)獲取特征重要性
          • 方法2:從樹模型獲取特征重要性
          • 方法3:從 PCA 分?jǐn)?shù)中獲取特征重要性
          • 結(jié)論

          數(shù)據(jù)集加載和準(zhǔn)備

          為了方便介紹,我這里使用"load_breast_cancer"數(shù)據(jù)集,該數(shù)據(jù)內(nèi)置于 Scikit-Learn 中。

          以下代碼段演示如何導(dǎo)入庫(kù)和加載數(shù)據(jù)集:

          import numpy as np
          import pandas as pd
          from sklearn.datasets import load_breast_cancer
          import matplotlib.pyplot as plt
          from matplotlib import rcParams
          rcParams['figure.figsize'] = 147
          rcParams['axes.spines.top'] = False
          rcParams['axes.spines.right'] = False
          # Load data
          data = load_breast_cancer()

          調(diào)用以下代碼,輸出結(jié)果。

          df = pd.concat([pd.DataFrame(data.data, columns=data.feature_names),pd.DataFrame(data.target, columns=['y'])], axis=1)
          df.head()

          上述數(shù)據(jù)中有 30 個(gè)特征變量和一個(gè)目標(biāo)變量。所有值都是數(shù)值,并且沒有缺失的值。在解決縮放問題之前,還需要執(zhí)行訓(xùn)練、測(cè)試拆分。

          from sklearn.preprocessing import StandardScaler
          from sklearn.model_selection import train_test_split
          X = df.drop('y', axis=1)
          y = df['y']
          X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.25, random_state=42)
          ss = StandardScaler()
          X_train_scaled = ss.fit_transform(X_train)
          X_test_scaled = ss.transform(X_test)

          方法1:從系數(shù)獲取特征重要性

          檢查特征重要性的最簡(jiǎn)單方法是檢查模型的系數(shù)。例如,線性回歸和邏輯回歸都?xì)w結(jié)為一個(gè)方程,其中將系數(shù)(重要性)分配給每個(gè)輸入值。

          簡(jiǎn)單地說,如果分配的系數(shù)是一個(gè)大(負(fù)或正)數(shù)字,它會(huì)對(duì)預(yù)測(cè)產(chǎn)生一些影響。相反,如果系數(shù)為零,則對(duì)預(yù)測(cè)沒有任何影響。

          邏輯非常簡(jiǎn)單,讓我們來測(cè)試一下,邏輯回歸是一種合適的算法。擬合模型后,系數(shù)將存儲(chǔ)在屬性中coef_。

          from sklearn.linear_model import LogisticRegression

          model = LogisticRegression()
          model.fit(X_train_scaled, y_train)
          importances = pd.DataFrame(data={
              'Attribute': X_train.columns,
              'Importance': model.coef_[0]
          })
          importances = importances.sort_values(by='Importance', ascending=False)
          # 可視化
          plt.bar(x=importances['Attribute'], height=importances['Importance'], color='#087E8B')
          plt.title('Feature importances obtained from coefficients', size=20)
          plt.xticks(rotation='vertical')
          plt.show()

          下面是相應(yīng)的可視化效果:該方法最大特點(diǎn):「簡(jiǎn)單」、「高效」。系數(shù)越大(在正方向和負(fù)方向),越影響預(yù)測(cè)效果。

          方法2:從樹模型獲取重要性

          訓(xùn)練任何樹模型后,你都可以訪問 feature_importances 屬性。這是獲取功特征重要性的最快方法之一。

          以下代碼演示如何導(dǎo)入模型并在訓(xùn)練數(shù)據(jù)上擬合模型,以及重要性的獲取:

          from xgboost import XGBClassifier

          model = XGBClassifier()
          model.fit(X_train_scaled, y_train)
          importances = pd.DataFrame(data={
              'Attribute': X_train.columns,
              'Importance': model.feature_importances_
          })
          importances = importances.sort_values(by='Importance', ascending=False)
          # 可視化
          plt.bar(x=importances['Attribute'], height=importances['Importance'], color='#087E8B')
          plt.title('Feature importances obtained from coefficients', size=20)
          plt.xticks(rotation='vertical')
          plt.show()

          相應(yīng)的可視化效果如下所示:

          方法3:從 PCA 分?jǐn)?shù)獲取特征重要性

          主成分分析(PCA)是一種出色的降維技術(shù),也可用于確定特征的重要性。

          PCA 不會(huì)像前兩種技術(shù)那樣直接顯示最重要的功能。相反,它將返回 N 個(gè)主組件,其中 N 等于原始特征的數(shù)量。

          from sklearn.decomposition import PCA
          pca = PCA().fit(X_train_scaled)
          # 可視化
          plt.plot(pca.explained_variance_ratio_.cumsum(), lw=3, color='#087E8B')
          plt.title('Cumulative explained variance by number of principal components', size=20)
          plt.show()

          但這是什么意思呢?這意味著你可以使用前五個(gè)主要組件解釋源數(shù)據(jù)集中 90%的方差。同樣,如果你不知道這意味著什么,繼續(xù)往下看。

          loadings = pd.DataFrame(
              data=pca.components_.T * np.sqrt(pca.explained_variance_), 
              columns=[f'PC{i}' for i in range(1, len(X_train.columns) + 1)],
              index=X_train.columns
          )
          loadings.head()

          第一個(gè)主要組成部分至關(guān)重要。它只是一個(gè)要素,但它解釋了數(shù)據(jù)集中超過 60% 的方差。從上圖中可以看到,它與平均半徑特征之間的相關(guān)系數(shù)接近 0.8,這被認(rèn)為是強(qiáng)正相關(guān)。

          讓我們可視化所有輸入要素與第一個(gè)主組件之間的相關(guān)性。下面是整個(gè)代碼段(包括可視化):

          pc1_loadings = loadings.sort_values(by='PC1', ascending=False)[['PC1']]
          pc1_loadings = pc1_loadings.reset_index()
          pc1_loadings.columns = ['Attribute''CorrelationWithPC1']

          plt.bar(x=pc1_loadings['Attribute'], height=pc1_loadings['CorrelationWithPC1'], color='#087E8B')
          plt.title('PCA loading scores (first principal component)', size=20)
          plt.xticks(rotation='vertical')
          plt.show()

          這就是如何"破解"PCA,使用它作為特征重要性的方法。

          結(jié)論

          上述總結(jié)來 3 個(gè)機(jī)器學(xué)習(xí)特征重要性的方法,這三個(gè)可根據(jù)場(chǎng)景靈活運(yùn)用。如果你對(duì)機(jī)器學(xué)習(xí)感興趣,可以關(guān)注我。

          也可以加一下老胡的微信
          圍觀朋友圈~~~


          推薦閱讀

          (點(diǎn)擊標(biāo)題可跳轉(zhuǎn)閱讀)

          麻省理工學(xué)院計(jì)算機(jī)課程【中文版】
          【清華大學(xué)王東老師】現(xiàn)代機(jī)器學(xué)習(xí)技術(shù)導(dǎo)論.pdf
          機(jī)器學(xué)習(xí)中令你事半功倍的pipeline處理機(jī)制
          機(jī)器學(xué)習(xí)避坑指南:訓(xùn)練集/測(cè)試集分布一致性檢查
          機(jī)器學(xué)習(xí)深度研究:特征選擇中幾個(gè)重要的統(tǒng)計(jì)學(xué)概念

          老鐵,三連支持一下,好嗎?↓↓↓

          瀏覽 59
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          評(píng)論
          圖片
          表情
          推薦
          點(diǎn)贊
          評(píng)論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報(bào)
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  日批网站在线看 | 久久綜合很很很 | 午影操逼视频 | 久热精品视频在线观看 | 亚洲AⅤ |