<jupyter_start><jupyter_text>Multiple Linear Regression  Dataset 가져오기<jupyter_code>import pandas
import seaborn
seaborn.set()
from urllib.request import urlretrieve
URL = 'https://go.gwu.edu/engcomp6data3'
urlretrieve(URL, 'auto_mpg.csv')
mpg_data = pandas.read_csv('/content/auto_mpg.csv')
mpg_data.head()
mpg_data.info()
y_col = 'mpg'
x_cols = mpg_data.columns.drop(['car name', 'origin', 'mpg'])

print(x_cols)
seaborn.pairplot(data=mpg_data, height=5, aspect=1, 
                 x_vars = x_cols,
                 y_vars = y_col);
from autograd import numpy
from autograd import grad
X = mpg_data[x_cols].values
X = numpy.hstack((numpy.ones((X.shape[0], 1)), X))
y = mpg_data[y_col].values

print("X.shape = {}, y.shape = {}".format(X.shape, y.shape))
def linear_regression(params, X):
  return numpy.dot(X, params)  # 벡터의 내적을 구하는 함수-> 선형회귀가 QᵀX꼴로 표현되니깐

def cost_function(params, model, X, y):
  y_pred = model(params, X)
  return numpy.mean(numpy.sum((y-y_pred)**2))
gradient = grad(cost_function)
max_iter = 30
alpha = 0.001
params = numpy.zeros(X.shape[1])

for i in range(max_iter):
  descent = gradient(params, linear_regression, X, y)
  params = params - descent * alpha
  loss = cost_function(params, linear_regression, X, y)
  if i%5 == 0:
    print(f"iterations {i}, loss = {loss}")
mpg_data[x_cols].describe().loc[['max', 'min']]
from sklearn.preprocessing import MinMaxScaler

min_max_scaler = MinMaxScaler()
X_scaled = min_max_scaler.fit_transform(mpg_data[x_cols])
X_scaled = numpy.hstack((numpy.ones((X_scaled.shape[0], 1)), X_scaled)) 

pandas.DataFrame(X_scaled).describe().loc[['max', 'min']]
max_iter = 1000
alpha = 0.001
params = numpy.zeros(X.shape[1])

for i in range(max_iter):
  descent = gradient(params, linear_regression, X_scaled, y)
  params = params - descent * alpha
  loss = cost_function(params, linear_regression, X_scaled, y)
  if i%100 == 0:
    print(f"iterations {i}, loss = {loss}")
params
y_pred_gd = X_scaled @ params
from sklearn.metrics import mean_absolute_error, mean_squared_error

mae = mean_absolute_error(y, y_pred_gd)
rsme = mean_squared_error(y, y_pred_gd, squared=False)
print(mae)
print(rsme)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14410279944928867, 'fraction_numerical': 0.014226709499770536, 'mean_word_length': 5.300578034682081, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 6, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '408222', 'license': 'MIT', 'n_tokens_mistral': 907, 'n_tokens_neox': 896, 'n_words': 177, 'path': 'ML2.ipynb', 'repo_name': 'AI-odyssey/AI-odyssey.github.io', 'size': '2179'}