Www.future-fisheries.jp



VII-2-3-3.NNMによる判別分析(keras)VII-2-3-3-i.準備とデータの読み込み#必要なライブラリーの読み込みimport pandas as pdimport numpy as npimport matplotlib.pyplot as plt%matplotlib inline#データの読み込みdf =pd.read_csv("sample3.csv")xn,D=df.shapeK=5 #クラスの数入力D=D-K #説明変数の数M=D # 第1層の細胞の数X=np.zeros((xn,D))T=np.zeros((xn,K))for i in range(D): X[:,i]=df.iloc[:,K+i]for i in range(K): T[:,i]=df.iloc[:,i]X_range0=[-2,2] #項目1の範囲X_range1=[-2,2] #項目2の範囲#データをテストデータとトレーニング用のデータに分割して保存TrainingRatio=1X_n_training=int(xn*TrainingRatio)X_train=X[:X_n_training,:]X_test=X[X_n_training:,:]T_train=T[:X_n_training,:]T_test=T[X_n_training:,:]VII-2-3-3-ii.データ分布の確認import matplotlib.pyplot as plt%matplotlib inlinedef show_data(x,t): wk,N=t.shape col=["b","r","g","y","w"] for k in range (K): plt.plot(x[t[:,k]==1,0],x[t[:,k]==1,1],linestyle='none',marker='o',markeredgecolor='black',color=col[k],alpha=0.8) plt.grid(True)#動作plt.figure(1,figsize=(8,3.7))plt.subplot(1,2,1)show_data(X_train,T_train)plt.xlim(X_range0)plt.ylim(X_range1)plt.title('Training data')plt.subplot(1,2,2)show_data(X_test,T_test)plt.xlim(X_range0)plt.ylim(X_range1)plt.title('Test data')plt.show()VII-2-3-3-ii.Kerasの導入#Kerasの導入import numpy as npimport matplotlib.pyplot as pltimport timenp.random.seed(1) #乱数を固定import keras.optimizers #Kerasを読み込むfrom keras.models import Sequential #入力1、出力1のFNNモデルfrom keras.layers.core import Dense, Activation #全結合型の中間層をつくるVII-2-3-3—iv.モデルの作成と実行startTime=time.time#Sequantialモデルの作成model=Sequential()model.add(Dense(M,input_dim=M,activation='sigmoid',kernel_initializer='uniform')) #第一層にM細胞、入力2次元、活性化シグモイド、初期値一様model.add(Dense(K,activation='softmax',kernel_initializer='uniform')) #第二層にK細胞sgd=keras.optimizers.SGD(lr=0.5,momentum=0.0,decay=0.0,nesterov=False) #学習方法の設定。lr学習率pile(optimizer=sgd,loss='categorical_crossentropy',metrics=['accuracy']) #sgdをoptimizerに渡す。交差エントロピー誤差が目的関数#実行startTime=time.time()history=model.fit(X_train,T_train,epochs=1000,batch_size=100,verbose=0,validation_data=(X_test,T_test)) #1000回試行でフィッティング#modelの評価score=model.evaluate(X_train,T_train,verbose=0) #score[0]は交差エントロピー誤差、score[1]は正答率accuracyprint('cross entropy {0:.2f},accuracy {1:.2f}'.format(score[0],score[1]))calculation_time=time.time()-startTimeprint("Calculation time:{0:.3f} sec".format(calculation_time))#結果の保存#modelの保存model.save("sample2w.h5")VII-2-3-3-v.経緯と結果の表示plt.figure(1,figsize=(12,3))plt.subplots_adjust(wspace=0.5)#学習曲線plt.subplot(1,3,1)plt.plot(history.history['loss'],'black',label='training') #trainingデータの交差エントロピー誤差の変化plt.legend()#等高線表示plt.subplot(1,3,2)show_data(X_test,T_test)xn=60 #解像度x0=np.linspace(X_range0[0],X_range0[1],xn)x1=np.linspace(X_range1[0],X_range1[1],xn)xx0,xx1=np.meshgrid(x0,x1)A=xx0.reshape(xn*xn,1)B=xx1.reshape(xn*xn,1)x=np.c_[A,B]p=model.predict(x)for k in range(K): f=p[:,k] f=f.reshape(xn,xn) f=f.T cont=plt.contour(xx1,xx0,f,levels=[0.5,0.95],colors=['cornflowerblue','black']) cont.clabel(fmt='%.2f',fontsize=9) plt.xlim(X_range0) plt.ylim(X_range1)#散布図との重ね書きplt.subplot(1,3,3)show_data(X_train,T_train)xn=60 #解像度x0=np.linspace(X_range0[0],X_range0[1],xn)x1=np.linspace(X_range1[0],X_range1[1],xn)xx0,xx1=np.meshgrid(x0,x1)A=xx0.reshape(xn*xn,1)B=xx1.reshape(xn*xn,1)x=np.c_[A,B]p=model.predict(x)for k in range(K): f=p[:,k] f=f.reshape(xn,xn) f=f.T cont=plt.contour(xx1,xx0,f,levels=[0.5,0.95],colors=['cornflowerblue','black']) cont.clabel(fmt='%.2f',fontsize=9) plt.xlim(X_range0) plt.ylim(X_range1)plt.show()VII-2-3-3-vi.拡大図import matplotlibmatplotlib.use('Agg')import matplotlib.pyplot as pltplt.figure(1,figsize=(4,4))show_data(X_train,T_train)xn=60 #解像度x0=np.linspace(X_range0[0],X_range0[1],xn)x1=np.linspace(X_range1[0],X_range1[1],xn)xx0,xx1=np.meshgrid(x0,x1)A=xx0.reshape(xn*xn,1)B=xx1.reshape(xn*xn,1)x=np.c_[A,B]p=model.predict(x)for k in range(K): f=p[:,k] f=f.reshape(xn,xn) f=f.T cont=plt.contour(xx1,xx0,f,levels=[0.5,0.95],colors=['cornflowerblue','black']) cont.clabel(fmt='%.2f',fontsize=9) plt.xlim(X_range0) plt.ylim(X_range1)plt.show()plt.savefig("kerasfig,png")VII-2-3-3-vii.係数の取得from tensorflow.python.keras.models import load_modelmodel=load_model("sample2w.h5")model.summary()weights=model.load_weights("sample2w.h5")l1=model.layers[0] #第1層のデータ取得Ax=(l1.get_weights()[0]) #変数の係数A0=(l1.get_weights()[1]) #定数項(ダミー変数の係数)l2=model.layers[1] #第1層のデータ取得Bx=(l2.get_weights()[0]) #変数の係数B0=(l2.get_weights()[1]) #定数項(ダミー変数の係数)#係数の出力のimport pandas as pdimport csvI,J=Ax.shapeJ,K=Bx.shapeA0=A0.TAx=Ax.TB0=B0.TBx=Bx.TAA=np.zeros((J,I+1))BB=np.zeros((K,J+1))AA[:,0]=A0[:]AA[:,1:]=Ax[:,:]BB[:,0]=B0[:]BB[:,1:]=Bx[:,:]print(AA)print(BB)VII-2-3-3-viii.係数の保存df=pd.DataFrame(AA)df.to_csv('parameter1Ake.csv')df=pd.DataFrame(BB)df.to_csv('parameter1Bke.csv')VII-2-3-3-ix.判別(予測)df=pd.read_csv("data.csv")p=model.predict(df)print(p)np.savetxt("prediction1.csv",p,delimiter=',',fmt='%.4f') ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download