application_vnd.jupyter.stderr_output_1.png image_output_1.png
from sklearn.metrics import mean_absolute_error, r2_score, mean_squared_error

from sklearn.model_selection import train_test_split, cross_validate

from sklearn.preprocessing import LabelEncoder, StandardScaler

import matplotlib.pyplot as plt

import numpy as np

import pandas as pd

import pprint

import seaborn as sns

import pandas as pd
import numpy as np
df = pd.read_csv('/kaggle/input/car-price-prediction/CarPrice_Assignment.csv')

dfx = df.copy()
cat = []
num = []
for n, d in dfx.items():
    if d.dtype == 'object':
        cat.append(n)
    else:
        num.append(n)
dfx = df.copy()
from sklearn.preprocessing import LabelEncoder, StandardScaler
le = LabelEncoder()
for i in cat:
    dfx[i] = le.fit_transform(dfx[i])
ss = StandardScaler()
for i in num:
    dfx[i] = ss.fit_transform(dfx[[i]])
import matplotlib.pyplot as plt
import seaborn as sns
corr = dfx.corr()
matrix = np.triu(corr)

X = df.drop(['price'], axis=1)
y = df[['price']]

cat = []
num = []
for n, d in X.items():
    if d.dtype == 'object':
        cat.append(n)
    else:
        num.append(n)

le = LabelEncoder()
for i in cat:
    X[i] = le.fit_transform(X[i])
ss = StandardScaler()
for i in num:
    X[i] = ss.fit_transform(X[[i]])
for i in cat:
    X[i] = ss.fit_transform(X[[i]])

import pprint
from sklearn.model_selection import train_test_split, cross_validate
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_absolute_error, r2_score, mean_squared_error
def model_eval(m, X, y):
    a, d, s, f = train_test_split(X, y)
    m.fit(a, s)
    g = m.predict(d)
def model_cv(m, X, y):
    scoring = ['neg_mean_absolute_error', 'neg_root_mean_squared_error', 'r2']
    scores = cross_validate(m, X, y, scoring=scoring, cv=4, return_train_score=False)

X1 = X.copy()
X1['enginesize'].loc[np.random.randint(153, size=30)] = np.nan
X1.isnull().sum()

es_mean = X1['enginesize'].mean()
X11 = X1.copy()
X11 = X11.fillna(value=0)
X12 = X1.copy()
X12 = X12.fillna(value=es_mean)

des = ['without null values :', 'null values replaced by 0', 'null values replaced by mean']
j = 0
for i in [X, X11, X12]:
    print(des[j])
    j += 1
    model_eval(lr, i, y)
    print('cv\t:')
    model_cv(lr, i, y)
    print()