#keras搭建神經網絡import sklearnfrom keras.models import Sequentialfrom keras.layers import Dense,Activationfrom keras.optimizers import SGDimport numpy as npfrom sklearn.datasets import load_irisiris=load_iris()x=iris.datay=iris.targetprint(y)#進行結果的標籤化處理one-hot處理from sklearn.preprocessing import LabelBinarizerprint(LabelBinarizer().fit_transform(y))#進行數據的可視化處理from sklearn.model_selection import train_test_splitx_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") #損失函數爲crossmodel.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))