#调整随机森林的参数(调整max_features,结果未见明显差异) from sklearn import datasets X, y = datasets.make_classification(n_samples=10000,n_features=20,n_informative=15,flip_y=.5, weights=[.2, .8]) import numpy as np training = np.random.choice([True, False], p=[.8, .2],size=y.shape) from sklearn.ensemble import RandomForestClassifier rf = RandomForestClassifier() rf.fit(X[training], y[training]) preds = rf.predict(X[~training]) print ("Accuracy:\t", (preds == y[~training]).mean()) from sklearn.metrics import confusion_matrix max_feature_params = [‘auto‘, ‘sqrt‘, ‘log2‘, .01, .5, .99] confusion_matrixes = {} for max_feature in max_feature_params: rf = RandomForestClassifier(max_features=max_feature) rf.fit(X[training], y[training]) print ("Accuracy:\t", (preds == y[~training]).mean()) confusion_matrixes= confusion_matrix(y[~training],rf.predict(X[~training])) print(max_feature,confusion_matrixes) print(‘--------------------------------------------------------------------‘) from sklearn.metrics import confusion_matrix y_true = [2, 0, 2, 2, 0, 1] y_pred = [0, 0, 2, 2, 0, 2] print(confusion_matrix(y_true, y_pred)) y_true = ["cat", "ant", "cat", "cat", "ant", "bird"] y_pred = ["ant", "ant", "cat", "cat", "ant", "cat"] print(confusion_matrix(y_true, y_pred, labels=["ant", "bird", "cat"])) ‘‘‘ Accuracy: 0.640324214792 Accuracy: 0.640324214792 auto [[278 403] [306 987]] -------------------------------------------------------------------- Accuracy: 0.640324214792 sqrt [[280 401] [324 969]] -------------------------------------------------------------------- Accuracy: 0.640324214792 log2 [[304 377] [320 973]] -------------------------------------------------------------------- Accuracy: 0.640324214792 0.01 [[285 396] [324 969]] -------------------------------------------------------------------- Accuracy: 0.640324214792 0.5 [[289 392] [305 988]] -------------------------------------------------------------------- Accuracy: 0.640324214792 0.99 [[294 387] [295 998]] -------------------------------------------------------------------- [[2 0 0] [0 0 1] [1 0 2]] [[2 0 0] [0 0 1] [1 0 2]] ‘‘‘
时间: 2024-11-08 23:05:12