【问题标题】:Can I use pca or z score to choose features?我可以使用 pca 或 z score 来选择特征吗?
【发布时间】:2020-06-06 21:49:06
【问题描述】:

我正在尝试做一个篮球项目。在这个项目中,我有大量关于过去球员表现的数据。有 54 个特征。我刚刚了解了 PCA 和 z 分数(仍然模糊不清)。

我可以使用 PCA 对我的特征执行特征选择吗?

谢谢!

【问题讨论】:

    标签: python project pca feature-selection


    【解决方案1】:

    嗯,进行 PCA 和计算 Z 分数可能会让您到达那里,但是有一种更好的方法可以解决此类问题。请考虑使用特征工程来识别与一组数据(因变量)高度相关的特征,并删除不相关或不太重要的特征,这些特征对我们的目标变量贡献不大(以实现更好的整体准确度)我们的模型)。

    import pandas as pd
    import numpy as np
    import matplotlib.pyplot as plt
    
    
    df = pd.read_csv("https://rodeo-tutorials.s3.amazonaws.com/data/credit-data-trainingset.csv")
    df.head()
    
    from sklearn.ensemble import RandomForestClassifier
    
    features = np.array(['revolving_utilization_of_unsecured_lines',
                         'age', 'number_of_time30-59_days_past_due_not_worse',
                         'debt_ratio', 'monthly_income','number_of_open_credit_lines_and_loans', 
                         'number_of_times90_days_late', 'number_real_estate_loans_or_lines',
                         'number_of_time60-89_days_past_due_not_worse', 'number_of_dependents'])
    clf = RandomForestClassifier()
    clf.fit(df[features], df['serious_dlqin2yrs'])
    
    # from the calculated importances, order them from most to least important
    # and make a barplot so we can visualize what is/isn't important
    importances = clf.feature_importances_
    sorted_idx = np.argsort(importances)
    
    padding = np.arange(len(features)) + 0.5
    plt.barh(padding, importances[sorted_idx], align='center')
    plt.yticks(padding, features[sorted_idx])
    plt.xlabel("Relative Importance")
    plt.title("Variable Importance")
    plt.show()
    

    只需进行您需要进行的任何(非常明显的)更改,以根据您的特定数据集自定义该代码。

    这里有几个链接可以进一步解释特征工程的工作原理。

    https://github.com/WillKoehrsen/feature-selector/blob/master/Feature%20Selector%20Usage.ipynb

    https://towardsdatascience.com/feature-selection-techniques-in-machine-learning-with-python-f24e7da3f36e

    供您参考,这里有一个很好的链接,可以更好地理解 PCA。

    https://scikit-learn.org/stable/auto_examples/decomposition/plot_pca_iris.html

    此外,这里有一个很好的链接,可以更好地理解 Z 分数。

    Pandas - Compute z-score for all columns

    【讨论】:

      【解决方案2】:

      嗯,这取决于特征重要性和您获得的分数(例如准确度、F1 分数、ROC)。如果您的模型过拟合,那么您可能会删除不太重要的特征。

      https://en.wikipedia.org/wiki/Curse_of_dimensionality

      PCA 不一定,除了 ASH 的响应,您还可以使用其他树模型来查找特征重要性。只是不要忘记在建模之前对特征进行缩放,如果不缩放,那么重要性结果可能会被破坏。

      【讨论】:

        猜你喜欢
        • 2020-03-20
        • 2012-10-29
        • 1970-01-01
        • 2018-07-31
        • 2014-02-05
        • 2021-04-06
        • 2013-01-09
        • 1970-01-01
        • 2017-08-14
        相关资源
        最近更新 更多