Advertisement
Guest User

Untitled

a guest
Aug 19th, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.71 KB | None | 0 0
  1. import numpy as np
  2. import pandas as pd
  3. import time
  4. from sklearn.ensemble import RandomForestClassifier
  5. from sklearn.externals import joblib
  6. import scipy.io as sio #存mat
  7.  
  8. start=time.clock()#开始计时
  9.  
  10. # x_4=joblib.load('reduced_x_4.pkl') #读取数据
  11. # x_5=joblib.load('reduced_x_5.pkl')
  12. x_4=joblib.load('x_4.pkl') #读取数据
  13. x_5=joblib.load('x_5.pkl')
  14. x_6=joblib.load('x_6.pkl')
  15. x_7=joblib.load('x_7.pkl')
  16. y_4=joblib.load('y_4.pkl')
  17. y_5=joblib.load('y_5.pkl')
  18. y_6=joblib.load('y_6.pkl')
  19.  
  20. xtrain=x_6
  21. ytrain=y_6[:,1]
  22. xtest=x_7
  23. # ytest=y_5[:,1]
  24.  
  25. #RF
  26. clf=RandomForestClassifier(n_estimators=50,max_depth=25)
  27. clf.fit(xtrain,ytrain) #.astype('float')强制把类型定义为float,不然dtype=object会报错
  28. prediction1=clf.predict_proba(xtest)
  29. prediction=np.zeros((prediction1.shape[0],1))
  30. for i in range(0,prediction1.shape[0]):
  31. if prediction1[i,1]>0.5:
  32. prediction[i,0]=1
  33. else:
  34. prediction[i,0]=0
  35.  
  36. # 准确率P和召回率R。真正例TP,假正例FP,真反例TN,假反例FN
  37. # TP,FP,FN=0,0,0
  38. # for i in range(len(prediction)):
  39. # if ytest[i]==1 and prediction[i]==1:
  40. # TP+=1
  41. # if ytest[i]==0 and prediction[i]==1:
  42. # FP+=1
  43. # if ytest[i]==1 and prediction[i]==0:
  44. # FN+=1
  45. # P=TP/(TP+FP)
  46. # R=TP/(TP+FN)
  47.  
  48. index=np.lexsort([-1*prediction1[:,1]]) #按第二列降序排序
  49. prediction2=prediction1[index,:]
  50. print("Time used:",time.clock()-start,'s')#计算时间
  51. # print('P=',P)
  52. # print('R=',R)
  53.  
  54. # '''存mat('文件名.mat', {'文件里变量名': 要存的变量名})'''
  55. # sio.savemat('x_4.mat', {'x_4': x_4})
  56. # sio.savemat('x_5.mat', {'x_5': x_5})
  57. # sio.savemat('y_4.mat', {'y_4': y_4})
  58. # sio.savemat('y_5.mat', {'y_5': y_5})
  59.  
  60. # np.savetxt("prediction_7.csv", prediction, delimiter = ',')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement