【问题标题】:Very low Regression scores and ultra low Classification scores非常低的回归分数和超低的分类分数
【发布时间】:2020-08-13 01:50:10
【问题描述】:

我的数据集如下所示(前 20 条记录)。我正在测试的脚本如下。

Credit_Score    Net_Advance APR Mosaic  Time_at_Address Time_in_Employment  Time_with_Bank  Value_of_Property   Total_Outstanding_Balances  Age
918 3000    14.4    46  132 288 168 178,000.00  64406   46
903 21000   7.9 16  288 37  300 180,000.00  31614   59
1060    7200    7.9 17  276 154 369 199,000.00  26045   56
839 8000    16.9    47  48  82  216 120,000.00  181217  33
1057    7650    7.4 55  156 342 510 180,000.00  63811   49
913 33000   9.4 59  18  170 240 205,000.00  219003  45
840 8000    15.9    12  293 77  317 179,000.00  90797   51
961 5300    11.9    43  163 351 243 92,000.00   84624   49
901 12000   11.9    11  108 24  180 180,000.00  158678  55
915 6000    12.9    49  36  72  384 120,000.00  2785    48
840 10150   12.4    24  37  58  261 110,000.00  109231  27
968 18000   8.4 24  2   168 420 120,000.00  85502   49
904 10000   8.7 46  24  8   174 150,000.00  157718  37
924 8000    9.9 47  418 439 379 120,000.00  2827    72
896 5000    9.4 15  4   240 300 246,000.00  257560  48
804 5000    17.1    44  12  36  240 165,000.00  160650  37
840 21200   11.5    44  339 133 231 117,000.00  31316   50
862 2000    31.9    18  44  63  186 291,000.00  279819  35
785 1100    40.9    23  94  54  150 120,000.00  789 39
847 20000   9.4 16  237 309 326 272,000.00  170348  59

这是我的实际代码。

# Using both Regression and Classification to measure the Credit Score of a customer
import numpy as np
import pandas as pd
from sklearn import datasets
from scipy import stats
import seaborn as sns
import matplotlib.pyplot as plt
from sklearn.ensemble import RandomForestClassifier
from sklearn import model_selection# random forest model creation
from sklearn.model_selection import train_test_split# implementing train-test-split
from sklearn.model_selection import cross_val_score
from sklearn.metrics import classification_report, confusion_matrix

# load data from CSV into data frame and use a specific argument 'thousands=',''
df = pd.read_csv("C:\\my_path\\credit.csv", encoding="ISO-8859-1",sep=',', thousands=',')

# view a small sample of data for piece of mind
df.head()

from sklearn.ensemble import RandomForestClassifier
features = np.array(['Net_Advance', 'APR', 'Mosaic', 'Mosaic_Class', 'Time_at_Address', 'Number_of_Dependants', 'Time_in_Employment', 'Income_Range', 'Time_with_Bank', 'Value_of_Property', 'Outstanding_Mortgage_Bal', 'Total_Outstanding_Balances', 'Age'])
clf = RandomForestClassifier()
clf.fit(df[features], df['Credit_Score'])
# 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()

# try PCA & LDA methodologies
# first PCA ...
X = df[['Net_Advance', 'APR', 'Mosaic', 'Mosaic_Class', 'Time_at_Address', 'Number_of_Dependants', 'Time_in_Employment', 'Income_Range', 'Time_with_Bank', 'Value_of_Property', 'Outstanding_Mortgage_Bal', 'Total_Outstanding_Balances', 'Age']]
y = df[['Credit_Score']]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)


X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, random_state=66)

from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
from sklearn.decomposition import PCA
pca = PCA()
X_train = pca.fit_transform(X_train)
X_test = pca.transform(X_test)
explained_variance = pca.explained_variance_ratio_
from sklearn.decomposition import PCA
pca = PCA(n_components=1)
X_train = pca.fit_transform(X_train)
X_test = pca.transform(X_test)
from sklearn.ensemble import RandomForestClassifier
classifier = RandomForestClassifier(max_depth=2, random_state=0)
classifier.fit(X_train, y_train)
# Predicting the Test set results
y_pred = classifier.predict(X_test)

# Performance Evaluation
from sklearn.metrics import confusion_matrix
from sklearn.metrics import accuracy_score
cm = confusion_matrix(y_test, y_pred)
print(cm)
print('Accuracy ' + str(accuracy_score(y_test, y_pred)))

# Result:
Accuracy 0.009062326613648974

所以,我的问题是,学习怎么会如此、如此、如此低?根据上面显示的代码和结果,这基本上是零学习成果。此外,当我测试一些其他概念/实验时,如下所述,我看到的准确度结果最多约为 60%。我希望得到大约 90% 以上的准确度结果...这是我正在测试的代码。

    # Baggin Classifier
    from sklearn.ensemble import BaggingClassifier
    from sklearn import tree
    model = BaggingClassifier(tree.DecisionTreeClassifier(random_state=1))
    model.fit(x_train, y_train)
    model.score(x_test,y_test)
    # around 5% accorate.  horrible!


    # Bagging Regressor
    from sklearn.ensemble import BaggingRegressor
    model = BaggingRegressor(tree.DecisionTreeRegressor(random_state=1))
    model.fit(x_train, y_train)
    model.score(x_test,y_test)
    # almost 65% accurate; better but not great!


    # AdaBoostClassifier
    from sklearn.ensemble import AdaBoostClassifier
    model = AdaBoostClassifier(random_state=1)
    model.fit(x_train, y_train)
    model.score(x_test,y_test)
    # just 1% accurate!  no way!!


    # AdaBoostRegressor
    from sklearn.ensemble import AdaBoostRegressor
    model = AdaBoostRegressor()
    model.fit(x_train, y_train)
    model.score(x_test,y_test)
    # around 60%. just ok.


    # GradientBoostingClassifier
    from sklearn.ensemble import GradientBoostingClassifier
    model= GradientBoostingClassifier(learning_rate=0.1,random_state=1)
    model.fit(x_train, y_train)
    model.score(x_test,y_test)
    # around 60%. just ok. 


    # GradientBoostingRegressor
    from sklearn.ensemble import GradientBoostingRegressor
    model= GradientBoostingRegressor()
    model.fit(x_train, y_train)
    model.score(x_test,y_test)
    # around 60%. just ok.


    # XGBClassifier
    import xgboost as xgb
    model=xgb.XGBClassifier(random_state=1,learning_rate=0.1)
    model.fit(x_train, y_train)
    model.score(x_test,y_test)
    # around 60%. just ok.


    # XGBRegressor
    import xgboost as xgb
    model=xgb.XGBRegressor()
    model.fit(x_train, y_train)
    model.score(x_test,y_test)
    # around 60%. just ok.

知道为什么这个,一方面,是错的???

# XGBRegressor
import xgboost as xgb
model=xgb.XGBRegressor()
model.fit(x_train, y_train)
model.score(x_test,y_test)

【问题讨论】:

    标签: python python-3.x machine-learning scikit-learn


    【解决方案1】:

    这里有几个问题,但让我先提示一下:

    您在这里尝试的许多模型都有两个“版本”——ClassifierRegressor,这是有根本原因的;分类和回归是两种不同且互斥的问题类型,只有其中一种类型适用于特定问题,而不能同时适用。

    问题是分类还是回归问题的标准由目标变量确定;在这里,您的 Credit_Score 是一个数字变量,因此您处于 回归 设置中。作为推论,您在这里使用分类器模型进行的所有实验都是有意义且无效的,并且可以安全地丢弃它们(难怪它们显示出如此超低的“准确性”性能)。

    另一个问题是您对“准确性”的使用;这个词在 ML 中有一个非常具体的含义,和它在日常生活中的使用并不完全相同:它是正确分类的样本的百分比;正如这个定义中已经暗示的那样,准确度只适用于分类问题,它在回归问题中的使用(比如你这里的)是meaningless

    您在此处使用的每个 scikit-learn 模型都有其自己的 score 方法,建议您查看文档以确定适用于特定模型的确切分数;无论好坏,scikit-learn 开发人员都选择在他们的回归模型中通常使用 R^2(或 R 平方)系数。虽然通常(并非总是)这是[0, 1] 中的一个数字,但我们通常不会将其用作百分比(就像您在此处所做的那样),它也不是回归的“准确度”模型(同样,这样的东西不存在)。

    我已经解释了elsewhere 为什么在 ML predictive 设置中选择 R^2 是一个不幸的选择;引用:

    整个 R 平方的概念实际上直接来自于 统计,重点是解释性模型,它 在机器学习环境中几乎没有用,重点是 预测模型;至少 AFAIK,并且超出了一些非常 入门课程,我从来没有(我的意思是从来没有...)见过 R平方用于任何类型的预测建模问题 绩效评估;流行也不是偶然 机器学习介绍,例如 Andrew Ng 在 Coursera 的 Machine Learning,甚至都懒得提及。并作为 在上面的Github thread 中注明(已添加重点):

    特别是在使用 test 集时,我有点不清楚 R^2 的含义。

    我当然同意。

    因此,您最好摆脱使用的回归器的原生 score 方法,改为使用均方误差 (MSE)、平均绝对误差 (MAE)、均方根误差 (RMSE) 等分数等等,这些都是在 predictive ML 设置中实际使用的那些(并且都在 scikit-learn 中可用)。

    这些回归指标的“缺点”是,根据定义,它们不能以百分比表示,因此您需要进行一些额外的检查,以确定结果是否足以满足您的情况。


    关于演示的最后通知。

    根据我在 SO 的经验,很奇怪,像这样的事情(即在回归问题中使用分类器)发生的频率比人们想象的要频繁,但社区中的某个人却没有很长时间地接受它,这很奇怪在我回答前约 7 小时;我的猜测是,这是由于发布了令人生畏的代码量,加上一个相当不幸且可以说是糟糕的标题(我几乎通过了自己)。只是说...

    【讨论】:

    • 是的,是的,是的,我同意这里的一切。我当然理解回归和分类之间的区别。我想我只是期望回归算法表现更好,我也期望分类表现更好一点,至少比 0 好。我想每个人都在做它设计的事情。感谢您的反馈!欣赏它!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-10-28
    • 2019-08-21
    • 2020-02-05
    • 2021-03-07
    • 1970-01-01
    • 2022-07-25
    • 1970-01-01
    相关资源
    最近更新 更多