import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn import preprocessing
from sklearn.model_selection import train_test_split,GridSearchCV
from sklearn.feature_selection import SelectFromModel
from sklearn.ensemble import ExtraTreesClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import LinearSVC
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.metrics import classification_report,confusion_matrix
dataset = datasets.load_iris()
features,labels= dataset.feature_names,list(dataset.target_names)
X,y = dataset.data,dataset.target
scaler = preprocessing.StandardScaler()
x = scaler.fit_transform(X)
#特征选择
clf = ExtraTreesClassifier(n_estimators=10,criterion='gini',random_state=1)
clf.fit(x,y)
print('基于树的特征打分结果如下:',clf.feature_importances_)
model = SelectFromModel(clf,prefit=True)
result = model.transform(x)
index = []
for col in range(x.shape[1]):
if x[0,col] in list(result[0]):
index.append(col)
print('基于树的特征选择结果:',index)
clf = LogisticRegression(penalty='l1',dual=False,C=0.1,random_state=1)
clf.fit(x,y)
print('基于Logistic特征打分结果如下:',clf.coef_[0])
model = SelectFromModel(clf,prefit=True)
result = model.transform(x)
index = []
for col in range(x.shape[1]):
if x[0,col] in list(result[0]):
index.append(col)
print('基于Logistic的特征选择结果:',index)
clf = LinearSVC(penalty='l1',dual=False,C=0.1,random_state=1)
clf.fit(x,y)
print('基于SVM特征打分结果如下:',clf.coef_[0])
model = SelectFromModel(clf,prefit=True)
result = model.transform(x)
index = []
for col in range(x.shape[1]):
if x[0,col] in list(result[0]):
index.append(col)
print('基于LinearSVC的特征选择结果:',index)
x_train,x_test,y_train,y_test = train_test_split(x,y,test_size=0.3,random_state=1)
#优化参数
params = {
'learning_rate':[0.0001,0.001,0.01,0.1],
'n_estimators':range(100,200)
}
model = GridSearchCV(GradientBoostingClassifier(loss='deviance'),params)
model.fit(x_train,y_train)
print('最佳模型为:\n',model.best_estimator_)
print('最佳参数为:\n',model.best_params_)
...
LR=0.0001,n_estimators=189
...
clf = GradientBoostingClassifier(loss='deviance',learning_rate=0.0001,n_estimators=189)
clf.fit(x_train,y_train)
print('----------------训练集----------------')
train_predict = clf.predict(x_train)
print(classification_report(y_train,train_predict,target_names=labels))
print(pd.DataFrame(confusion_matrix(y_train,train_predict)))
print('----------------测试集----------------')
test_predict = clf.predict(x_test)
print(classification_report(y_test,test_predict,target_names=labels))
print(pd.DataFrame(confusion_matrix(y_test,test_predict)))