Random Forests decision tree classifier is to use more common prediction, classification took the most votes that divide the data to predict the classification, not much else to say, the code is attached below,
# - * - Coding: UTF- 8 - * - Import numpy AS NP from sklearn Import Datasets, Ensemble from sklearn.model_selection Import train_test_split from sklearn.model_selection Import cross_val_score Import matplotlib.pyplot AS plt Import PANDAS AS pd from sklearn.metrics Import accuracy_score # here is how to import the local data as training data #data = pd.read_csv ( ' iris.data ' ) #Print (data.head ( . 5 )) #attributes = data [[ ' SL ' , 'SW ' , ' PL ' , ' PW ' ]] # property is a simplified front four SL, SW, PL, PW #types = Data [ ' type ' ] # 5 column attribute categories iris IRIS = datasets.load_iris () iris_X = iris.data iris_y = iris.target feature_names = iris.feature_names target_names = iris.feature_names #Print (iris_X [: . 4 ,:]) X_train, X_test, y_train, android.permission.FACTOR. = train_test_split (iris_X, iris_y, test_size = 0.3 ) Print ( "\ n training set sample size: " , X_train.shape) Print ( " training set size label: " , y_train.shape) Print ( " test set sample size: " , X_test.shape) Print ( " test set labels size: " , y_test.shape)
# cross-validation, classifier selection to see some results when the best generalization of the model, to select an optimal number of classifiers according to FIG. '' ' cv_scores = [] k_range = Range ( . 1 , 20 is ) for n- in k_range: CLF = ensemble.RandomForestClassifier (= n-MAX_DEPTH, n_estimators = . 1 , max_features = . 1 ) Scores= cross_val_score(clf,X_train,y_train,cv=10,scoring='accuracy') cv_scores.append(scores.mean()) plt.plot(k_range,cv_scores) plt.xlabel('K') plt.ylabel('Accuracy') plt.show() ''' #clf = ensemble.RandomForestClassifier(max_depth=5, n_estimators=1, max_features=1) #clf.fit(X_train,y_train) #print(knn.predict(X_test)) #print(clf.score(X_test,y_test)) #X=np.array(X_train) #Y=np.array(y_train) clf = ensemble.RandomForestClassifier(max_depth=9, n_estimators=1, max_features=1) clf.fit(iris_X,iris_y) #print(clf.score(X_train,y_train)) #print(clf.score(X_test,y_test)) print(clf.predict( [[5.1,3.5,1.4,0.2], [4.9,3.0,1.4,0.2], [5.7,3.0,4.2,1.2], [5.7,2.9,4.2,1.3], [5.7,2.8,4.1,1.3], [6.3,3.3,6.0,2.5], [5.8,2.7,5.1,1.9], [6.2,2.9,4.3,1.3], [5.1,2.5,3.0,1.1], [7.1,3.0,5.9,2.1], [6.3,2.9,5.6,1.8], [6.5,3.0,5.8,2.2], [4.7,3.2,1.3,0.2], [4.6,3.1,1.5,0.2]] ))