728x90
반응형

# 교차검증 // 과대적합 방지
# hold out 검증 : 데이터셋 무작위 선택,
# kfold 검증 : 데이터 k개 그룸으로 분리하고 1개는 검증 나머지는 학습

from sklearn.model_selection import KFold
from tensorflow.keras.datasets.boston_housing import load_data
from tensorflow.keras.layers import Dense
from sklearn.model_selection import KFold
import numpy as np
(x_train, y_train), (x_test, y_test) = load_data(path='boston_housing.npz', test_split=0.2, seed=777)

 

# 데이터 표준화
import numpy as np
# 평균
mean = np.mean(x_train, axis = 0)
# 표준편차
std = np.std(x_train, axis = 0) # 표준편차값
# 표준화값
x_train = (x_train - mean) / std
x_test = (x_test - mean) / std

 

k = 3
kfold = KFold(n_splits = k)
def get_model() :
    model = Sequential()
    model.add(Dense(64, activation = 'relu', input_shape = (13,)))
    model.add(Dense(32, activation = 'relu'))
    model.add(Dense(1)) # 출력값 1개 # 시그모이드하면 안됨 0~1이니깐 
    model.compile(optimizer = 'adam', loss = 'mse', metrics = ['mae'])
    return model

 

mae_list = []
for train_index, val_index in kfold.split(x_train) :
    x_train_fold, x_val_fold = x_train[train_index], x_train[val_index]
    y_train_fold, y_val_fold = y_train[train_index], y_train[val_index]
    model = get_model()
    model.fit(x_train_fold, y_train_fold, epochs = 300, validation_data = (x_val_fold, y_val_fold))
    # 평가 : 손실함수값, 평가기준값
    _, test_mae = model.evaluate(x_test, y_test)
    mae_list.append(test_mae)

 

model.evaluate(x_test, y_test)

4/4 [==============================] - 0s 1ms/step - loss: 9.3774 - mae: 2.1469
[9.377416610717773, 2.14687442779541]

 

mae_list

# [2.11413836479187, 2.008519411087036, 2.14687442779541]

 

# 최종결과
print('결과 :', np.mean(mae_list))

# 결과 : 2.089844067891439

 

# 산점도를 이용하여 실제 주택가격과 예측가격 시각화하기
results = model.predict(x_test)
results[:10]

array([[21.987972],
       [21.056334],
       [47.57793 ],
       [22.31767 ],
       [12.485436],
       [33.702785],
       [26.14967 ],
       [29.55312 ],
       [26.651386],
       [24.07104 ]], dtype=float32)

 

import matplotlib.pyplot as plt
plt.figure(figsize = (5,5))
plt.plot(y_test, results, 'b.')
plt.plot([min(y_test), max(y_test)], [min(results), max(results)], ls='--', c='.3')
plt.xlabel('y_test')
plt.ylabel('results')
plt.show()

 

반응형

+ Recent posts