0) ๋ฐ์ดํฐ ํ์ธ ๋ฐ ์ ์ฒ๋ฆฌ
# ๋ฐ์ดํฐ ๋ถ๋ฌ์ค๊ธฐ
import tensorflow as tf
boston_housing = tf.keras.datasets.boston_housing
(X_train, y_train), (X_test, y_test) = boston_housing.load_data()
# ๋ฐ์ดํฐ ์
shape ํ์ธ
X_train.shape
# ๋ฐ์ดํฐ ํน์ฑ scale ๋ง์ถ๊ธฐ
from sklearn.preprocessing import StandardScaler
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
1) ์ ๊ฒฝ๋ง ๋ชจ๋ธ
model = tf.keras.models.Sequential()
model.add(tf.keras.layers.Dense(32, activation='relu'))
model.add(tf.keras.layers.Dense(16, activation='relu'))
model.add(tf.keras.layers.Dense(1))
model.compile(optimizer='sgd',
loss='mean_squared_error',
metrics='mae')
model.fit(X_train_scaled, y_train, epochs=300)
model.evaluate(X_test_scaled, y_test, verbose=2)
2) ๋จธ์ ๋ฌ๋ ๋ชจ๋ธ
from sklearn.linear_model import LinearRegression
model2 = LinearRegression()
model2.fit(X_train_scaled, y_train)
y_pred_test = model2.predict(X_test_scaled)
from sklearn.metrics import mean_squared_error as mse, mean_absolute_error as mae, r2_score as r2
print(f'mse : {mse(y_test, y_pred_test)}')
print(f'mae : {mae(y_test, y_pred_test)}')
print(f'r2 : {r2(y_test, y_pred_test)}')
์ ๊ฒฝ๋ง ๋ฐ ๋จธ์ ๋ฌ๋ ๋ชจ๋ธ์ ์ ๋ง ๋จ์ํ๊ฒ ๊ตฌ์ฑํด๋ณด์๋๋ฐ, ์์ฃผ ํฐ ์ฐจ์ด๋ ์๋์ง๋ง ์ ๊ฒฝ๋ง์ ์ฑ๋ฅ์ด ์ฐ์ํ๊ฒ ๋์จ ๊ฒ์ ํ์ธํ ์ ์์์ต๋๋ค. :)
'๐ฟ Data > ์ด๋ชจ์ ๋ชจ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฅ๋ฌ๋]ํ์ดํผ ํ๋ผ๋ฏธํฐ ํ๋(sklearn์ RandomizedSearchCV, keras_tuner์ RandomSearch) (0) | 2022.03.01 |
---|---|
[๋ฅ๋ฌ๋]keras_cifar100 ์ด์ฉํ ๊ฐ๋จ ์ ๊ฒฝ๋ง ๋ฐ ๊ณผ์ ํฉ ๋ฐฉ์ง, ํ์ดํผํ๋ผ๋ฏธํฐ ํ๋ (0) | 2022.02.26 |
[๋ฅ๋ฌ๋]์ตํฐ๋ง์ด์ (Optimizer) (0) | 2022.02.24 |
[๋ฅ๋ฌ๋]์์ค ํจ์ (0) | 2022.02.23 |
[๋ฅ๋ฌ๋]์ญ์ ํ ์๊ณ ๋ฆฌ์ฆ (0) | 2022.02.23 |