在Python(Keras)中使用ANN进行多元回归

2024-04-24 19:43:52 发布

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

Python希望使用Python包中的prediction来执行任务。对于每个观察,我有4个(连续的)自变量(my X)和1个(连续的)因变量(my y)的测量值。y通常是-10到+10之间的值,尽管在这个范围之外的数据中也有y的测量值。当我运行下面的代码(以及后来的一些代码在测试集上生成预测)时,ANN预测的是相同的输出,而不管它提供了什么样的x值(当然,当提供一组具有不同值的因变量时,我确实希望模型给出不同的预测)。所讨论的数据集是关于高尔夫球的,我有大约。450000次观察(x-y对)。我正试图预测下一轮比赛中球员的表现。在

显然,我不需要使用ANN来实现这一点,但我想探索它的功能。在

这是我的代码:

# Importing the Keras libraries and packages
import keras
from keras.models import Sequential
from keras.layers import Dense

X = dataset.iloc[:, 3:7].values
y = dataset.iloc[:, 13].values

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

# Initialising the ANN
classifier = Sequential()

# Adding the input layer and the first hidden layer
classifier.add(Dense(units = 6, kernel_initializer = 'uniform', activation = 'relu', input_dim = 4))

# Adding the second hidden layer
classifier.add(Dense(units = 3, kernel_initializer = 'uniform', activation = 'relu'))

# Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform'))

# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'mse', metrics = ['ame'])

# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 10, epochs = 5)

Tags: andthe代码fromtestimportlayertrain