我正在做一些练习神经网络,并且只停留在一个问题上。我的网络无法预测出正确的结果,即使它说训练得分为97%。
这是我的代码:
# Import `datasets` from `sklearn`
from sklearn import datasets
import pandas as pd
from sklearn.model_selection import train_test_split
# Import `train_test_split`
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.neural_network import MLPClassifier
# Load in the `digits` data
from sklearn.preprocessing import scale
iris = datasets.load_iris()
# split the data up - 3/4 for training, 1/4 for testing
data_train, data_test, name_train, name_test = train_test_split(iris.data,
iris.target, test_size=0.25, random_state=0)
# Number of training features
# n_samples, n_features = data_train.shape
scaler = StandardScaler()
scaler.fit(data_train)
params_train_scaled = scaler.transform(data_train)
params_test_scaled = scaler.transform(data_test)
# 1 hidden layer, same size as the input layer
mlp = MLPClassifier(
solver='lbfgs',
hidden_layer_sizes=(iris.data.shape[1], ),
random_state=0)
mlp.fit(params_train_scaled, name_train)
print(name_train)
print('Train score: %.3g' % mlp.score(params_train_scaled, name_train))
print('Test Score: %.3g' % mlp.score(params_test_scaled, name_test))
print
test_val = [[5.1, 3.5, 1.4, 0.2]]
print(mlp.predict(test_val))
我想这就是我如何扩展训练和测试数据的方法,但是我不确定...
我得到的输出是:
火车成绩:1
测试成绩:0.974
但是,预测值应为0,而不是1。
感谢您的帮助。
答案 0 :(得分:4)
由于您缩放了训练数据,因此还应该缩放测试数据:
user_id|sub_category_id|category_id|is_removed
5 |201 |2 |false