Home>

I want to label the elements of 4 numbers as A (1) or B (0) and classify the newly given 4 numbers as A or B.

Now, the following code works well until learning, but if you try to predict the data with four numbers called test.csv with predict () "ValueError: Error when checking input: expected dense_1_input to The error code "have shape (4,) but got array with shape (1,)" will appear.

By increasing the input data by 3 to form a 4x4 matrix, you can predict it.
I'm assuming that I'm going to input 4 data for each row, so I thought it would work with a 1x4 matrix, but I want to know why an error occurs.

* Add shape result *
The shape of each matrix is ​​as follows. Now, testdata is the data I want to predict and data is the teacher data.

>>>print (testdata.shape)
(Four,)
>>>print (data.shape)
(100, 4)
>>>

Input data

12.7404,15.3204,9.74263,13.086

Part of teacher data

9.60385,11.0032,9.32986,9.25753
9.37304,13.0028,11.0855,9.64098
8.40733,12.6233,10.2337,8.79376
8.73675,12.249,8.87519,7.89159
,
,
,
11.7207,14.0737,10.9952,9.37442
12.7404,16.0793,14.1397,12.5642
11.2507,15.38,12.9987,12.0801
11.9871,15.1148,11.6168,10.9482
11.0797,13.8117,9.74263,8.2056
11.6802,15.2681,12.8943,12.1861
import numpy as np
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Dense, Activation
data = np.loadtxt (fname = "CSV/teacher.csv", delimiter = ",")
testdata = np.loadtxt (fname = "CSV/test.csv", delimiter = ",")
labels = np.array (
    [1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 , 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 ,
    0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] )
labels = np_utils.to_categorical (labels)
model = Sequential ()
model.add (Dense (10, input_dim = 4))
model.add (Activation ('relu'))
model.add (Dense (2, activation = 'softmax'))
model.compile ('rmsprop', 'categorical_crossentropy', metrics = ['accuracy'])
model.fit (data, labels, epochs = 100, validation_split = 0.2)
test = model.predict (testdata)
print (test)
Trends