1模型融合
1.1载入数据
import pandas as pd
data = 'pima-indians-diabetes.data.csv'
names = ['preg' , 'plas' , 'pres' , 'skin' ,'test' , 'mass' , 'pedi', 'age' ,'class']
df = pd.read_csv(data , names = names)
df.head()
1
df['class'].unique()
2
1.2 投票器模型融合(VotingClassifier)
from sklearn import model_selection
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.svm import SVC
from sklearn.ensemble import VotingClassifier
import warnings
warnings.filterwarnings('ignore')
df.head(2)
3
array = df.values
array
4
X = array[:,0:8]
Y = array[:,8]
kfold = model_selection.KFold(n_splits=5, random_state=0)
# 创建投票器的子模型
estimators = []
model_1 = LogisticRegression()
estimators.append(('logistic', model_1))
model_2 = DecisionTreeClassifier()
estimators.append(('dt', model_2))
model_3 = SVC()
estimators.append(('svm', model_3))
# 构建投票器融合
ensemble = VotingClassifier(estimators)
result = model_selection.cross_val_score(ensemble, X, Y, cv=kfold)
print(result.mean())
5
1.3 Bagging
from sklearn.ensamble import BaggingClassifier
dt = DecisionTreeClassifier()
num = 100
kfold = model_selection.KFold(n_split = 5 ,random_state = 0)
model = BaggingClassifier(base_estimator = dt , n_estimators = num , random_state = 0)
result = model_selection.cross_val_score(model , X ,Y , cv = kfold)
print(result.mean())
6
1.4RandomForest
from sklearn.ensemble import RandomForestClassifier
num_trees = 100
max_feature_num = 5
kfold = model_selection.KFold(n_splits=5, random_state=2018)
model = RandomForestClassifier(n_estimators=num_trees, max_features=max_feature_num)
result = model_selection.cross_val_score(model, X, Y, cv=kfold)
print(result.mean())
7
1.5 Adaboost
from sklearn.ensemble import AdaBoostClassifier
num_trees = 25
kfold = model_selection.KFold(n_splits=5, random_state=2018)
model = AdaBoostClassifier(n_estimators=num_trees, random_state=2018)
result = model_selection.cross_val_score(model, X, Y, cv=kfold)
print(result.mean())
8
网友评论