预测下一个结果MLP神经网络Python

2024-06-24 11:46:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试预测数据集序列中的下一组数字,但是使用预测函数classifys对整个数据集进行分类,如何更改代码以便它预测序列中的下一个结果?你知道吗

我是按照这个教程和他的模型输出80基于50,60,70作为他的数据集。但我的只是预测整个数据集?How to Get Started with Deep Learning for Time Series Forecasting (7-Day Mini-Course)

这是我的数据集

enter image description here

代码如下:

import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
from sklearn.metrics import classification_report,confusion_matrix

from numpy import loadtxt
from keras.models import Sequential
from keras.layers import Dense
# load the dataset
col_names = ['N1', 'N2', 'N3', 'N4', 'N5', 'L1', 'L2','label']
# load dataset 
pima = pd.read_csv("dataset.csv", header=None, names=col_names)
pima.head()

feature_cols = ['N1', 'N2', 'N3', 'N4', 'N5', 'L1', 'L2']
X = pima[feature_cols] # Features
y = pima.label 

model = Sequential()
model.add(Dense(122, input_dim=7, activation='relu'))
model.add(Dense(20, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])
model.fit(X, y, epochs=2000, batch_size=10)
# =======================
_, accuracy = model.evaluate(X, y)
print('Accuracy: %.2f' % (accuracy*100))

yhat = model.predict(X, verbose=0)
print(yhat) <- this outputs the predictions for the entire dataset and not the next prediction

编辑:

我得到的结果是,对于整个数据集。你知道吗

enter image description here

数据集从1到1251行运行,我想预测1252行的输出为N1、N2、N3、N4、N5、L1、L2。你知道吗


Tags: the数据fromimportmodelnamessklearndataset
1条回答
网友
1楼 · 发布于 2024-06-24 11:46:06

最后一层是sigmoid激活,它的输出范围是[-1,1],这不是你想要的最后一层,因为你在预测下一个数字。你知道吗

model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

尝试将此更改为

model.add(Dense(1))
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

另外,对于这类任务最好使用MSE loss函数,BCE更多的是用于分类任务,您可以在这里检查一下

Why is the Cross Entropy method preferred over Mean Squared Error? In what cases does this doesn't hold up?

model.compile(loss='mse', optimizer='adam', metrics=['accuracy'])

希望这能解决你的问题

相关问题 更多 >