먼저 데이터를 전처리하고 모델링 한다.

import tensorflow.keras
from keras.models import Sequential
from keras.layers import Dense
from sklearn.preprocessing import MinMaxScaler

def build_model():
    model = Sequential()
    model.add(Dense(units = 5, activation = 'relu', input_shape = (5, )))
    model.add(Dense(units = 25, activation = 'relu'))
    model.add(Dense(units = 10, activation = 'relu'))
    model.add(Dense(units = 1, activation = 'linear'))
    model.compile('adam', 'mse')
    return model

model = build_model()

마지막 레이어는 활성함수를 linear로 해줘야하고 compile loss 는 mse로 정해준다

epoch_history = model.fit(X_train, y_train, epochs = 20, batch_size = 10)
y_pred = model.predict(X_test)

# mse 계산
((y_test - y_pred) ** 2).mean()

# 예측 데이터 되돌리기
sc_y.inverse_transform(y_pred)

모델 학습을 시키고 예측을 한뒤 mse 측정을 한다.

inverse_transform으로 원래 단위로 되돌릴 수 있다.

+ Recent posts