<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>

          數(shù)據(jù)分析——特征工程之特征關(guān)聯(lián)

          共 2167字,需瀏覽 5分鐘

           ·

          2020-09-19 07:17

          一、特征的相關(guān)矩陣

          • 特征的相關(guān)矩陣

          • 目標(biāo)的相關(guān)矩陣

          • 畫出最相關(guān)的特征之間的關(guān)系

          import?pandas?as?pd
          import?seaborn?as?sns
          import?matplotlib.pyplot?as?plt

          df?=?pd.read_csv('./data/train.csv')
          corr_mat?=?df.corr()
          f,?ax?=?plt.subplots(figsize=(12,?9))
          sns.heatmap(corr_mat,?vmax=.8,?square=True,?ax=ax)
          plt.show()

          通過這個(gè)熱力圖,可以很直觀的對(duì)各個(gè)特征之間的關(guān)系一目了然。另外,從圖中可以看出TotalBsmtSF和1stFlrSF變量之間的相關(guān)性很明顯,這就說明了如果同時(shí)使用這兩個(gè)特征就會(huì)導(dǎo)致信息的冗余性,所以要盡量避免使用冗余的特征。

          其中看一下df.corr()函數(shù)的源碼:

          ?def?corr(self,?method='pearson',?min_periods=1):
          ????????"""
          ????????Compute?pairwise?correlation?of?columns,?excluding?NA/null?values

          ????????Parameters
          ????????----------
          ????????method?:?{'pearson',?'kendall',?'spearman'}
          ????????????*?pearson?:?standard?correlation?coefficient
          ????????????*?kendall?:?Kendall?Tau?correlation?coefficient
          ????????????*?spearman?:?Spearman?rank?correlation
          ????????min_periods?:?int,?optional
          ????????????Minimum?number?of?observations?required?per?pair?of?columns
          ????????????to?have?a?valid?result.?Currently?only?available?for?pearson
          ????????????and?spearman?correlation

          ????????Returns
          ????????-------
          ????????y?:?DataFrame
          ????????"
          ""

          默認(rèn)method參數(shù)是pearson,下面介紹下這三種相關(guān)系數(shù):

          1、Pearson correlation coefficient(皮爾遜相關(guān)系數(shù))

          皮爾遜相關(guān)系數(shù)用于度量?jī)蓚€(gè)變量X和Y之間的相關(guān)性(線性相關(guān)),它的值介于-1和1之間

          兩個(gè)變量之間的皮爾遜相關(guān)系數(shù)定義為兩個(gè)變量之間的協(xié)方差和標(biāo)準(zhǔn)差的商。

          2、Kendall correlation coefficient(肯德爾相關(guān)性系數(shù))

          它所計(jì)算的對(duì)象是分類變量。

          3、Spearman correlation coefficient(斯皮爾曼相關(guān)性系數(shù))

          它是衡量?jī)蓚€(gè)變量的依賴性的非參數(shù)指標(biāo)。它利用單調(diào)方程評(píng)價(jià)兩個(gè)統(tǒng)計(jì)變量的相關(guān)性,如果數(shù)據(jù)中沒有重復(fù)值,并且兩個(gè)變量完全單調(diào)相關(guān)時(shí),斯皮爾曼相關(guān)系數(shù)則為+1或者-1。


          二、目標(biāo)的相關(guān)矩陣

          找到與目標(biāo)值相關(guān)性最大的幾個(gè)特征,而這幾個(gè)特征之間的相關(guān)性要低。

          ?k?=?10
          cols?=?corr_mat.nlargest(k,?'SalePrice')['SalePrice'].index
          cm?=?np.corrcoef(df[cols].values.T)
          sns.heatmap(cm,?annot=True,?square=True,?yticklabels=cols.values,?xticklabels=cols.values)
          plt.show()

          通過以上代碼得到與目標(biāo)值相關(guān)性最大的前10個(gè)值,做出熱力圖。

          從圖中可以很明顯地看出OverallQual、GrLivArea等特征與SalePrice目標(biāo)值之間有很大的關(guān)聯(lián)。


          三、最相關(guān)的特征之間的關(guān)系圖

          cols?=?['SalePrice',?'OverallQual',?'GrLivArea',?'GarageCars',?'TotalBsmtSF',?'FullBath',?'YearBuilt']
          sns.pairplot(df[cols],?height?=?2.5)
          plt.show()

          盡管我們已經(jīng)知道了這些特征之間的一些關(guān)系,但是通過這個(gè)圖可以給與一個(gè)更充分的理由。


          《數(shù)據(jù)科學(xué)與人工智能》公眾號(hào)推薦朋友們學(xué)習(xí)和使用Python語言,需要加入Python語言群的,請(qǐng)掃碼加我個(gè)人微信,備注【姓名-Python群】,我誠邀你入群,大家學(xué)習(xí)和分享。


          ? ? 關(guān)于Python語言,有任何問題或者想法,請(qǐng)留言或者加群討論。

          瀏覽 298
          點(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>
                  亚洲视频一区二区三区 | 精品欧美乱码久久久久久 | 成人三级电影天堂 | 91av在线观看免费一区 | 风间由美一区二区 |