用于 Logistic 回归评估的 Sklearn Python Log Loss 引发错误
Posted
技术标签:
【中文标题】用于 Logistic 回归评估的 Sklearn Python Log Loss 引发错误【英文标题】:Sklearn Python Log Loss for Logistic Regression evaluation raised an error 【发布时间】:2019-03-29 15:02:36 【问题描述】:我使用 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)
逻辑回归模型:
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:无法将字符串转换为浮点数:'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']
【问题讨论】:
【参考方案1】:问题如下:
要计算 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)
【讨论】:
嗨@GabrielM,现在它返回另一个错误ValueError: Input contains NaN, infinity or a value too large for dtype('float64').
你能打印 'lg_loan_status_probas' 和 'lg_y_test' 吗?
错误出现在lg_loan_status_probas
,无法打印。
我在上面的问题中添加了lg_y_test
的print
结果,请看一下!
我明白了。你能打印 'lg_loan_status' 吗?谢谢!【参考方案2】:
要计算逻辑回归的对数损失或交叉熵损失,请执行以下操作(自包含示例):
from sklearn.datasets import load_iris
from sklearn.linear_model import LogisticRegression
from sklearn import metrics
X, y = load_iris(return_X_y=True)
clf = LogisticRegression(random_state=0).fit(X, y)
clf.predict(X[:2, :])
clf.predict_proba(X[:2, :])
clf.score(X, y)
y_probs = cls.predict_proba(X)
qry_loss_t = metrics.log_loss(y, y_probs)
参考:
https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html https://scikit-learn.org/stable/modules/generated/sklearn.metrics.log_loss.html【讨论】:
以上是关于用于 Logistic 回归评估的 Sklearn Python Log Loss 引发错误的主要内容,如果未能解决你的问题,请参考以下文章
基于sklearn进行线性回归logistic回归svm等的简单操作总结
如何消除 _logistic 回归上的 sklearn 警告
R语言glm拟合logistic回归模型:输出logistic回归的summary信息可视化logistic回归模型的系数logistic回归模型分类评估计算(混淆矩阵accuracy偏差)
为啥是负数(MSE 或 MAS)评分参数(如 SKLEARN 中的 neg_mean_absolute_error)被考虑用于回归模型评估