import pandas as pd
import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers
from xgboost import XGBRegressor
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.preprocessing import MinMaxScaler
dataset_path = keras.utils.get_file("auto-mpg.data",
"http://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data")
column_names = ['MPG', 'Cylinders', 'Displacement',
'Horsepower', 'Weight', 'Acceleration',
'model_Year', 'Origin']
data = pd.read_csv(dataset_path, names=column_names,
na_values='?', comment='\t', sep=" ", skipinitialspace=True)
df = data.copy()
df = df.dropna()
df['USA'] = df.Origin.apply(lambda x : 1 if x == 1 else 0)
df['Europe'] = df.Origin.apply(lambda x : 1 if x == 2 else 0)
df['Japan'] = df.Origin.apply(lambda x : 1 if x == 3 else 0)
df.drop('Origin', axis=1, inplace=True)
train = df.sample(frac=0.8, random_state=0)
test = df.drop(train.index)
X_train, y_train = train.drop('MPG', axis=1), train['MPG']
X_test, y_test = test.drop('MPG', axis=1), test['MPG']
scaler = MinMaxScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(X_test)
케라스 사용 모델
model = keras.Sequential()
model.add(layers.Dense(64, activation='relu', input_shape=(9,)))
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(1))
optimizer = tf.keras.optimizers.RMSprop(0.001)
model.compile(loss='mse',
optimizer=optimizer,
metrics=['mae', 'mse'])
history = model.fit(
X_train, y_train,
epochs=500, validation_split = 0.2, verbose=0)
result = model.evaluate(X_test, y_test)
print("mae :", round(result[1], 3))
print("mse :", round(result[2], 3))
XGBRegressor 사용 모델
reg = XGBRegressor()
reg.fit(X_train, y_train)
y_pred = reg.predict(X_test)
print("mae :", round(mean_absolute_error(y_test, y_pred), 3))
print("mse :", round(mean_squared_error(y_test, y_pred), 3))