【问题标题】:Ensure minimal information loss with classifiears with Python's Scikit-Learn使用 Python 的 Scikit-Learn 分类器确保最小的信息丢失
【发布时间】:2019-12-28 12:31:57
【问题描述】:

我有一个包含很多列/特征的数据集,我想制作将数据分类为 1 和 0 的 ML 模型,但我想减少列数。 我已经阅读了很多关于feature selection 的内容,但我并不清楚。 当我减少列/功能的数量时,如何确保将丢失的信息降至最低,或者我如何知道我是否丢失了很多信息? 我可以删除的最佳特征数量是多少,我怎么知道我没有过度拟合模型?

我用过PCA,我做了这样的事情:

import pandas as pd

from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score

from sklearn.decomposition import PCA

from sklearn.ensemble import RandomForestClassifier


values = [0.9, 0.925, 0.95, 0.96, 0.97]

for i in values:
    print(i)

    pca = PCA(i)

    x_train, x_test, y_train, y_test = train_test_split(x, y, test_size = 0.4, random_state = 0)

    pca.fit(x_train)

    x_train = pca.transform(x_train)
    x_test = pca.transform(x_test)

    print(pca.n_components_)

    model = RandomForestClassifier(n_estimators = 100)
    model.fit(x_train, y_train)

    # provera tacnosti
    model_score1 = model.score(x_train, y_train)
    model_score2 = model.score(x_test, y_test)

    print(model_score1, model_score2)

    prediction_result =  model.predict(x_test)
    accuracy_test = accuracy_score(y_test, prediction_result)

    prediction_result_train =  model.predict(x_train)
    accuracy_train= accuracy_score(y_train, prediction_result_train)

    print(accuracy_train, accuracy_test)

我检查了训练和测试数据的score 和accuracy_score,因为我想确保我没有过度拟合(训练和测试准确度之间的巨大差异)

我也用过'RFE'(但它需要很多时间),我也用过ExtraTreesClassifier和RandomForestClassifier和model.feature_importances_:

model = ExtraTreesClassifier(n_estimators = 100)
model.fit(x, y)
extra_tree_score = model.feature_importances_   

model = RandomForestClassifier(n_estimators = 100)
model.fit(x, y)
extra_tree_score = model.feature_importances_

我喜欢model.feature_importances_,但我不知道我应该采用哪些功能 *(top 10, top 50, top 75) 以确保将信息丢失降至最低。

【问题讨论】:

    标签: python scikit-learn pca feature-selection dimensionality-reduction


    【解决方案1】:

    特征选择

    PCA 只是一种降维方法。通过使用 PCA,您的功能将发生变化,并且它们没有原来的解释。

    如果您想使用降维,尤其是 PCA,您可以测试 PCA 的工作原理。只需:每台 PC 的方差(信息)百分比即可。如果旋转数据协调没有保留足够的原始数据,那么 PCA 就不能很好地工作。您可以在

    中看到良好的 PC 图表

    A step by step explanation of Principal Component Analysis

    PCA 不适用于非线性数据。所以 PCA 并不总是可行的。

    您可以检查数据相关性以消除数据中的冗余。

    How to Calculate Correlation Between Variables in Python

    当两个特征(变量)相互之间具有良好的相关性时:

    对于多个特征的测试相关性,最好看看这个链接:

    Finding Correlation Between Many Variables (Multidimensional Dataset) with Python

    在这个矩阵混淆中,你会看到箭头和风有相关性。

    降维的非线性方法有很多。对于非线性,我找到了这个 python 包。

    pyHSICLasso

    【讨论】:

    • 好的,但是如果我有 100 个特征,如何检查我的数据是否是线性的。我试过df.corr(),然后按output对这些值进行排序,看看这些值是接近1还是-1,我发现大多数数据在-0.4和+0.4之间,这意味着我的数据是不是线性的?非线性数据的特征选择方法有哪些?
    猜你喜欢
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 2018-07-03
    • 1970-01-01
    • 2016-12-29
    • 2018-07-01
    • 2018-03-26
    相关资源
    最近更新 更多