<jupyter_start><jupyter_text>딥-러닝 과정 MLP   Keras 모델 생성/학습 - 보스턴 집값 예측 모델![](https://3qeqpr26caki16dnhd19sv6by6v-wpengine.netdna-ssl.com/wp-content/uploads/2016/06/Regression-Tutorial-with-Keras-Deep-Learning-Library-in-Python.jpg)* CRIM: per capita crime rate by town  * ZN: proportion of residential land zoned for lots over 25,000 sq.ft.* INDUS: proportion of non-retail business acres per town.* CHAS: Charles River dummy variable (1 if tract bounds river; 0 otherwise)* NOX: nitric oxides concentration (parts per 10 million)* RM: average number of rooms per dwelling* AGE: proportion of owner-occupied units built prior to 1940* DIS: weighted distances to five Boston employment centres* RAD: index of accessibility to radial highways* TAX: full-value property-tax rate per \$10,000* PTRATIO: pupil-teacher ratio by town* B: 1000(Bk — 0.63)² where Bk is the proportion of blacks by town* LSTAT: % lower status of the population* MEDV: Median value of owner-occupied homes in $1000’s  딥러닝을 이용하여 문제 해결 해보기<jupyter_code># 1. 데이터 불러오기
import pandas as pd
import numpy as np
import seaborn as sns
from matplotlib import pyplot as plt

from sklearn.model_selection import train_test_split
df= pd.read_csv('BostonHousingData.csv')
df
df.info()
-df.describe()
df.isnull().sum()
plt.figure(figsize=(7,7))
sns.heatmap(data = df.corr(), annot=True, 
fmt = '.2f', linewidths=.5, cmap='Blues')<jupyter_output><empty_output><jupyter_text>TAX와 RAD 는 상관관계가 높아서 하나는 빼주기로하고 describe에서 확인했을때, 0이 있던 컬럼을 빼주어 박스 플롯으로 시각화 하였는데위에서 다시 확인한 결과, 'CHAS'는 0,1 카테고리형으로 나타나짐.<jupyter_code>plt.rcParams['figure.figsize'] = [10, 6]

%matplotlib inline

fig, ax = plt.subplots()
ax.boxplot([df['ZN'], df['CHAS']],sym="b*")

plt.show()

#그래서 안쓰는 컬럼을 우선 하나 drop 해보자. 
df = df.drop('RAD', axis=1)
df

y = df[['MEDV']]
y
# 2. Data 정규화
from sklearn.preprocessing import MinMaxScaler

scaler = MinMaxScaler()

df[:] = scaler.fit_transform(df[:])

# x,y 분류 
X = df.drop('MEDV', axis=1)

X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.2,random_state=100)

X_val, X_test, y_val, y_test = train_test_split(X,y, test_size=0.5, random_state=100)
from tensorflow import keras
from tensorflow.keras import layers
from tensorflow.keras.layers import Dense, Dropout
# 3. MLP 모델 생성
model = keras.Sequential()

model.add(layers.Dense(input_dim =12, units=128,kernel_regularizer = keras.regularizers.L2(0.1)))
model.add(layers.Activation('relu'))
#kernel_regularizer = keras.regularizers.L2(0.1)
# model.add(kernel_regularizer = keras.regularizers.L2(0.1))

model.add(layers.Dense(units=512))
model.add(layers.Activation('relu'))
model.add(layers.Dropout(0.2))  # 앞에있는 layer에 영향이 간다. 

model.add(layers.Dense(units=128))
model.add(layers.Activation('relu'))


model.add(layers.Dense(units=1))
model.add(layers.Activation('linear'))
model.summary()
# 4. Compile - Optimizer, Loss function 설정
from keras.callbacks import ModelCheckpoint
adam= keras.optimizers.Adam(lr=0.1)
model.compile(loss='mean_squared_error', optimizer='Adam', metrics=['mae'])<jupyter_output><empty_output><jupyter_text>modelcheckoutpoint 는, loss값이 가장 적은 좋은 곳을 저장해주는 기능 model_path = 'keras_house_model.h5'model_checkoutpoinr = ModelCheckpoint(filepath=model_path,                                        monitor = 'val_loss',                                        save_best_only=True                                     )<jupyter_code># 5.학습시키기
from keras.callbacks import EarlyStopping    #학습 진행되는지 먼저 보고, 나중에 추가하는 것이 좋다
early_stopping = EarlyStopping(monitor='val_loss', # val_loss 는 default 값
                            patience=10 ) 

model_path = 'keras_house_model.h5'
model_checkoutpoinr = ModelCheckpoint(filepath=model_path,
                                        monitor = 'val_loss',
                                        save_best_only=True
                                     )


# val_loss가 올라갈때 참을성 10번 할당           
history= model.fit(X_train,y_train,
          validation_data = (X_val, y_val),  # 위에서 안하고, 바로 할 수도 있다. validation_split =0.1
          batch_size=32,
          epochs=1000,
          verbose=1, 
          callbacks= [early_stopping])         
          #verbose 는 0,1,2 의 값만 가지게 된다. 학습이 진행되는 걸 보고 싶르면
          #0으로 지정, 안 보고 싶다면 1, loss 값만 보고 싶다면 2
         
# 6. Model Evaluate
train_result = model.evaluate(X_train, y_train)
test_result = model.evaluate(X_test, y_test)

plt.plot(history.history['loss'])
plt.plot(history.history['val_loss'])

plt.xlabel('Epoch')
plt.ylabel('loss')
plt.legend(['train', 'val'], loc = 'upper left')
plt.show()
X<jupyter_output><empty_output><jupyter_text>전통적인 머신러닝 방법으로 해결해보자.  기본적인 회귀 모형으로는, 식 1개로 해결했다면위의 딥러닝 방법으로는, active함수 포함 및 여러가지의 모델들이 복잡하게 연결되어 있고, 앙상블의 역할을 톡톡히 한다.<jupyter_code>y
from sklearn.linear_model import LinearRegression

linear_model= LinearRegression()   # y= wx +b / bias,(기본값이 True : bias의 영향을 미치게 한다. ) intercept
# linear_model =  LinearRegression(intercept = False)  -> 영향을 없애준다. 
linear_model.fit(X_train, y_train)
from sklearn.tree import DecisionTreeRegressor
tree_model =DecisionTreeRegressor().fit(X_train, y_train)
y_pred = linear_model.predict(X_test)
print(y_pred)
print(y_test)
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score


mae_l= mean_absolute_error(y_test, y_pred)
sme_l = mean_squared_error(y_test, y_pred)
r2_l = r2_score(y_test, y_pred)

print('mae =', mae_l)
print('msme =', sme_l)
print('r2 =', r2_l)<jupyter_output>mae = 3.4841400526446744
msme = 27.09208627320056
r2 = 0.6996870210322352
{'fraction_non_alphanumeric': 0.11478102189781023, 'fraction_numerical': 0.03193430656934307, 'mean_word_length': 4.005479452054795, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 17, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '351375', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2458, 'n_tokens_neox': 2512, 'n_words': 491, 'path': 'Deep_Learning/04.MLP_Housing_Regression.ipynb', 'repo_name': 'jeewoolee-jasmine/TIL', 'size': '5480'}