我已经使用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
标签/年
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:无法将字符串转换为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']
答案 0 :(得分:0)
问题如下:
要计算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)
答案 1 :(得分:0)
要计算逻辑回归的对数损失或交叉熵损失,请执行以下操作(自包含示例):
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)
参考: