keras를 사용한 학습 방법이다.
* 필요 라이브러리 import
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.callbacks import EarlyStopping
* 위 라이브러리를 사용한 학습 모델 구성 (layer 쌓기)
Dense layer 4개로만 이루어진 간단한 모델을 쌓는다.
이전 단계에서 만들어둔 x_train의 개수가 57개이다. (아래 코드에서는 57 대신 shape의 크기를 가져왔다)
model = Sequential()
model.add(Dense(units=160, activation='relu', input_dim=int(x_train.shape[1])) )
model.add(Dense(units=60, activation='relu') )
model.add(Dense(units=20, activation='relu') )
model.add(Dense(units=1, activation='linear') )
>> 입력된 값으로부터 cnt라는 하나의 변수를 예측해야 하므로 가장 마지막 layer의 unit 수 = 1
* 학습 모델 compile
model.compile(loss='mae',optimizer='adam',metrics='mae']
early_stopping = EarlyStopping(monitor='loss',patience=5,mode='min')
# loss가 5번 올라가면 멈춘다. (overfitting 방지)
history = model.fit(x_train, y_train, epochs=50, batch_size=1, validation_split=0.1, callbacks=[early_stopping])
>>
* 학습 결과 시각화
plt.plot( history.history['val_loss'] )
plt.plot( history.history['loss'] )
plt.title( 'val_loss & loss' )
plt.xlabel('Epochs')
plt.ylabel('loss')
plt.legend(['val_loss','loss'])
plt.show()
>>
* 학습 모델 기반 예측 결과 출력
y_pred = model.predict(x_test)
* 평가 방법 정의 및 평가
from sklearn.metrics import mean_squared_error
def RMSE(y_test, y_pred):
return np.sqrt(mean_squared_error(y_test, y_pred)
>>
아주 큰 숫자가 나온다.ㅎㅎ
'개발 > python' 카테고리의 다른 글
Kaggle - London Bike Sharing (6) 머신러닝 기반 예측값 분석 (2) | 2023.10.05 |
---|---|
Kaggle - London Bike Sharing (5) 머신러닝 기반 학습,예측 (0) | 2023.10.03 |
Kaggle - London Bike Sharing (3): 데이터셋 split (0) | 2023.10.03 |
Kaggle - London Bike Sharing (2) 데이터 전처리:이상치 제거 (1) | 2023.10.03 |
Kaggle - London Bike Sharing (1) - 데이터 I/O, 데이터 형태 파악 (2) | 2023.10.03 |