주제 : 딥러닝 전체 과정의 코드 실습
내용 : 이를 통해, 딥러닝 회귀, 분류를 완벽하게 구현해 낼 수 있다.
path = 'https://raw.githubusercontent.com/DA4BAM/dataset/master/advertising.csv'
adv = pd.read_csv(path)
adv.head()
target = 'Sales'
x = adv.drop(target, axis=1)
y = adv.loc[:, target]
# 데이터 분할
x_train, x_val, y_train, y_val = train_test_split(x, y, test_size=.2, random_state = 20)
# 선형회귀 모델링
from sklearn.linear_model import LinearRegression
model = LinearRegression()
model.fit(x_train, y_train)
pred = model.predict(x_val)
print(f'RMSE : {mean_squared_error(y_val, pred, squared=False)}')
print(f'MAE : {mean_absolute_error(y_val, pred)}')
print(f'MAPE : {mean_absolute_percentage_error(y_val, pred)}')
scaler = MinMaxScaler()
x_train = scaler.fit_transform(x_train)
x_val = scaler.transform(x_val)
from keras.models import Sequential
from keras.layers import Dense
from keras.backend import clear_session
여기서 Dense의 1은 output_shape(nfeatures에 있는 것들 합친것)
nfeatures = x_train.shape[1] #num of columns
nfeatures
# 메모리 정리
clear_session()
# Sequential 타입 모델 선언
model = Sequential( Dense(1, input_shape = (nfeatures,)) )
# 모델요약
model.summary()
model.compile(optimizer='adam', loss='mse')
model.fit(x_train, y_train)
pred = model.predict(x_val)
print(f'RMSE : {mean_squared_error(y_val, pred, squared=False)}')
print(f'MAE : {mean_absolute_error(y_val, pred)}')
print(f'MAPE : {mean_absolute_percentage_error(y_val, pred)}')