用神经网络模型,预测红酒质量;
后又用KNN逻辑回归SVM模型试了试,准确率都差不多,神经网络稍高。原始数据格式如下:

用神经网络模型,预测红酒质量-冯金伟博客园

 最后输出如下:

神经网络模型的预测准确率是: 0.755
KNN模型的预测准确率是:0.7275
LogicRe模型的预测准确率是:0.7325
SVM模型的预测准确率是:0.7425

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler
from sklearn.neural_network import MLPClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.linear_model import LogisticRegression
from sklearn.svm import SVC
import seaborn as sns
import matplotlib.pyplot as plt

pd.set_option('expand_frame_repr', False)  # 当列太多时不换行
pd.set_option('display.max_rows', 500)  # 最多显示数据的行数

file_path = './data/wine_quality.csv'

if __name__ == '__main__':
    data_df = pd.read_csv(file_path)
    all_cols = data_df.columns.tolist()    # 巧妙的取出了所有列名,并转化为list
    feat_cols = all_cols[:-1]

    # 看看quality值各有多少个
    # sns.countplot(data_df['quality'])    # 这句跟下面一句等价
    # sns.countplot(data=data_df, x='quality')
    # plt.show()

    # 对quality列进行处理,原来若干种分类变为0、1两种分类
    data_df.loc[data_df['quality'] <= 5,'quality'] = 0
    data_df.loc[data_df['quality'] >= 6,'quality'] = 1
    # sns.countplot(data=data_df, x='quality')
    # plt.show()

    X = data_df[feat_cols]
    y = data_df['quality']

    # 对特征值进行归一化
    scaler = MinMaxScaler()
    X_process = scaler.fit_transform(X)

    X_train,X_test,y_train,y_test = train_test_split(X_process,y,test_size=0.25,random_state=10)

    # 神经网络模型;隐藏层也不是越多越好;max_iter设置太小会有警告(没达到最优),max_iter默认200;random_state设置后每次运行结果一样。
    mlp_model = MLPClassifier(hidden_layer_sizes=(100,100),max_iter=1000,activation='relu',random_state=17)
    mlp_model.fit(X_train,y_train)
    accuracy = mlp_model.score(X_test,y_test)
    print('神经网络模型的预测准确率是:',accuracy)

    # KNN逻辑回归SVM模型试试
    model_dict = {
        'KNN': KNeighborsClassifier(n_neighbors=3),
        'LogicRe': LogisticRegression(C=1e3, solver='liblinear', multi_class='auto'),
        'SVM': SVC(C=1e3, gamma='auto')  # C值越小表示越强的正则化,也就是更弱复杂度;C值默认为1.0
    }
    for model_name,model in model_dict.items():
        model.fit(X_train,y_train)
        acc = model.score(X_test,y_test)
        print('{}模型的预测准确率是:{}'.format(model_name,acc))