【问题标题】:What features to use for regression or classification?哪些特征用于回归或分类?
【发布时间】:2019-11-24 04:50:34
【问题描述】:

有没有办法确定哪些特征与我的机器学习模型最相关。如果我有 20 个功能,是否有一个功能可以决定我应该使用哪些功能(或自动删除不相关功能的功能)? 我计划为回归或分类模型执行此操作。

我想要的输出是最相关的值列表和预测

import pandas as pd
from sklearn.linear_model import LinearRegression

dic = {'par_1': [10, 30, 11, 19, 28, 33, 23],
       'par_2': [1, 3, 1, 2, 3, 3, 2],
       'par_3': [15, 3, 16, 65, 24, 56, 13],
       'outcome': [101, 905, 182, 268, 646, 624, 465]}

df = pd.DataFrame(dic)

variables = df.iloc[:,:-1]
results = df.iloc[:,-1]

print(variables.shape)
print(results.shape)


reg = LinearRegression()
reg.fit(variables, results)

x = reg.predict([[18, 2, 21]])[0]
print(x)

【问题讨论】:

    标签: python machine-learning feature-selection


    【解决方案1】:

    您正在寻找的术语是特征选择:它包括确定哪些特征与您的分析最相关。 scikit-learn 库有一个专门的部分 here

    另一种可能性是采用降维技术,例如PCA(主成分分析)或随机投影。每种技术都有其优缺点,很大程度上取决于您拥有的数据和具体的应用。

    【讨论】:

    • 我已经阅读了它,但我不知道如何在我的代码中实现它。如何获取最相关的功能列表
    【解决方案2】:

    您可以访问 reg 对象的 coef_ 属性:

    print(reg.coef_)
    

    称这些权重过于简单,因为它们在线性回归中具有特定含义。但它们就是你所拥有的。

    【讨论】:

      【解决方案3】:

      使用线性模型时,使用线性独立特征很重要。您可以使用df.corr() 可视化相关性:

      import pandas as pd
      import numpy as np
      from sklearn.linear_model import LinearRegression
      from sklearn.decomposition import PCA
      from sklearn.metrics import mean_squared_error
      
      numpy.random.seed(2)
      
      dic = {'par_1': [10, 30, 11, 19, 28, 33, 23],
             'par_2': [1, 3, 1, 2, 3, 3, 2],
             'par_3': [15, 3, 16, 65, 24, 56, 13],
             'outcome': [101, 905, 182, 268, 646, 624, 465]}
      
      df = pd.DataFrame(dic)
      
      print(df.corr())
      
      out:
                  par_1     par_2     par_3   outcome
      par_1    1.000000  0.977935  0.191422  0.913878
      par_2    0.977935  1.000000  0.193213  0.919307
      par_3    0.191422  0.193213  1.000000 -0.158170
      outcome  0.913878  0.919307 -0.158170  1.000000
      

      您可以看到par_1par_2 具有很强的相关性。正如@taga 提到的,您可以使用PCA 将您的特征映射到线性独立的低维空间:

      variables = df.iloc[:,:-1]
      results = df.iloc[:,-1]
      
      pca = PCA(n_components=2)
      pca_all = pca.fit_transform(variables)
      
      print(np.corrcoef(pca_all[:, 0], pca_all[:, 1]))
      
      out:
      [[1.00000000e+00 1.87242048e-16]
       [1.87242048e-16 1.00000000e+00]]
      

      记得在样本外数据上验证您的模型:

      X_train = variables[:4]
      y_train = results[:4]
      X_valid = variables[4:]
      y_valid = results[4:]
      
      pca = PCA(n_components=2)
      pca.fit(X_train)
      
      pca_train = pca.transform(X_train)
      pca_valid = pca.transform(X_valid)
      print(pca_train)
      
      reg = LinearRegression()
      reg.fit(pca_train, y_train)
      
      yhat_train = reg.predict(pca_train)
      yhat_valid = reg.predict(pca_valid)
      
      print(mean_squared_error(yhat_train, y_train))
      print(mean_squared_error(yhat_valid, y_valid))
      

      特征选择并非易事:有很多 sklearn 模块可以实现它(请参阅docs),您应该始终尝试至少其中几个,看看哪些可以提高样本外数据的性能。

      【讨论】:

        【解决方案4】:

        嗯,最初我遇到了同样的问题。我发现这两种方法对选择相关功能很有用。

        1.您可以通过模型的特征重要性属性获取数据集每个特征的特征重要性。特征重要性是基于树的分类器附带的内置类。

        import pandas as pd
        import numpy as np
        data = pd.read_csv("D://Blogs//train.csv")
        X = data.iloc[:,0:20]  #independent columns
        y = data.iloc[:,-1]    #target column i.e price range
        from sklearn.ensemble import ExtraTreesClassifier
        import matplotlib.pyplot as plt
        model = ExtraTreesClassifier()
        model.fit(X,y)
        print(model.feature_importances_) #use inbuilt class feature_importances of tree based classifiers
        #plot graph of feature importances for better visualization
        feat_importances = pd.Series(model.feature_importances_, index=X.columns)
        feat_importances.nlargest(10).plot(kind='barh')
        plt.show()
        

        click to see image

        2.带有热图的相关矩阵

        相关性说明特征如何相互关联或与目标变量关联。 它可以直观地了解特征如何与目标变量相关。

        click to see image

        这不是我的研究,但这个博客 feature selection 帮助消除了我的疑问,我相信你也会这样做。:)

        【讨论】:

          猜你喜欢
          • 2017-05-25
          • 2013-05-01
          • 2020-04-08
          • 2018-08-08
          • 2019-01-04
          • 1970-01-01
          • 2020-05-27
          • 2017-04-09
          • 2020-07-27
          相关资源
          最近更新 更多