#keras搭建神经网络
import sklearn
from keras.models import Sequential
from keras.layers import Dense,Activation
from keras.optimizers import SGD
import numpy as np
from sklearn.datasets import load_iris
iris=load_iris()
x=iris.data
y=iris.target
print(y)
#进行结果的标签化处理one-hot处理
from sklearn.preprocessing import LabelBinarizer
print(LabelBinarizer().fit_transform(y))
#进行数据的可视化处理
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y)
y_train1=LabelBinarizer().fit_transform(y_train)
y_test1=LabelBinarizer().fit_transform(y_test) #分类结果标签处理
print(x.shape,x_train.shape)
print(y_train)
model=Sequential(
[
Dense(5,input_dim=4), #输入层为4个输入结果,隐含层为5个节点
Activation("relu"), #激活函数为relu函数
Dense(3), #输出层为3个节点
Activation("sigmoid"), #激活函数为sigmoid函数
]
)
sgd=SGD(lr=0.01,decay=1e-6,momentum=0.9,nesterov=True) #lr因子,步长,实质因子
model.compile(optimizer=sgd,loss="categorical_crossentropy") #损失函数为cross
model.fit(x_train,y_train1,nb_epoch=300,batch_size=80) #训练200轮,每次取40个数字
print(model.predict_classes(x_test))
y_pre=model.predict_classes(x_test)
print(sklearn.metrics.accuracy_score(y_test,y_pre))