1. 程式人生 > >機器學習:wine 分類

機器學習:wine 分類

算法 sub quad type 數據處理 線性判別分析 rain -s scl

數據來源:http://archive.ics.uci.edu/ml/datasets/Wine

參考文獻:《機器學習Python實戰》魏貞原

博文目的:復習

工具:Geany

#導入類庫

from pandas import read_csv #讀數據
from pandas.plotting import scatter_matrix #畫散點圖
from pandas import set_option #設置打印數據精確度

import numpy as np


import matplotlib.pyplot as plt #畫圖

from sklearn.preprocessing import Normalizer #數據預處理:歸一化
from sklearn.preprocessing import StandardScaler #數據預處理:正態化

from sklearn.preprocessing import MinMaxScaler #數據預處理:調整數據尺度

from sklearn.model_selection import train_test_split #分離數據集

from sklearn.model_selection import cross_val_score #計算算法準確度
from sklearn.model_selection import KFold #交叉驗證
from sklearn.model_selection import GridSearchCV #機器學習算法的參數優化方法:網格優化法

from sklearn.linear_model import LinearRegression #線性回歸
from sklearn.linear_model import Lasso #套索回歸
from sklearn.linear_model import ElasticNet #彈性網絡回歸
from sklearn.linear_model import LogisticRegression #邏輯回歸算法

from sklearn.discriminant_analysis import LinearDiscriminantAnalysis #線性判別分析
from sklearn.discriminant_analysis import QuadraticDiscriminantAnalysis #二次判別分析
from sklearn.tree import DecisionTreeRegressor #決策樹回歸
from sklearn.tree import DecisionTreeClassifier #決策樹分類

from sklearn.neighbors import KNeighborsRegressor #KNN回歸

from sklearn.neighbors import KNeighborsClassifier #KNN分類

from sklearn.naive_bayes import GaussianNB #貝葉斯分類器

from sklearn.svm import SVR #支持向量機 回歸
from sklearn.svm import SVC #支持向量機 分類

from sklearn.pipeline import Pipeline #pipeline能夠將從數據轉換到評估模型的整個機器學習流程進行自動化處理

from sklearn.ensemble import RandomForestRegressor #隨即森林回歸
from sklearn.ensemble import RandomForestClassifier #隨即森林分類
from sklearn.ensemble import GradientBoostingRegressor #隨即梯度上升回歸
from sklearn.ensemble import GradientBoostingClassifier #隨機梯度上分類
from sklearn.ensemble import ExtraTreesRegressor #極端樹回歸
from sklearn.ensemble import ExtraTreesClassifier #極端樹分類
from sklearn.ensemble import AdaBoostRegressor #AdaBoost回歸
from sklearn.ensemble import AdaBoostClassifier #AdaBoost分類

from sklearn.metrics import mean_squared_error #
from sklearn.metrics import accuracy_score #分類準確率

from sklearn.metrics import confusion_matrix #混淆矩陣

from sklearn.metrics import classification_report #分類報告


#導入數據
filename = 'wine.csv'
data = read_csv(filename, header=None, delimiter=',')
#數據理解
print(data.shape)
#print(data.dtypes)
#print(data.corr(method='pearson'))
#print(data.describe())
#print(data.groupby(0).size())


#數據可視化:直方圖、散點圖、密度圖、關系矩陣圖

#直方圖

#data.hist()
#plt.show()


#密度圖

#data.plot(kind='density', subplots=True, layout=(4,4), sharex=False, sharey=False)
#plt.show()


#散點圖

#scatter_matrix(data)
#plt.show()


#關系矩陣圖

#fig = plt.figure()
#ax = fig.add_subplot(111)
#cax = ax.matshow(data.corr(), vmin=-1, vmax=1)
#fig.colorbar(cax)
#plt.show()



#數據處理:調整數據尺度、歸一化、正態化、二值化
array = data.values
X = array[:, 1:14].astype(float)
Y = array[:,0]

scaler = MinMaxScaler(feature_range=(0,1)).fit(X)
X_m = scaler.transform(X)

scaler = Normalizer().fit(X)
X_n = scaler.transform(X)

scaler = StandardScaler().fit(X)
X_s = scaler.transform(X)

#分離數據集
validation_size = 0.2
seed = 7

X_train, X_test, Y_train, Y_test = train_test_split(X, Y, test_size=validation_size, random_state=seed)

X_m_train, X_m_test, Y_m_train, Y_m_test = train_test_split(X, Y, test_size=validation_size, random_state=seed)

X_n_train, X_n_test, Y_n_train, Y_n_test = train_test_split(X, Y, test_size=validation_size, random_state=seed)

X_s_train, X_s_test, Y_s_train, Y_s_test = train_test_split(X, Y, test_size=validation_size, random_state=seed)

#選擇模型:(本例是一個分類問題)
#非線性:KNN, SVC, CART, GaussianNB,
#線性:KNN, SVR, LR, Lasso, ElasticNet, LDA,
models = {}
models['KNN'] = KNeighborsClassifier()
models['SVM'] = SVC()
models['CART'] = DecisionTreeClassifier()
models['GN'] = GaussianNB()
#models['LR'] = LinearRegression()
#models['Lasso'] = Lasso()
#models['EN'] = ElasticNet()
models['LDA'] = LinearDiscriminantAnalysis()
models['QDA'] = QuadraticDiscriminantAnalysis()

#評估模型
scoring = 'accuracy'
num_folds = 10
seed = 7

results = []
for key in models:
kfold = KFold(n_splits=num_folds, random_state=seed)
cv_results =cross_val_score(models[key], X_train, Y_train, scoring=scoring, cv=kfold)
results.append(cv_results)
print('%s %f(%f)'%(key, cv_results.mean(), cv_results.std()))

results_m = []
for key in models:
kfold = KFold(n_splits=num_folds, random_state=seed)
cv_results_m =cross_val_score(models[key], X_m_train, Y_m_train, scoring=scoring, cv=kfold)
results_m.append(cv_results_m)
print('調整數據尺度:%s %f(%f)'%(key, cv_results_m.mean(), cv_results_m.std()))

results_n = []
for key in models:
kfold = KFold(n_splits=num_folds, random_state=seed)
cv_results_n =cross_val_score(models[key], X_n_train, Y_n_train, scoring=scoring, cv=kfold)
results_n.append(cv_results_n)
print('歸一化數據:%s %f(%f)'%(key, cv_results_n.mean(), cv_results_n.std()))

results_s = []
for key in models:
kfold = KFold(n_splits=num_folds, random_state=seed)
cv_results_s =cross_val_score(models[key], X_s_train, Y_s_train, scoring=scoring, cv=kfold)
results_s.append(cv_results_s)
print('正態化數據:%s %f(%f)'%(key, cv_results_s.mean(), cv_results_s.std()))
#箱線圖

#算法優化:LDA
param_grid = {'solver':['svd', 'lsqr', 'eigen']}
model = LinearDiscriminantAnalysis()
kfold = KFold(n_splits=num_folds, random_state=seed)
grid = GridSearchCV(estimator=model, param_grid=param_grid, scoring=scoring, cv=kfold)
grid_result = grid.fit(X=X_train, y=Y_train)
print('最優:%s 使用:%s'%(grid_result.best_score_, grid_result.best_params_))
cv_results = zip(grid_result.cv_results_['mean_test_score'], grid_result.cv_results_['std_test_score'], grid_result.cv_results_['params'])
for mean, std, params in cv_results:
print('%f(%f) with %r'%(mean, std, params))


#算法集成
#bagging: 隨機森林,極限樹;
#boosting:ada, 隨機梯度上升
ensembles = {}
ensembles['RF'] = RandomForestClassifier()
ensembles['ET'] = ExtraTreesClassifier()
ensembles['ADA'] = AdaBoostClassifier()
ensembles['GBM'] = GradientBoostingClassifier()

results = []
for key in ensembles:
kfold = KFold(n_splits=num_folds, random_state=seed)
cv_results =cross_val_score(ensembles[key], X_train, Y_train, scoring=scoring, cv=kfold)
results.append(cv_results)
print('%s %f(%f)'%(key, cv_results.mean(), cv_results.std()))

#集成算法調參gbm
param_grid = {'n_estimators':[10,50,100,200,300,400,500,600,700,800,900]}
model = GradientBoostingClassifier()
kfold = KFold(n_splits=num_folds, random_state=seed)
grid = GridSearchCV(estimator=model, param_grid=param_grid, cv=kfold, scoring=scoring)
grid_result = grid.fit(X=X_train, y=Y_train)
print('最優:%s 使用:%s'%(grid_result.best_score_, grid_result.best_params_))
cv_results = zip(grid_result.cv_results_['mean_test_score'], grid_result.cv_results_['std_test_score'], grid_result.cv_results_['params'])
for mean, std, params in cv_results:
print('%f(%f) with %r'%(mean, std, params))

#訓練最終模型
model = LinearDiscriminantAnalysis(solver='svd')
model.fit(X=X_train, y=Y_train)

#評估最終模型
predictions = model.predict(X_test)
print(accuracy_score(Y_test, predictions))
print(confusion_matrix(Y_test, predictions))
print(classification_report(Y_test, predictions))



機器學習:wine 分類