|
| 1 | +# 树模型 |
| 2 | + |
| 3 | +本节选用随机森林树模型进行演示,也可尝试sklearn和GBDT模型,以及xgboost模型,lightgbm模型 树模型的基础知识请参照周志华的《机器学习》以及李航的《统计学习方法》,另外代码可以参照机器学习实战 树模型最核心的就是信息熵,基尼指数等叶子结点得分,这是树模型分裂的依据,请着重研究~ 想偷懒的同学,社区的连接在此,看看吧 |
| 4 | + |
| 5 | +https://github.com/apachecn/MachineLearning/blob/master/docs/7.集成方法-随机森林和AdaBoost.md |
| 6 | + |
| 7 | +顺便说一下,嫌慢的同学还是用lightgbm吧,这个超快的~ GridSearchCV是用来调参的,只需要把注释去掉就可以显示出最佳的参数,不过实测慢的要死。。 |
| 8 | + |
| 9 | + |
| 10 | +> 代码: |
| 11 | +
|
| 12 | +```python |
| 13 | +from sklearn.ensemble import RandomForestClassifier |
| 14 | +from sklearn.model_selection import train_test_split |
| 15 | +from sklearn.decomposition import PCA |
| 16 | +import pandas as pd |
| 17 | +import numpy as np |
| 18 | +# from sklearn.grid_search import GridSearchCV |
| 19 | +# from numpy import arange |
| 20 | +# from lightgbm import LGBMClassifier |
| 21 | + |
| 22 | +train_data=pd.read_csv(r"C:\Users\312\Desktop\digit-recognizer\train.csv") |
| 23 | +test_data=pd.read_csv(r"C:\Users\312\Desktop\digit-recognizer\test.csv") |
| 24 | +data=pd.concat([train_data,test_data],axis=0).reset_index(drop=True) |
| 25 | +data.drop(['label'],axis=1,inplace=True) |
| 26 | +label=train_data.label |
| 27 | + |
| 28 | + |
| 29 | +pca=PCA(n_components=100, random_state=34) |
| 30 | +data_pca=pca.fit_transform(data) |
| 31 | + |
| 32 | +Xtrain,Ytrain,xtest,ytest=train_test_split(data_pca[0:len(train_data)],label,test_size=0.1, random_state=34) |
| 33 | + |
| 34 | + |
| 35 | +clf=RandomForestClassifier(n_estimators=110,max_depth=5,min_samples_split=2, min_samples_leaf=1,random_state=34) |
| 36 | + |
| 37 | +# clf=LGBMClassifier(num_leaves=63, max_depth=7, n_estimators=80, n_jobs=20) |
| 38 | + |
| 39 | +# param_test1 = {'n_estimators':arange(10,150,10),'max_depth':arange(1,11,1)} |
| 40 | +# gsearch1 = GridSearchCV(estimator = clf, param_grid = param_test1, scoring='accuracy',iid=False,cv=5) |
| 41 | +# gsearch1.fit(Xtrain,xtest) |
| 42 | +# print(gsearch1.grid_scores_, gsearch1.best_params_, gsearch1.best_score_) |
| 43 | + |
| 44 | + |
| 45 | +clf.fit(Xtrain,xtest) |
| 46 | +y_predict=clf.predict(Ytrain) |
| 47 | + |
| 48 | + |
| 49 | +zeroLable=ytest-y_predict |
| 50 | +rightCount=0 |
| 51 | +for i in range(len(zeroLable)): |
| 52 | + if list(zeroLable)[i]==0: |
| 53 | + rightCount+=1 |
| 54 | +print ('the right rate is:',float(rightCount)/len(zeroLable)) |
| 55 | + |
| 56 | + |
| 57 | +result=clf.predict(data_pca[len(train_data):]) |
| 58 | + |
| 59 | +i=0 |
| 60 | +fw = open("C:\\Users\\312\\Desktop\\digit-recognizer\\result.csv", 'w') |
| 61 | +with open('C:\\Users\\312\\Desktop\\digit-recognizer\\sample_submission.csv') as pred_file: |
| 62 | + fw.write('{},{}\n'.format('ImageId', 'Label')) |
| 63 | + for line in pred_file.readlines()[1:]: |
| 64 | + splits = line.strip().split(',') |
| 65 | + fw.write('{},{}\n'.format(splits[0],result[i])) |
| 66 | + i+=1 |
| 67 | +``` |
| 68 | + |
| 69 | +> 结果: |
| 70 | +
|
| 71 | +``` |
| 72 | +the right rate is: 0.819047619047619 |
| 73 | +``` |
0 commit comments