Advertisement
Guest User

ml

a guest
Apr 23rd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. """
  3. Created on Mon Apr 23 17:07:08 2018
  4.  
  5. @author: twixs
  6. """
  7.  
  8. # -*- coding: utf-8 -*-
  9. """
  10. Spyder Editor
  11.  
  12. This is a temporary script file.
  13. """
  14.  
  15. import pandas as pd
  16. from sklearn.preprocessing import LabelEncoder
  17. from sklearn.preprocessing import StandardScaler
  18. from sklearn.ensemble import RandomForestClassifier
  19. from sklearn.model_selection import KFold
  20.  
  21.  
  22. data = pd.read_csv("C:\\Users\\twixs\\Desktop\\WS_MACHINE\\credit.csv")
  23.  
  24. def prepareDataForLearning(pandas_data, class_index):
  25. class_col = pandas_data.iloc[:, class_index]
  26. copy = pandas_data.copy(deep=True)
  27. copy = copy.drop(copy.columns[class_index], axis=1)
  28. return copy, class_col
  29.  
  30. #Split Data
  31. inputData, expectedResult = prepareDataForLearning(data,15)
  32.  
  33. #Labelling
  34. encoder = LabelEncoder()
  35. eOutput = encoder.fit_transform(expectedResult)
  36.  
  37. iData = inputData.apply(encoder.fit_transform)
  38.  
  39.  
  40. #Scaler
  41.  
  42. scaler = StandardScaler()
  43. scaler.fit(iData)
  44.  
  45. iData = scaler.transform(iData)
  46.  
  47.  
  48. #Kfold
  49.  
  50. nSamples = 10.0
  51.  
  52. kf=KFold(n_splits=int(nSamples))
  53.  
  54. #Create classifier
  55. meanAccuracy=0
  56.  
  57. for train_index, test_index in kf.split(iData):
  58.  
  59.  
  60. clf = RandomForestClassifier(n_estimators=1, n_jobs=1, random_state=0)
  61.  
  62. clf.fit(iData[train_index], eOutput[train_index])
  63.  
  64. acc=clf.score(iData[test_index],eOutput[test_index])
  65.  
  66. meanAccuracy += acc
  67.  
  68. print("Final Precision= ", (meanAccuracy/nSamples)*100)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement