text_plain_output_1.png
from sklearn import linear_model

from sklearn.linear_model import LinearRegression

from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score

from sklearn.preprocessing import RobustScaler

import numpy as np

import pandas as pd

import seaborn as sns

import warnings

import pandas as pd
import numpy as np
from matplotlib import pyplot as plt
plt.rcParams.update({'figure.max_open_warning': 0})
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn import linear_model
from statsmodels.stats.outliers_influence import variance_inflation_factor
from sklearn.preprocessing import RobustScaler
from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score
import warnings
warnings.filterwarnings('ignore')

def visualize(data):
    for c in data.columns:
        plt.figure()
        plt.tight_layout()
        sns.set(rc={"figure.figsize":(8, 5)})
        if data[c].dtype=='int64' or data[c].dtype=='float64':
            f, (ax_box, ax_hist) = plt.subplots(2, sharex=True)
            plt.gca().set(xlabel= c,ylabel='Frequency')
            sns.boxplot(data[c], ax=ax_box , linewidth= 1.0)
            sns.histplot(data[c], ax=ax_hist , bins = 10,kde=True)
        else:
            plt.gca().set(xlabel= c,ylabel='Frequency')
            sns.histplot(data[c], bins = 10)
            
            
def evaluate(model,x_train , y_train, x_test , y_test, y_predict):
    print(f'score x_train , y_train : {model.score(x_train , y_train)}')
    print(f'score x_test , y_test : {model.score(x_test , y_test)}')
    print(f'r2_score : {r2_score(y_test, y_predict)}')
    print(f'mean absolute err : {mean_absolute_error(y_predict, y_test)}')
    print(f'mean squared err : {mean_squared_error(y_test, y_predict)}')

df = pd.read_csv('../input/car-price-prediction/CarPrice_Assignment.csv')

df.columns

pd.set_option('display.max_columns', None)

df.isnull().sum()

df.duplicated().sum()

visualize(df.drop(['car_ID', 'CarName'], axis=1))

df.columns

limits = {}
def IQRoutliers(data):
    global limits
    for c in data.columns:
        if data[c].dtype == 'int64' or data[c].dtype == 'float64':
            Q1, Q3 = np.percentile(data[c], [25, 75])
            IQR = Q3 - Q1
            limits[c] = [Q1 - 1.5 * IQR, Q3 + 1.5 * IQR]
            df[c] = np.where(df[c] > limits[c][1], limits[c][1], np.where(df[c] < limits[c][0], limits[c][0], df[c]))

IQRoutliers(df.drop(['car_ID'], axis=1))

visualize(df.drop(['car_ID', 'CarName'], axis=1))

df = pd.get_dummies(df)

model = LinearRegression()
model.fit(x_train, y_train)
y_predict = model.predict(x_test)
evaluate(model, x_train, y_train, x_test, y_test, y_predict)

model = linear_model.Lasso()
model.fit(x_train, y_train)
y_predict = model.predict(x_test)
evaluate(model, x_train, y_train, x_test, y_test, y_predict)

scaler = RobustScaler()
x_train = scaler.fit_transform(x_train)
x_test = scaler.fit_transform(x_test)

model = linear_model.BayesianRidge()
model.fit(x_train, y_train)
y_predict = model.predict(x_test)
result = pd.DataFrame({'Y_test': y_test, 'Y_predicted': y_predict})
evaluate(model, x_train, y_train, x_test, y_test, y_predict)

sns.regplot(x=y_test, y=y_predict, color='green')