简体   繁体   English

如何重塑我的 nupy 数组,以在 Keras 中进行有效预测

[英]How to reshape my nupy array, to make a valid prediction in Keras

i trained my n-network, and everything work fine, except that i don't know how to format my data to make a prediction on data that are not in training and testing set.我训练了我的 n 网络,一切正常,除了我不知道如何格式化我的数据以对不在训练和测试集中的数据进行预测。

  • I loaded csv data.我加载了 csv 数据。
  • I split it into training and testing set, and everything fork fine for我把它分成训练集和测试集,一切都很好

    x_train, x_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.1, random_state=0) x_train, x_test, y_train, y_test = model_selection.train_test_split(X, y, test_size=0.1, random_state=0)

    \n\n

    bestmodel.fit(x_train, y_train, epochs=1, batch_size=5) bestmodel.fit(x_train, y_train, epochs=1, batch_size=5)

    i got like 97% acc.我得到了 97% 的acc。 For为了

    print(type(x_test)) print(x_test.dtype) print(x_test.shape)打印(类型(x_test))打印(x_test.dtype)打印(x_test.shape)

i have output like我有类似的输出

class 
'numpy.ndarray'
float64
(905, 14)

i made my own example,我做了我自己的例子,

 z = np.array([1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1]).astype(float)

    np.reshape(z, (14,)) 

but when i try但是当我尝试

bestmodel.predict(z)
i got error 我有错误
raceback (most recent call last): Raceback(最近一次通话):\n  File "/home/administrator/PycharmProjects/BankMarketinData/main.py", line 81, in文件“/home/administrator/PycharmProjects/BankMarketinData/main.py”,第 81 行,在 \n    main()主要的()\n  File "/home/administrator/PycharmProjects/BankMarketinData/main.py", line 76, in main文件“/home/administrator/PycharmProjects/BankMarketinData/main.py”, line 76, in main\n    score = bestmodel.predict(z)分数 = bestmodel.predict(z)\n  File "/home/administrator/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 1149, in predict文件“/home/administrator/anaconda3/lib/python3.6/site-packages/keras/engine/training.py”,第1149行,在预测中\n    x, _, _ = self._standardize_user_data(x) x, _, _ = self._standardize_user_data(x)\n  File "/home/administrator/anaconda3/lib/python3.6/site-packages/keras/engine/training.py", line 751, in _standardize_user_data文件“/home/administrator/anaconda3/lib/python3.6/site-packages/keras/engine/training.py”,第 751 行,在 _standardize_user_data\n    exception_prefix='input')异常前缀='输入')\n  File "/home/administrator/anaconda3/lib/python3.6/site-packages/keras/engine/training_utils.py", line 138, in standardize_input_data文件“/home/administrator/anaconda3/lib/python3.6/site-packages/keras/engine/training_utils.py”,第138行,在standardize_input_data\n    str(data_shape)) str(data_shape))\nValueError: Error when checking input: expected dense_1_input to have shape (14,) but got array with shape (1,) ValueError:检查输入时出错:预期dense_1_input 具有形状(14,) 但得到形状为(1,) 的数组

Can you help me reshape and format this z table, that i can use it for prediciton ?你能帮我重塑和格式化这个 z 表,我可以用它来预测吗?

You need to add the batch dimension with a value of 1:您需要添加值为 1 的批次维度:

z = np.array([1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 0, 1, 0, 1]).astype(float)
# z.shape is (14,)
z = np.expand_dims(z, axis=0)
# z.shape is now (1, 14)

bestmodel.predict(z)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM