交易数据异常检测

 1 from sklearn.linear_model import LogisticRegression
 2 from sklearn.model_selection import KFold,cross_val_score
 3 from sklearn.metrics import confusion_matrix,recall_score,classification_report
 4 
 5 def printing_Kfold_scores(x_train_data,y_train_data):
 6     fold = KFold(5,shuffle=False)
 7     
 8     c_param_range = [0.01,0.1,1,10,100]
 9     
10     results_table = pd.DataFrame(index = range(len(c_param_range),2), columns = ['C_parameter','Mean recall score'])
11     
12     j = 0
13     for c_param in c_param_range:
14         print('------------------------------')
15         print('C parameter:', c_param)
16         print('------------------------------')
17         print('')
18         
19         recall_accs = []
20         for iteration, indices in enumerate(fold.split(x_train_data)): #交叉验证
21             #建立罗辑回归模型
22             lr = LogisticRegression(C = c_param, penalty='l1')
23             
24             lr.fit(x_train_data.iloc[indices[0],:],y_train_data.iloc[indices[0],:].values.ravel())
25             
26             y_pred_undersample = lr.predict(x_train_data.iloc[indices[1],:].values)
27             
28             recall_acc = recall_score(y_train_data.iloc[indices[1],:].values,y_pred_undersample)
29             recall_accs.append(recall_acc)
30             print('Iteration ', iteration , ' :recall score= ', recall_acc)
31             
32         results_table.loc[j,'Mean recall score'] = np.mean(recall_accs)
33         j += 1
34         print('')
35         print('Mean recall score', np.mean(recall_accs))
36         print('')
37         
38     best_c = results_table.loc[results_table['Mean recall score'].astype('float64').idxmax()]['C_parameter']
39     print('*****************************************')
40     print('Best model to choose from cross validation is with C Paramter =', best_c)
41     print('*****************************************')
42     
43     return best_c
44 best_c = printing_Kfold_scores(X_train_undersample,y_train_undersample)

 

转载于:https://www.cnblogs.com/tozh/p/11051600.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值