# CNN으로 구성
# 2차원을 4차원으로 늘여서 하시오.

import numpy as np

# 1. 데이터
from sklearn.datasets import load_breast_cancer
dataset = load_breast_cancer()
x = dataset.data
y = dataset.target

print(x.shape)      # (569, 30)
print(y.shape)      # (569,)

from tensorflow.keras.utils import to_categorical
y = to_categorical(y)
print(y.shape)      # (569, 2)

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=45)

from sklearn.preprocessing import MinMaxScaler
scaler = MinMaxScaler()
scaler.fit(x_train)
x_train = scaler.transform(x_train)
x_test = scaler.transform(x_test)

x_train = x_train.reshape(x_train.shape[0], x_train.shape[1], 1, 1)
x_test = x_test.reshape(x_test.shape[0], x_test.shape[1], 1, 1)

# 2. 모델
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Conv2D, Flatten, Dropout
model = Sequential()
model.add(Conv2D(filters=256, kernel_size=(2,2), padding='same', strides=1, input_shape=(30,1,1)))
model.add(Dropout(0.2))
model.add(Conv2D(128, (2,2), padding='same', strides=1))
model.add(Flatten())
model.add(Dense(64, activation='relu'))
model.add(Dense(64, activation='relu'))
model.add(Dense(2, activation='sigmoid'))

model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['acc'])
model.fit(x_train, y_train, epochs=100, batch_size=1, verbose=2)

loss, acc = model.evaluate(x_test, y_test)
print('loss :', loss)
print('acc: ', acc)

# result
# loss : 2.016068696975708
# acc:  0.9736841917037964

y_pred = model.predict(x_test)

for i in range(10,20):
    true = np.argmax(y_test[i])
    pred = np.argmax(y_pred[i])
    print('실제 :', true, '| 예측 :', pred)
