본문 바로가기
Data Science/Tensorflow & Pytorch

[Tensorflow] 회귀(Regression) 신경망 실습

by 루크 Luke 2022. 1. 7.
반응형

본 실습은 Sklearn Boston 데이터 셋을 활용했습니다

 

1. Import Library and Random seed

import numpy as np
import tensorflow as tf
np.random.seed(0)
tf.random.set_seed(0)

2. Load Dataset and check target

# target check - continuous
from sklearn import datasets
raw_boston = datasets.load_boston()
X = raw_boston.data
y = raw_boston.target
print(X.shape)
print(set(y))

3. Train / Test Split

# Split
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=13)

4. Model design and fitting

# modeling
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense

n_feat = X_train.shape[1]
epo = 30
model = tf.keras.models.Sequential([
    tf.keras.layers.Dense(20, input_dim=n_feat, activation='relu'),
    tf.keras.layers.Dense(1)
])

# compile
model.compile(optimizer='adam',
             loss='mean_squared_error',
             metrics=['mean_squared_error'])
             
# fitting
hist = model.fit(X_train, y_train, epochs=epo, batch_size=5)

5. Evaluate and visualize

# evaluate
print(model.evaluate(X_train, y_train))
print(model.evaluate(X_test, y_test))

# Vis
import matplotlib.pyplot as plt
%matplotlib inline

plot_target = ['mean_squared_error']
plt.figure(figsize=(12, 8))
for each in plot_target:
    plt.plot(hist.history[each], label=each)
    
plt.legend()
plt.grid()
plt.show()

6. Real vs Predict visualize

# real vs predict
import pandas as pd
pred = model.predict(X_test).flatten()
result = pd.DataFrame(pred, columns=['pred'])
result['real'] = y_test
result_sort = result.sort_values(by='pred', ascending=True)
idx = np.arange(1, len(result)+1)
plt.scatter(idx, result_sort['real'], marker='o', label='real')
plt.plot(idx, result_sort['pred'], color='r', label='pred')
plt.xlabel('index')
plt.ylabel('value')
plt.show()

 

 

※ 장철원 저자의 <선형대수와 통계학으로 배우는 머신러닝 with 파이썬>을 참고했습니다.

반응형

댓글