Fenny_Theo

<Type1>540_Model Building

Jul 22nd, 2021
873
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import pandas as pd
  2. import numpy as np
  3. import h5py
  4. import matplotlib
  5. import csv
  6. import matplotlib.pyplot as plt
  7. from keras.models import Model, Sequential
  8. from keras.layers import Dense, Input, concatenate,Conv1D, Conv2D, MaxPooling2D, Conv2DTranspose,MaxPooling1D, Cropping2D, Multiply, subtract, Flatten, Reshape, Permute, LSTM, TimeDistributed,Dropout,BatchNormalization,UpSampling1D
  9. from keras.optimizers import SGD
  10. from keras.callbacks import ModelCheckpoint,EarlyStopping
  11. import tensorflow as tf
  12. from keras.models import load_model
  13. from keras import optimizers
  14. from keras import regularizers
  15. from math import sqrt
  16. from sklearn.metrics import mean_squared_error
  17. from sklearn.metrics import mean_absolute_error
  18. import pickle
  19. from sklearn.preprocessing import StandardScaler
  20. import time
  21. from sklearn import preprocessing
  22. from numpy import argmax
  23. from keras.utils import to_categorical
  24. from tabulate import tabulate
  25. from numpy import array
  26. from sklearn.ensemble import RandomForestClassifier
  27. from sklearn.datasets import load_iris
  28. from sklearn.model_selection import train_test_split
  29. from sklearn.externals import joblib
  30.  
  31.  
  32. #读取csv文件
  33. filename="D:\\NCSU Research\\GEARS_Jul9\\GEARS\\540\\data.complete.csv"
  34. train=pd.read_csv(filename)
  35.  
  36. #建模
  37. #x_columns = [x for x in train.columns if x not in ["label_new"]]
  38. #X=train[x_columns]
  39. #y=train["label_new"]
  40.  
  41.  
  42.  
  43.  
  44. clf = RandomForestClassifier(n_estimators=100, criterion="entropy",oob_score=True) #oob:out of bag 通过袋外样本,可以评估这个树的准确度,估算算法的泛化能力(explanation of oob)
  45.  
  46. if __name__=="__main__":
  47.     x_columns = [x for x in train.columns if x not in ["label_new"]]
  48.     X=train[x_columns]
  49.     y=train["label_new"]
  50.  
  51.  
  52.     Xd_train, Xd_test, y_train, y_test = train_test_split(X, y,test_size=0.25)
  53.     clf = clf.fit(Xd_train, y_train)
  54.     y_predicted = clf.predict(Xd_test)    
  55.     accuracy = np.mean(y_predicted == y_test) * 100
  56.  
  57.     print ("y_test\n",y_test)
  58.     print ("y_predicted\n",y_predicted)
  59.     print ("accuracy:",accuracy)
  60.     print(clf.oob_score_)
  61.    
  62.     #joblib.dump(clf,"train_model_540.m") #preserve the model
  63.  
  64.     joblib.dump(clf,"train_model_540.m") #preserve the model
  65.  
  66.     #clf=joblib.load("train_model_540.m")
  67.  
  68.  
RAW Paste Data