为什么在python中使用LSTM时没有显示预测值

为什么在python中使用LSTM时没有显示预测值,python,tensorflow,lstm,Python,Tensorflow,Lstm,首先,我编写了一个模型,用4个输入来训练LSTM模型。 这是我的密码: training_data= data8.iloc[:,1:] # read inputs data except datetime sc = MinMaxScaler(feature_range=(0, 1)) #scale all the input data into 0,1 train_data = sc.fit_transform(training_data) x_train = [] y_train =

首先,我编写了一个模型,用4个输入来训练LSTM模型。 这是我的密码:

training_data= data8.iloc[:,1:] # read inputs data except datetime
sc = MinMaxScaler(feature_range=(0, 1))  #scale all the input data into 0,1
train_data = sc.fit_transform(training_data)

 x_train = []
 y_train = []
   for i in range(3,len(training_data)): #predict future into 3 times
         x_train.append(train_data[i-3:i,3]) #create x_train value
         y_train.append(train_data[i,0])     #create y_train value
x_train, y_train = np.array(x_train), np.array(y_train) #convert into array
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1],1)) #reshape it for LSTM model, with whole data, 4(input) , output(1)


   model = Sequential() #create LSTM model
   model.add(LSTM(units=10, return_sequences=True, input_shape=(None,x_train.shape[1])))
   model.add(LSTM(units=10))
   model.add(Dense(units=1))
   model.compile(loss='mean_squared_error', optimizer='adam')
   model.fit(x_train, y_train, epochs=5, batch_size=32)
          dataset_test = pd.read_csv('data92.csv') #read second csv
          dataset_test = dataset_test.replace(np.nan, 0) #replace nan into 0
          inputs =dataset_test.iloc[:,1:].values #read 4 inputs

           inputs = inputs.reshape(-1,4) #reshape it

           inputs = sc.transform(inputs) #scale into 0,1
              for test in range(0,5):  #put it into range 5 not to read whole
                inputs=np.append(inputs,inputs[0])  
                inputs=inputs.reshape(-1,1)
        X_test = []
          for i in range(3,4):  #assume data will read in in this range
             X_test.append(inputs[test:i+test,:]) #create x_test


       X_test = np.array(X_test)

       X_test = np.reshape(X_test, (X_test.shape[0],X_test.shape[1],1)) #reshape it

      new_output = model.predict(X_test) #predict the value

     inputs=np.delete(inputs,len(inputs)-1,axis=0)
     inputs=np.append(inputs,new_output)
     inputs=inputs.reshape(-1,1)   #reshpae predict value into real value

    new= dataset_test.iloc[:,1].values #read first column
    plt.plot(new)
    plt.plot(new_output)
    plt.show()
然后我添加了新的csv文件来预测x1输入的值。这是我的密码:

training_data= data8.iloc[:,1:] # read inputs data except datetime
sc = MinMaxScaler(feature_range=(0, 1))  #scale all the input data into 0,1
train_data = sc.fit_transform(training_data)

 x_train = []
 y_train = []
   for i in range(3,len(training_data)): #predict future into 3 times
         x_train.append(train_data[i-3:i,3]) #create x_train value
         y_train.append(train_data[i,0])     #create y_train value
x_train, y_train = np.array(x_train), np.array(y_train) #convert into array
x_train = np.reshape(x_train, (x_train.shape[0], x_train.shape[1],1)) #reshape it for LSTM model, with whole data, 4(input) , output(1)


   model = Sequential() #create LSTM model
   model.add(LSTM(units=10, return_sequences=True, input_shape=(None,x_train.shape[1])))
   model.add(LSTM(units=10))
   model.add(Dense(units=1))
   model.compile(loss='mean_squared_error', optimizer='adam')
   model.fit(x_train, y_train, epochs=5, batch_size=32)
          dataset_test = pd.read_csv('data92.csv') #read second csv
          dataset_test = dataset_test.replace(np.nan, 0) #replace nan into 0
          inputs =dataset_test.iloc[:,1:].values #read 4 inputs

           inputs = inputs.reshape(-1,4) #reshape it

           inputs = sc.transform(inputs) #scale into 0,1
              for test in range(0,5):  #put it into range 5 not to read whole
                inputs=np.append(inputs,inputs[0])  
                inputs=inputs.reshape(-1,1)
        X_test = []
          for i in range(3,4):  #assume data will read in in this range
             X_test.append(inputs[test:i+test,:]) #create x_test


       X_test = np.array(X_test)

       X_test = np.reshape(X_test, (X_test.shape[0],X_test.shape[1],1)) #reshape it

      new_output = model.predict(X_test) #predict the value

     inputs=np.delete(inputs,len(inputs)-1,axis=0)
     inputs=np.append(inputs,new_output)
     inputs=inputs.reshape(-1,1)   #reshpae predict value into real value

    new= dataset_test.iloc[:,1].values #read first column
    plt.plot(new)
    plt.plot(new_output)
    plt.show()
当我绘制图表时,它只显示了我的真实价值。没有显示预测值。 我的图表:

列车模型的我的csv文件:

培训模型后,我的下一个csv文件将用于测试:


数据看起来如何?试着打印
new
new\u output
@Solvalou这里new value=63 104 93 177 133 70 83&我的new\u output=0.476632只显示了一个值,但这是有意义的,因为
X\u test
似乎也有一个长度。我猜你想要预测的数据有问题。但是你的代码对我来说很难理解,所以请先自己检查一下。@Solvalou好的,给我一秒钟,我会对我在代码中所做的一切进行注释,然后你就可以理解了it@Solvalou我评论了它,我希望现在你能理解我的代码