13-1什么是集成学习
Notbook 示例
Notbook 源码
1 集成学习 2 [1] 3 import numpy as np 4 import matplotlib.pyplot as plt 5 [2] 6 from sklearn import datasets 7 X, y = datasets.make_moons(n_samples=500,noise=0.3, random_state=42) 8 [3] 9 plt.scatter(X[y==0,0], X[y==0,1]) 10 plt.scatter(X[y==1,0], X[y==1,1]) 11 <matplotlib.collections.PathCollection at 0x2337fd4a160> 12 13 [4] 14 from sklearn.model_selection import train_test_split 15 16 X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42) 17 [5] 18 from sklearn.linear_model import LogisticRegression 19 20 log_clf = LogisticRegression() 21 log_clf.fit(X_train, y_train) 22 log_clf.score(X_test, y_test) 23 0.864 24 [6] 25 from sklearn.svm import SVC 26 27 svm_clf = SVC() 28 svm_clf.fit(X_train, y_train) 29 svm_clf.score(X_test, y_test) 30 0.896 31 [7] 32 from sklearn.tree import DecisionTreeClassifier 33 34 dt_clf = DecisionTreeClassifier() 35 dt_clf.fit(X_train, y_train) 36 dt_clf.score(X_test, y_test) 37 0.84 38 [8] 39 y_predict1 = log_clf.predict(X_test) 40 y_predict2 = svm_clf.predict(X_test) 41 y_predict3 = dt_clf.predict(X_test) 42 [9] 43 y_predict = np.array( (y_predict1 + y_predict2 + y_predict3) >= 2, dtype='int') 44 [10] 45 y_predict[:10] 46 array([1, 0, 0, 1, 1, 1, 0, 0, 0, 0]) 47 [11] 48 from sklearn.metrics import accuracy_score 49 50 accuracy_score(y_test, y_predict) 51 0.904 52 Voting Classifier 53 [12] 54 from sklearn.ensemble import VotingClassifier 55 56 voting_clf = VotingClassifier(estimators=[ 57 ('log_clf', LogisticRegression()), 58 ('svm_clf',SVC()), 59 ('dt_clf', DecisionTreeClassifier()) 60 ],voting='hard') 61 [13] 62 voting_clf.fit(X_train, y_train) 63 VotingClassifier(estimators=[('log_clf', LogisticRegression()), 64 ('svm_clf', SVC()), 65 ('dt_clf', DecisionTreeClassifier())]) 66 [14] 67 voting_clf.score(X_test,y_test) 68 0.912
13-2 SoftVoting Classifier
Notbook 示例
Notbook 源码
1 Soft Voting 2 [1] 3 import numpy as np 4 import matplotlib.pyplot as plt 5 [2] 6 from sklearn import datasets 7 X, y = datasets.make_moons(n_samples=500,noise=0.3, random_state=42) 8 [3] 9 plt.scatter(X[y==0,0], X[y==0,1]) 10 plt.scatter(X[y==1,0], X[y==1,1]) 11 <matplotlib.collections.PathCollection at 0x1dbd04d9190> 12 13 [4] 14 from sklearn.model_selection import train_test_split 15 16 X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42) 17 Hard Voting Classifier 18 [5] 19 from sklearn.linear_model import LogisticRegression 20 from sklearn.svm import SVC 21 from sklearn.tree import DecisionTreeClassifier 22 from sklearn.ensemble import VotingClassifier 23 24 voting_clf = VotingClassifier(estimators=[ 25 ('log_clf', LogisticRegression()), 26 ('svm_clf',SVC()), 27 ('dt_clf', DecisionTreeClassifier()) 28 ],voting='hard') 29 [6] 30 voting_clf.fit(X_train, y_train) 31 voting_clf.score(X_test,y_test) 32 0.912 33 Soft Voting Classifier 34 [7] 35 voting_clf2 = VotingClassifier(estimators=[ 36 ('log_clf', LogisticRegression()), 37 ('svm_clf',SVC(probability=True)), 38 ('dt_clf', DecisionTreeClassifier()) 39 ],voting='soft') 40 [8] 41 voting_clf2.fit(X_train, y_train) 42 voting_clf2.score(X_test,y_test) 43 0.92
标签:集成,13,clf,train,随机,test,import,voting,sklearn From: https://www.cnblogs.com/Cai-Gbro/p/16855753.html