5-3 神经网络算法预测销量高低(python3.6下运行)
直接参考博文,https://blog.****.net/appleyuchi/article/details/71057012
其他类似博文:https://www.cnblogs.com/1138720556Gary/p/10040710.html
https://blog.****.net/weixin_41276745/article/details/79669529
直接复制代码运行,报错:
ValueError: ('Some keys in session_kwargs are not supported at this time: %s', dict_keys(['class_mode']))
解决办法:
代码中去掉
class_mode='binary'
即在3.6版本中完整代码为:
# -*- coding: utf-8 -*-
import pandas as pd
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
inputfile = '../data/sales_data.xls'
data = pd.read_excel(inputfile, index_col=u'序号')
data[data == u'好'] = 1
data[data == u'是'] = 1
data[data == u'高'] = 1
data[data != 1] = 0
x = data.iloc[:, :3].as_matrix().astype(int)
y = data.iloc[:, 3].as_matrix().astype(int)
from keras.models import Sequential
from keras.layers.core import Dense, Activation
model = Sequential()
# model.add(Dense(input_dim = 3, output_dim = 10))
model.add(Dense(units=10, input_dim=3))
model.add(Activation('relu'))
# model.add(Dense(input_dim = 10, output_dim = 1))
model.add(Dense(input_dim=10, units=1))
model.add(Activation('sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam')
model.fit(x, y, epochs=1000, batch_size=10)
yp = model.predict_classes(x).reshape(len(y))
def cm_plot(y, yp):
from sklearn.metrics import confusion_matrix #导入混淆矩阵函数
cm = confusion_matrix(y, yp) #混淆矩阵
import matplotlib.pyplot as plt #导入作图库
plt.matshow(cm, cmap=plt.cm.Greens) #画混淆矩阵图,配色风格使用cm.Greens,更多风格请参考官网。
plt.colorbar() #颜色标签
for x in range(len(cm)): #数据标签
for y in range(len(cm)):
plt.annotate(cm[x,y], xy=(x, y), horizontalalignment='center', verticalalignment='center')
plt.ylabel('True label') #坐标轴标签
plt.xlabel('Predicted label') #坐标轴标签
return plt
cm_plot(y, yp).show()
运行结果为: