我的数据集看起来如下(前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)发布于 2020-04-29 11:07:40
这里有几个问题,但让我先给出一个提示:
您在这里尝试过的许多模型有一个基本的原因-- Classifier和Regressor;分类和回归是两种不同和相互排斥的问题类型,其中只有一种类型适用于特定问题,而不是两者兼而有之。
分类或回归问题的标准由目标变量决定;在这里,您的Credit_Score是一个数值变量,因此您处于回归设置中。作为推论,您在这里使用分类器模型的所有实验都是有意义的和无效的,它们可以被安全丢弃(难怪它们显示出如此低的“准确性”性能)。
另一个问题是你使用“准确性”;这个术语在ML中有一个非常具体的含义,这与它在日常生活中的使用不完全相同:它是正确分类的样本的百分比;正如在这个定义中已经暗示的那样,准确性只适用于分类问题,它在回归问题中的使用(比如这里的例子)是无意义。
这里使用的每一种scikit学习模型都有自己的score方法,最好检查一下文档,以确定对特定模型适用的分数是多少;对于好坏,scikit的开发人员已经做出选择,在他们的回归器模型中通常使用R^2 (或R-平方)系数。虽然通常(并不总是)这是[0, 1]中的一个数字,但我们通常不使用它作为百分比(就像这里所做的那样),而且它也不是回归模型的“准确性”(同样,这样的东西并不存在)。
我已经解释了其他地方为什么在ML预测设置中选择R^2是一个不幸的选择;引用:
整个R-平方概念实际上直接来自于统计领域,在那里强调的是解释模型,在机器学习环境中几乎没有什么用处,在机器学习环境中,强调的显然是预测模型;至少AFAIK,除了一些非常入门的课程之外,我从来没有(我的意思是从来没有.)我们看到了一个预测建模问题,R-平方被用于任何类型的性能评估;流行的机器学习介绍,比如安德鲁·吴( Andrew )在古瑟拉的机器学习,甚至连提都不提,这也不是意外。正如在上面的Github螺纹中所指出的(强调后加): 特别是在使用测试集时,我有点不清楚R^2是什么意思。我当然同意。
因此,最好不要使用所使用的回归器的本地score方法,而改用均方误差(MSE)、平均绝对误差(MAE)、根均方误差(RMSE)等得分,这些方法实际上用于预测ML设置(并且都在科学知识-学习中可用)。
这些回归度量的“缺点”是,根据定义,它们不能以百分比表示,因此您需要进行一些额外的检查,以确定结果是否适合您的情况。
关于陈述的最后通知。
根据我在这里的经验,很奇怪,这类事情(例如在回归问题中使用量词)比人们想象的更频繁,但在我回答之前的7小时内,社区中的人并没有发现这种情况;我的猜测是,这是由于发布了大量令人恐惧的代码,加上一个相当不幸和可以说是不好的标题(我差点通过了自己的名字)。只是说..。
https://stackoverflow.com/questions/61493922
复制相似问题