def
printing_Kfold_scores(x_train_data,y_train_data):
#定一个函数,函数里面是两个参数
#different C
parameters 定义不同的参数
c_param_range =
[0.01,0.1,1,10,100]
#正则化惩罚项的参数
results_table=pd.DataFrame(index=range(len(c_param_range),2),columns=['C_parameter','Mean
recall score'])
#定一个空的表格,5行2列,且列名分别为 参数和 得分均值
results_table['C_parameter'] = c_param_range
#给参数列赋值
j =
0 #缩进低于def
for c_param in
c_param_range:
print('-------------------------------------------')
print('C parameters : ',c_param)
print('-------------------------------------------')
print('')
recall_accs=[]
#定义一个空的数据集,把得分都放置到这个数据集里
#iteration 交叉验证
fold = KFold(5,shuffle=False)
for train_index, test_index in
fold.split(x_train_data):
# Call the
logistic regression model with a certain C parameter
lr =
LogisticRegression(C = c_param, penalty = 'l1')
# Use the
training data to fit the model. In this case, we use the portion of
the fold to train the model
lr.fit(x_train_data.iloc[train_index,:],
y_train_data.iloc[train_index,:].values.ravel())
# Predict
values using the test indices in the training data
y_pred_undersample =
lr.predict(x_train_data.iloc[test_index,:].values)
#
Calculate the recall score and append it to a list for recall
scores representing the current c_parameter
recall_acc
=
recall_score(y_train_data.iloc[test_index,:].values,y_pred_undersample)
recall_accs.append(recall_acc)
print('recall score = ',
recall_acc)
results_table.loc[j,'Mean recall score'] =
np.mean(recall_accs)
j +=1
print('')
print('Mean recall score
:',np.mean(recall_accs))
print('')
return
results_table