多层感知机(Muti-Layer perceptron)
# 1.数据导入
import pandas as pd
names=[
"age",
"sex",
"cp",
"trestbps",
"chol",
"fbs",
"restecg",
"thalach",
"exang",
"oldpeak",
"slope",
"ca",
"thal",
"num"
]
heart_data= pd.read_csv(r"C:\Users\鹰\Desktop\ML_Set\heart+disease\processed.cleveland.data", names=names)
# 2.basic data precorssing---数据基本处理
# handing missing values---缺失值,缺失值处理
import numpy as np
heart_data=heart_data.replace("?", np.nan)
print(heart_data.isna().sum())
heart_data=heart_data.dropna()
print(heart_data.isna().sum())
# define x and y---确定特征值和目标值
x_all=heart_data.drop('num', axis=1)
y_all=heart_data.iloc[:, -1]
# plt.expend((passed,failed),('passed','failed'))
# plt.show()
# data split---数据集分割
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(x_all, y_all, test_size=0.33,random_state=10)
# 3.feature engineering---特征工程
# feature preprocessing--特征预处理--标准化
from sklearn.preprocessing import StandardScaler
scaler=StandardScaler()
x_train=scaler.fit_transform(x_train)
x_test= scaler.fit_transform(x_test)
# 特征提取和特征降维暂时不需要考虑
# 4.model training --模型训练
from keras.models import Sequential
mlp=Sequential()
from keras.layers import Dense, Activation
mlp.add(Dense(units=10000, input_dim=13, activation='sigmoid'))
mlp.add(Dense(units=8000, activation='sigmoid'))
mlp.add(Dense(units=1,activation='sigmoid'))
mlp.summary()
# compile the model
mlp.compile(optimizer='adam', loss='binary_crossentropy')
mlp.summary()
# 5.make prediction and calculate
from sklearn.metrics import accuracy_score
# 5.1.根据训练数据进行预测和评估
# 使用模型进行预测并得到每个类别的概率
y_train_predict_proba = mlp.predict(x_train)
# 将概率转换为类别标签
y_train_predict = np.argmax(y_train_predict_proba, axis=1)
# 计算准确率
accuracy_score1=accuracy_score(y_train, y_train_predict)
print(accuracy_score1)
# 5.2.根据测试集进行预测和评估
y_test_predict_pro=mlp.predict(x_test)
y_test_predict=np.argmax(y_test_predict_pro, axis=1)
accuracy_score2=accuracy_score(y_test, y_test_predict)
print(accuracy_score2)
下载链接:
官网下载
地址1:
https://archive.ics.uci.edu/dataset/45/heart+disease
百度网盘下载
地址2:
链接:https://pan.baidu.com/s/1VvgwXN5UHgWO5KxxKaeozA
提取码:8onf
愿各位前途坦荡
标签:Heart,heart,predict,集对,感知机,mlp,train,test,data From: https://blog.csdn.net/JR521314/article/details/143063869