天天看點

Python機器學習—BP神經網絡模型預測西瓜好壞

import pandas as pd
from sklearn.neural_network import MLPClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import classification_report
## 讀取資料
data = pd.read_csv('data.csv', encoding='gbk')
## 将target變為數字
data.loc[data['好瓜與否']!= '是','好瓜與否'] = 0
data.loc[data['好瓜與否']== '是','好瓜與否'] = 1
data['好瓜與否'] = data['好瓜與否'].astype('int')
## 取出X和y
X = pd.get_dummies(data.iloc[:,1:-1]).values
y = data.iloc[:,-1].values
## 切割資料集
X_train,X_test,y_train,y_test = train_test_split(X,y,train_size=0.8,random_state=125)
## 模組化并預測
BPNet = MLPClassifier(random_state=123)
BPNet.fit(X_train,y_train)
y_pred = BPNet.predict(X_test)
#print(y_test,y_pred)
# #輸出預測結果報告
print('預測報告為:\n',classification_report(y_test,y_pred))
           

西瓜資料集:

編号,色澤,根蒂,敲聲,紋理,臍部,觸感,密度,含糖率,好瓜與否

1,青綠,蜷縮,濁響,清晰,凹陷,硬滑,0.697,0.46,是

2,烏黑,蜷縮,沉悶,清晰,凹陷,硬滑,0.774,0.376,是

3,烏黑,蜷縮,濁響,清晰,凹陷,硬滑,0.634,0.264,是

4,青綠,蜷縮,沉悶,清晰,凹陷,硬滑,0.608,0.318,是

5,淺白,蜷縮,濁響,清晰,凹陷,硬滑,0.556,0.215,是

6,青綠,稍蜷,濁響,清晰,稍凹,軟粘,0.403,0.237,是

7,烏黑,稍蜷,濁響,稍糊,稍凹,軟粘,0.481,0.149,是

8,烏黑,稍蜷,濁響,清晰,稍凹,硬滑,0.437,0.211,是

9,烏黑,稍蜷,沉悶,稍糊,稍凹,硬滑,0.666,0.091,否

10,青綠,硬挺,清脆,清晰,平坦,軟粘,0.243,0.267,否

11,淺白,硬挺,清脆,模糊,平坦,硬滑,0.245,0.057,否

12,淺白,蜷縮,濁響,模糊,平坦,軟粘,0.343,0.099,否

13,青綠,稍蜷,濁響,稍糊,凹陷,硬滑,0.639,0.161,否

14,淺白,稍蜷,沉悶,稍糊,凹陷,硬滑,0.657,0.198,否

15,烏黑,稍蜷,濁響,清晰,稍凹,軟粘,0.36,0.37,否

16,淺白,蜷縮,濁響,模糊,平坦,硬滑,0.593,0.042,否

17,青綠,蜷縮,沉悶,稍糊,稍凹,硬滑,0.719,0.103,否

繼續閱讀