text_plain_output_1.png
from sklearn.linear_model import LinearRegression

from sklearn.model_selection import train_test_split

import numpy as np # linear algebra

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

import seaborn as sns

mpg_df = pd.read_csv('/kaggle/input/autompg-dataset/auto-mpg.csv')

mpg_df.columns

mpg_df = mpg_df.drop('car name', axis=1)
mpg_df = pd.get_dummies(mpg_df, columns=['origin'])

temp = pd.DataFrame(mpg_df.horsepower.str.isdigit())
temp[temp['horsepower'] == False]

mpg_df = mpg_df.replace('?', np.nan)
mpg_df[mpg_df.isnull().any(axis=1)]

mpg_df.median()
mpg_df = mpg_df.apply(lambda x: x.fillna(x.median()), axis=0)
mpg_df['hp'] = mpg_df['horsepower'].astype('float64')
mpg_df.dtypes

mpg_df_attr = mpg_df.iloc[:, 0:10]

mpg_df.columns

X = mpg_df.drop('mpg', axis=1)
X = X.drop({'origin_1', 'origin_2', 'origin_3'}, axis=1)
y = mpg_df[['mpg']]

from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=1)
regression_model = LinearRegression()
regression_model.fit(X_train, y_train)

intercept = regression_model.intercept_[0]

regression_model.score(X_train, y_train)

regression_model.score(X_test, y_test)

mse = np.mean((regression_model.predict(X_test) - y_test) ** 2)

regression_model.score(X_test, y_test)