修改了代码
#库的导入 import numpy as np import pandas as pd import math from tensorflow.keras.losses import BinaryCrossentropy losscc = BinaryCrossentropy() #小波基函数 def wavelet(x): return (math.cos(1.75*x)) * (np.exp((x**2)/(-2))) #激活函数tanh def tanh(x): return (np.exp(x)-np.exp(-x))/(np.exp(x)+np.exp(-x)) #数据输入 data=pd.read_pickle('ICC_rms.pkl') df=pd.DataFrame(data) X = df.iloc[:, 0:510].values #所有样本的x值,0-510列 矩阵(1544,510)由此得出样本个数1544个,特征510 y = df.iloc[:, 511].values #所有样本的标签,511列 矩阵(1544,) #把y转成1-0形式,Neurons对应0,Astrocytes对应1 Y=np.array([-1.0] * 1544) for i in range(len(y)): if y[i] =='Neurons': Y[i]=0 if y[i] =='Astrocytes': Y[i]=1 #输入数据的导入,用于测试数据的归一化与返归一化 df = pd.read_csv("train.csv") df.columns = ["Co", "Cr", "Mg", "Pb", "Ti"] Co = df["Co"] Co = np.array(Co) Cr = df["Cr"] Cr = np.array(Cr) Mg=df["Mg"] Mg=np.array(Mg) Pb = df["Pb"] Pb =np.array(Pb) Ti = df["Ti"] Tisample = np.array(Ti) sampleinsample = np.mat([Co,Cr,Mg,Pb]) Ti = np.array(Y) samplein = np.mat(X.T) sampleinminmax = np.array([samplein.min(axis=1).T.tolist()[0],samplein.max(axis=1).T.tolist()[0]]).transpose()#对应最大值最小值 sampleout = np.mat([Ti]) sampleoutminmax = np.array([sampleout.min(axis=1).T.tolist()[0],sampleout.max(axis=1).T.tolist()[0]]).transpose()#对应最大值最小值 #导入WNN.py训练好的参数 w1=np.load('w1.npy') b=np.load('b.npy') a=np.load('a.npy') w2=np.load('w2.npy') w1 = np.mat(w1) w2 = np.mat(w2) b = np.mat(b) a = np.mat(a) #隐含层节点数 hiddenunitnum = 8 #测试数据数量 testnum = 24 #测试数据的导入 df = pd.read_csv("test.csv") df.columns = ["Co", "Cr", "Mg", "Pb", "Ti"] Co = df["Co"] Co = np.array(Co) Cr = df["Cr"] Cr = np.array(Cr) Mg=df["Mg"] Mg=np.array(Mg) Pb = df["Pb"] Pb =np.array(Pb) Ti = df["Ti"] # Ti = np.array(Ti) # input=np.mat([Co,Cr,Mg,Pb]) Ti = np.array(Y) # inputtest = np.mat(X.T) inputtest=np.mat(X) #测试数据中输入数据的归一化 # inputnorm=(np.array(input.T)-sampleinminmax.transpose()[0])/(sampleinminmax.transpose()[1]-sampleinminmax.transpose()[0]) #hidden_out2用于保存隐含层输出 hidden_out = np.mat(np.zeros((testnum,hiddenunitnum))) #计算隐含层输出 for m in range(testnum): for j in range(hiddenunitnum): # d = ((inputnorm[m, :] * w1[j, :].T) - b[j, :]) * (a[j, :] ** (-1)) d = ((inputtest[m, :] * w1[j, :].T) - b[j, :]) * (a[j, :] ** (-1)) hidden_out[m, j] = wavelet(d) #计算输出层输出 output = tanh(hidden_out * w2 ) #对输出结果进行反归一化 # diff = sampleoutminmax[:,1]-sampleoutminmax[:,0] # networkout2 = output*diff+sampleoutminmax[0][0] networkout2 = output+sampleoutminmax[0][0] networkout2 = np.array(networkout2).transpose() output1=networkout2.flatten()#降成一维数组 output1=output1.tolist() for i in range(testnum): output1[i] = float('%.2f'%output1[i]) print("the prediction is:",output1) # #将输出结果与真实值进行对比,计算误差 # output=Ti # rmse = (np.sum(np.square(output-output1))/len(output)) ** 0.5 # mae = np.sum(np.abs(output-output1))/len(output) # #average_loss1=np.sum(np.abs((output-output1)/output))/len(output) # average_loss1=losscc(output,output1) # mape="%.2f%%"%(average_loss1*100) # f1 = 0 # for m in range(testnum): # f1 = f1 + np.abs(output[m]-output1[m])/((np.abs(output[m])+np.abs(output1[m]))/2) # f2 = f1 / testnum # smape="%.2f%%"%(f2*100) # print("the MAE is :",mae) # print("the RMSE is :",rmse) # print("the MAPE is :",mape) # print("the SMAPE is :",smape) #计算预测值与真实值误差与真实值之比的分布 A=0 B=0 C=0 D=0 E=0 for m in range(testnum): y1 = np.abs(output[m]-output1[m])/np.abs(output[m]) if y1 <= 0.1: A = A + 1 elif y1 > 0.1 and y1 <= 0.2: B = B + 1 elif y1 > 0.2 and y1 <= 0.3: C = C + 1 elif y1 > 0.3 and y1 <= 0.4: D = D + 1 else: E = E + 1 print("Ratio <= 0.1 :",A) print("0.1< Ratio <= 0.2 :",B) print("0.2< Ratio <= 0.3 :",C) print("0.3< Ratio <= 0.4 :",D) print("Ratio > 0.4 :",E)
修改了inputtest并且去掉了归一化,但是还是不对,预测值全变成了0
the prediction is: [0.0, 0.0, 0.0, 0.0, -0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.0, 0.0, -0.0, 0.0, -0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.0, -0.0, 0.0]
标签:接上,df,0.0,预测值,output1,np,test,output,array From: https://www.cnblogs.com/jiezstudy/p/17569645.html