用于Logistic回归评估的Sklearn Python Log Loss引发了错误
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用于Logistic回归评估的Sklearn Python Log Loss引发了错误相关的知识,希望对你有一定的参考价值。
我使用Logistic回归训练了一个模型,需要用Log Loss来评估它的准确性。以下是有关数据的一些细节:
特点/ X.
Principal terms age Gender weekend Bachelor HighSchoolerBelow college
0 1000 30 45 0 0 0 1 0
1 1000 30 33 1 0 1 0 0
2 1000 15 27 0 0 0 0 1
标签/ Y.
array(['PAIDOFF', 'PAIDOFF', 'PAIDOFF', 'PAIDOFF', 'COLLECTION'], dtype=object)
Logistic回归模型:
from sklearn.linear_model import LogisticRegression
logreg = LogisticRegression(C=1e5, solver='lbfgs', multi_class='multinomial')
Feature = df[['Principal','terms','age','Gender','weekend']]
Feature = pd.concat([Feature,pd.get_dummies(df['education'])], axis=1)
Feature.drop(['Master or Above'], axis = 1,inplace=True)
X = Feature
X= preprocessing.StandardScaler().fit(X).transform(X)
y = df['loan_status'].values
X_train, X_test, y_train, lg_y_test = train_test_split(X, y, test_size=0.3, random_state=4)
# we create an instance of Neighbours Classifier and fit the data.
logreg.fit(X_train, y_train)
lg_loan_status = logreg.predict(X_test)
lg_loan_status
现在我需要为此计算Jaccard, F1-score and LogLoss
。
这是我单独的测试数据集:
test_df['due_date'] = pd.to_datetime(test_df['due_date'])
test_df['effective_date'] = pd.to_datetime(test_df['effective_date'])
test_df['dayofweek'] = test_df['effective_date'].dt.dayofweek
test_df['weekend'] = test_df['dayofweek'].apply(lambda x: 1 if (x>3) else 0)
test_df.groupby(['Gender'])['loan_status'].value_counts(normalize=True)
# test_df['Gender'].replace(to_replace=['male','female'], value=[0,1],inplace=True)
Feature = test_df[['Principal','terms','age','Gender','weekend']]
Feature = pd.concat([Feature,pd.get_dummies(df['education'])], axis=1)
Feature.drop(['Master or Above'], axis = 1,inplace=True)
Feature.head()
X = Feature
Y = test_df['loan_status'].values
Feature.head()
Principal terms age Gender weekend Bechalor HighSchoolorBelow college
0 1000.0 30.0 50.0 female 0.0 0 1 0
1 300.0 7.0 35.0 male 1.0 1 0 0
2 1000.0 30.0 43.0 female 1.0 0 0 1
这是我尝试过的:
# Evaluation for Logistic Regression
X_train, X_test, y_train, lg_y_test = train_test_split(X, y, test_size=0.3, random_state=3)
lg_jaccard = jaccard_similarity_score(lg_y_test, lg_loan_status, normalize=False)
lg_f1_score = f1_score(lg_y_test, lg_loan_status, average='micro')
lg_log_loss = log_loss(lg_y_test, lg_loan_status)
print('Jaccard is : {}'.format(lg_jaccard))
print('F1-score is : {}'.format(lg_f1_score))
print('Log Loss is : {}'.format(lg_log_loss))
但它返回此错误:
ValueError:无法将字符串转换为float:'COLLECTION'
更新:这是lg_y_test
:
['PAIDOFF' 'PAIDOFF' 'COLLECTION' 'COLLECTION' 'PAIDOFF' 'COLLECTION'
'PAIDOFF' 'COLLECTION' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF'
'COLLECTION' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'COLLECTION'
'PAIDOFF' 'COLLECTION' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF'
'COLLECTION' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'COLLECTION' 'PAIDOFF'
'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'COLLECTION' 'PAIDOFF' 'COLLECTION'
'COLLECTION' 'COLLECTION' 'PAIDOFF' 'PAIDOFF' 'COLLECTION' 'PAIDOFF'
'PAIDOFF' 'COLLECTION' 'PAIDOFF' 'PAIDOFF' 'COLLECTION' 'COLLECTION'
'PAIDOFF' 'COLLECTION' 'PAIDOFF' 'PAIDOFF' 'COLLECTION' 'COLLECTION'
'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF'
'COLLECTION' 'COLLECTION' 'PAIDOFF' 'COLLECTION' 'PAIDOFF' 'PAIDOFF'
'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF'
'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF'
'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF'
'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'COLLECTION'
'COLLECTION' 'PAIDOFF' 'PAIDOFF' 'PAIDOFF' 'COLLECTION' 'PAIDOFF'
'PAIDOFF' 'PAIDOFF' 'COLLECTION']
答案
问题如下:
要计算log_loss,您需要具有预测的概率。如果仅提供预测类(具有最大概率的类),则无法计算此度量。
只要有可能,Sklearn就会提供predict_proba方法。您应该按如下方式使用它:
lg_loan_status_probas = logreg.predict_proba(X_test)
lg_log_loss = log_loss(lg_y_test, lg_loan_status_probas)
以上是关于用于Logistic回归评估的Sklearn Python Log Loss引发了错误的主要内容,如果未能解决你的问题,请参考以下文章
如何消除 _logistic 回归上的 sklearn 警告
基于sklearn进行线性回归logistic回归svm等的简单操作总结
R语言glm拟合logistic回归模型:输出logistic回归的summary信息可视化logistic回归模型的系数logistic回归模型分类评估计算(混淆矩阵accuracy偏差)