I'm thinking of classifying two classes at keras on CNN.Here's the code.
import random
import pandas aspd
from pandas import Series, DataFrame
import numpy as np
import matplotlib.pyplot asplt
import keras
from keras.datasets import fashion_mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten, Dropout,
Conv2D, MaxPooling2D, Reshape
from keras.optimizers import RMSprop
from keras.optimizers import Adam
df_list = [ ]
for i in range (4):
cl_data_set=pd.read_csv("./csv/cl_"+str(i)+".csv", sep=",",", header=0)
cl_x = DataFrame(cl_data_set.drop("POINT",axis=1))
df_list.append(cl_x)
op_data_set=pd.read_csv("./csv/op_"+str(i)+".csv", sep=",",", header=0)
op_x = DataFrame(op_data_set.drop("POINT",axis=1))
df_list.append(op_x)
close_1 = np.array(df_list[0])
open_1 = np.array(df_list[1])
close_2=np.array(df_list[2])
open_2=np.array(df_list[3])
close_3 = np.array(df_list[4])
open_3 = np.array(df_list[5])
close_4 = np.array(df_list[6])
open_4 = np.array(df_list[7])
train_dataset=[ ]
test_dataset=[ ]
for i in range (0,13000,100):
if i>=10400:
test_dataset.append ([close_1[i:i+100,:], 1])
test_dataset.append ([close_2[i:i+100,:], 1])
test_dataset.append ([open_1[i:i+100,:], 0])
test_dataset.append ([open_2[i:i+100,:], 0])
else:
train_dataset.append([close_1[i:i+100,:], 1])
train_dataset.append ([close_2[i:i+100,:], 1])
train_dataset.append ([open_1[i:i+100,:], 0])
train_dataset.append ([open_2[i:i+100,:], 0])
random.shuffle(train_dataset)
random.shuffle(test_dataset)
x_train_list = [ ]
y_train_list = [ ]
x_test_list = [ ]
y_test_list = [ ]
for i in range (416):
x_train_list.append (train_dataset[i][0])
y_train_list.append(train_dataset[i][1])
for i in range (104):
x_test_list.append (test_dataset[i][0])
y_test_list.append(test_dataset[i][1])
x_train=np.array(x_train_list)
x_test=np.array(x_test_list)
y_train=np.array(y_train_list)
y_test=np.array(y_test_list)
model=Sequential()
x_train=np.reshape(x_train, (-1,416,100,4))
model.add(Conv2D(32, kernel_size=(3,3), activation='relu', input_shape=(416,100,4))))
model.add(MaxPooling2D(pool_size=(1,2))))
model.add(Dropout(0.4))
model.add(Flatten())
model.add(Dense(128, activation='sigmoid'))
model.add(Dropout(0.4))
model.add(Dense(1, activation='sigmoid'))
model.summary()
print("\n")
model.compile(loss='binary_crossentropy', optimizer="Adam", metrics=['accuracy'])
history=model.fit(x_train,y_train,batch_size=200,epochs=2000,verbose=1,validation_data=(x_test,y_test))
score=model.validate(x_test,y_test,verbose=1)
print("\n")
print("Test loss:", score[0])
print("Test acuity:", score[1])
Next, list the full list of errors
Using TensorFlow backend.
x_train.shape
(416, 100, 4)
x_test.shape
(104, 100, 4)
y_train.shape
(416,)
y_test.shape
(104,)
Traceback (most recent call last):
File "CNN.py", line 121, in<module>
history=model.fit(x_train,y_train,batch_size=200,epochs=2000,verbose=1,validation_data=(x_test,y_test))
File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 950, infit
batch_size=batch_size)
File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training.py", line 802, in_standardize_user_data
check_array_length_consistency(x,y,sample_weights)
File "C:\Anaconda3\envs\tensorflow\lib\site-packages\keras\engine\training_utils.py", line 236, check_array_length_consistency
'and'+str(list(set_y)[0])+'target samples.')
ValueError: Input arrays should have the same number of samples as target arrays. Found 1 input samples and 416 target samples.
The total number of samples is 520 divided by train:test=8:2 and
x has 4ch data divided by 100 points and y has a label (0 or 1).
Please help me resolve the error and correct it as necessary
Just like x_train, I think y_train can be fixed by creating a dimension of the number of data.
x_train=np.reshape(x_train, (-1,416,100,4))#=>Do the same for y_train:
y_train=np.reshape(y_train, (-1,416))
© 2024 OneMinuteCode. All rights reserved.