鱼C论坛

 找回密码
 立即注册
查看: 2301|回复: 6

[已解决]有关信用卡欺诈的那个例子

[复制链接]
发表于 2019-12-11 16:23:57 | 显示全部楼层 |阅读模式
30鱼币

附件那边上传大小必须2m以下,我就只取了csv前面几行数据

代码是这样写的:


  1. #信用卡欺诈检测
  2. import pandas as pd
  3. import matplotlib.pyplot as plt
  4. import numpy as np
  5. #%matplotlib inline 魔法方法ipython中用
  6. data = pd.read_csv(r"D:\储存\学习\唐宇迪python\01、python数据分析与机器学习实战\课程资料\唐宇迪-机器学习课程资料\机器学习算法配套案例实战\逻辑回归-信用卡欺诈检测\creditcard.csv")
  7. count_classes = pd.value_counts(data['Class'], sort = True).sort_index()
  8. count_classes.plot(kind = 'bar')                #建立柱状图
  9. plt.title("Fraud class histogram")
  10. plt.xlabel("Class")
  11. plt.ylabel("Frequency")

  12. from sklearn.preprocessing import StandardScaler

  13. data['normAmount'] = StandardScaler().fit_transform(data['Amount'].values.reshape(-1, 1))
  14. data = data.drop(['Time','Amount'],axis=1)

  15. X = data.ix[:, data.columns != 'Class']
  16. y = data.ix[:, data.columns == 'Class']

  17. number_records_fraud = len(data[data.Class == 1])
  18. fraud_indices = np.array(data[data.Class == 1].index)


  19. normal_indices = data[data.Class == 0].index

  20. random_normal_indices = np.random.choice(normal_indices, number_records_fraud, replace = False)
  21. random_normal_indices = np.array(random_normal_indices)

  22. under_sample_indices = np.concatenate([fraud_indices,random_normal_indices])

  23. under_sample_data = data.iloc[under_sample_indices,:]

  24. X_undersample = under_sample_data.ix[:, under_sample_data.columns != 'Class']
  25. y_undersample = under_sample_data.ix[:, under_sample_data.columns == 'Class']
  26. # Showing ratio
  27. print("Percentage of normal transactions: ", len(under_sample_data[under_sample_data.Class == 0])/len(under_sample_data))
  28. print("Percentage of fraud transactions: ", len(under_sample_data[under_sample_data.Class == 1])/len(under_sample_data))
  29. print("Total number of transactions in resampled data: ", len(under_sample_data))

  30. from sklearn.model_selection import train_test_split


  31. X_train, X_test, y_train, y_test = train_test_split(X,y,test_size = 0.3, random_state = 0)


  32. print("Number transactions train dataset: ", len(X_train))
  33. print("Number transactions test dataset: ", len(X_test))
  34. print("Total number of transactions: ", len(X_train)+len(X_test))

  35. # 下采样
  36. X_train_undersample, X_test_undersample, y_train_undersample, y_test_undersample = train_test_split(X_undersample,y_undersample,test_size = 0.3,random_state = 0)
  37. print("")
  38. print("Number transactions train dataset: ", len(X_train_undersample))
  39. print("Number transactions test dataset: ", len(X_test_undersample))
  40. print("Total number of transactions: ", len(X_train_undersample)+len(X_test_undersample))

  41. #Recall = TP/(TP+FN)
  42. from sklearn.linear_model import LogisticRegression                                
  43. from sklearn.model_selection import KFold                                         
  44. from sklearn.model_selection import cross_val_score                                
  45. from sklearn.metrics import confusion_matrix,recall_score,classification_report   

  46. #交叉验证
  47. def printing_Kfold_scores(x_train_data,y_train_data):

  48.     fold = KFold(5,shuffle=False)            
  49. #    fold.get_n_splits(len(y_train_data))   
  50.    
  51.     # 定义不同正则化惩罚力度
  52.     c_param_range = [0.01,0.1,1,10,100]

  53.     #可视化显示
  54.     results_table = pd.DataFrame(index = range(len(c_param_range),2), columns = ['C_parameter','Mean recall score'])
  55.     results_table['C_parameter'] = c_param_range

  56.     # the k-fold will give 2 lists: train_indices = indices[0], test_indices = indices[1]
  57.     j = 0
  58.     for c_param in c_param_range:
  59.         print('-------------------------------------------')
  60.         print('C parameter: ', c_param)
  61.         print('-------------------------------------------')
  62.         print('')

  63.         recall_accs = []
  64.         for iteration, indices in fold.split(X):   
  65.             lr = LogisticRegression(C = c_param, penalty = 'l1')

  66.             # Use the training data to fit the model. In this case, we use the portion of the fold to train the model
  67.             # with indices[0]. We then predict on the portion assigned as the 'test cross validation' with indices[1]
  68.             lr.fit(x_train_data.iloc[indices[0],:],y_train_data.iloc[indices[0],:].values.ravel())

  69.             # Predict values using the test indices in the training data
  70.             y_pred_undersample = lr.predict(x_train_data.iloc[indices[1],:].values)

  71.             # Calculate the recall score and append it to a list for recall scores representing the current c_parameter
  72.             recall_acc = recall_score(y_train_data.iloc[indices[1],:].values,y_pred_undersample)
  73.             recall_accs.append(recall_acc)
  74.             print('Iteration ', iteration,': recall score = ', recall_acc)

  75.         # The mean value of those recall scores is the metric we want to save and get hold of.
  76.         results_table.ix[j,'Mean recall score'] = np.mean(recall_accs)
  77.         j += 1
  78.         print('')
  79.         print('Mean recall score ', np.mean(recall_accs))
  80.         print('')

  81.     best_c = results_table.loc[results_table['Mean recall score'].idxmax()]['C_parameter']
  82.    
  83.     # Finally, we can check which C parameter is the best amongst the chosen.
  84.     print('*********************************************************************************')
  85.     print('Best model to choose from cross validation is with C parameter = ', best_c)
  86.     print('*********************************************************************************')
  87.    
  88.     return best_c

  89. best_c = printing_Kfold_scores(X_train_undersample,y_train_undersample)
复制代码


===========================================================================================

结果就返回错误:
ValueError: Expected 2D array, got 1D array instead:
array=[-1.86375555  3.44264398 -4.46825973  2.80533626 -2.11841248 -2.33228489
-4.2612372   1.70168184 -1.43939588 -6.99990663  6.31620968 -8.670818
  0.31602399 -7.41771206 -0.43653747 -3.65280196 -6.29314532 -1.24324829
  0.36481048  0.360924    0.66792657 -0.51624236 -0.01221781  0.0706137
  0.05850447  0.30488284  0.41801247  0.20885828 -0.34923131].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

是这行有什么问题嘛?辛苦各位大神帮忙看看啦~
lr.fit(x_train_data.iloc[indices[0],:],y_train_data.iloc[indices[0],:].values.ravel())
最佳答案
2019-12-11 16:23:58
维度不对,期待的是二维矩阵,而且都提示将其reshape下就行,至于哪个维度该是1,你自己跑的应该更明白

creditcard.zip

1.07 MB, 下载次数: 1

最佳答案

查看完整内容

维度不对,期待的是二维矩阵,而且都提示将其reshape下就行,至于哪个维度该是1,你自己跑的应该更明白
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-12-11 16:23:58 | 显示全部楼层    本楼为最佳答案   
维度不对,期待的是二维矩阵,而且都提示将其reshape下就行,至于哪个维度该是1,你自己跑的应该更明白
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-12-11 21:35:23 | 显示全部楼层
塔利班 发表于 2019-12-11 16:36
维度不对,期待的是二维矩阵,而且都提示将其reshape下就行,至于哪个维度该是1,你自己跑的应该更明白

加了个冒号,维度应该没问题了,可是又提示
ValueError: Found array with 0 sample(s) (shape=(0, 29)) while a minimum of 1 is required.
这又是怎么回事呢?
我查了下如果加填个print(y_train_data.iloc[indices[0]:,:].values.ravel())
他只有第一次有数据,后面循环得到的全都是[]
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2019-12-11 21:44:46 | 显示全部楼层
Astray.R 发表于 2019-12-11 21:35
加了个冒号,维度应该没问题了,可是又提示
ValueError: Found array with 0 sample(s) (shape=(0, 29)) ...

没时间跑你的数据,你把numpy数组搞明白就行,还有你的indices也搞明白
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2019-12-11 23:20:41 | 显示全部楼层
解决办法:
for iteration, indices in fold.split(X)改成
for iteration, indices in enumerate(fold.split(x_train_data), start=1)
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-16 19:29:10 | 显示全部楼层
X = data.ix[:, data.columns != 'Class']
y = data.ix[:, data.columns == 'Class']
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2021-3-16 19:29:41 | 显示全部楼层
X = data.ix[:, data.columns != 'Class']
y = data.ix[:, data.columns == 'Class']
这两段代码该怎么理解呢
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-6-26 11:34

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表