import pandas as pd
import numpy as np

import seaborn as sns
from matplotlib import pyplot as plt

class CarPrice:

    def __init__(self):
        self.df = pd.read_csv('data/data.csv')
        print(f'${len(self.df)} lines loaded')

    def trim(self):
        self.df.columns = self.df.columns.str.lower().str.replace(' ', '_')
        string_columns = list(self.df.dtypes[self.df.dtypes == 'object'].index)
        for col in string_columns:
            self.df[col] = self.df[col].str.lower().str.replace(' ', '_')

    def validate(self):

        np.random.seed(2)

        n = len(self.df)

        n_val = int(0.2 * n)
        n_test = int(0.2 * n)
        n_train = n - (n_val + n_test)

        idx = np.arange(n)
        np.random.shuffle(idx)

        df_shuffled = self.df.iloc[idx]

        X_train = df_shuffled.iloc[:n_train].copy()
        X_val = df_shuffled.iloc[n_train:n_train+n_val].copy()
        X_test = df_shuffled.iloc[n_train+n_val:].copy()

        y_train = X_train.msrp
        y_val = X_val.msrp
        y_test = X_test.msrp

        del X_train['msrp']
        del X_val['msrp']
        del X_test['msrp']
        data = {
            'X_train': X_train,
            'X_val' : X_val,
            'X_test': X_test,
            'y_train': y_train,
            'y_val' : y_val,
            'y_test': y_test
        }
        return data

    def linear_regression(self, X, y):
        ones = np.ones(X.shape[0])
        X = np.column_stack([ones, X])

        XTX = X.T.dot(X)
        XTX_inv = np.linalg.inv(XTX)
        w = XTX_inv.dot(X.T).dot(y)
        return w[0], w[1:]

    def prepare_X(self, df):
        from sklearn.impute import SimpleImputer
        features = ['engine_hp', 'engine_cylinders', 'highway_mpg', 'city_mpg', 'popularity', 'year']
        df_features = df[features]
        # df_features=(df_features-df_features.min())/(df_features.max()-df_features.min())
        imp_medium = SimpleImputer(missing_values=np.nan, strategy='median')
        imp_medium.fit(df_features)
        return df_features

    def rmse(self, y, y_pred):
        error = y_pred - y
        mse = (error ** 2).mean()
        return np.sqrt(mse)

if __name__ == '__main__':
    carPrice = CarPrice()
    carPrice.trim()
    data = carPrice.validate()
    X_train = carPrice.prepare_X(data['X_train'])
    w_0, w = carPrice.linear_regression(X_train, data['y_train'])
    X_test = carPrice.prepare_X(data['X_test'])
    y_pred = w_0 + X_test.dot(w)
    rmse = carPrice.rmse(data['y_test'], y_pred)
    print(rmse)

    result = X_test[['engine_hp', 'engine_cylinders', 'highway_mpg', 'city_mpg', 'popularity', 'year']].head()
    result["msrp"] = data['y_test']
    result["msrp_pred"] = y_pred
    pd.options.display.max_columns = None
    pd.options.display.width = 0
    print(result)
584d59a76200559d9e74b6aed71b480db41db373
<mask token>


class CarPrice:

    def __init__(self):
        self.df = pd.read_csv('data/data.csv')
        print(f'${len(self.df)} lines loaded')

    def trim(self):
        self.df.columns = self.df.columns.str.lower().str.replace(' ', '_')
        string_columns = list(self.df.dtypes[self.df.dtypes == 'object'].index)
        for col in string_columns:
            self.df[col] = self.df[col].str.lower().str.replace(' ', '_')
    <mask token>

    def linear_regression(self, X, y):
        ones = np.ones(X.shape[0])
        X = np.column_stack([ones, X])
        XTX = X.T.dot(X)
        XTX_inv = np.linalg.inv(XTX)
        w = XTX_inv.dot(X.T).dot(y)
        return w[0], w[1:]

    def prepare_X(self, df):
        from sklearn.impute import SimpleImputer
        features = ['engine_hp', 'engine_cylinders', 'highway_mpg',
            'city_mpg', 'popularity', 'year']
        df_features = df[features]
        imp_medium = SimpleImputer(missing_values=np.nan, strategy='median')
        imp_medium.fit(df_features)
        return df_features
    <mask token>


<mask token>

<mask token>


class CarPrice:

    def __init__(self):
        self.df = pd.read_csv('data/data.csv')
        print(f'${len(self.df)} lines loaded')

    def trim(self):
        self.df.columns = self.df.columns.str.lower().str.replace(' ', '_')
        string_columns = list(self.df.dtypes[self.df.dtypes == 'object'].index)
        for col in string_columns:
            self.df[col] = self.df[col].str.lower().str.replace(' ', '_')
    <mask token>

    def linear_regression(self, X, y):
        ones = np.ones(X.shape[0])
        X = np.column_stack([ones, X])
        XTX = X.T.dot(X)
        XTX_inv = np.linalg.inv(XTX)
        w = XTX_inv.dot(X.T).dot(y)
        return w[0], w[1:]

    def prepare_X(self, df):
        from sklearn.impute import SimpleImputer
        features = ['engine_hp', 'engine_cylinders', 'highway_mpg',
            'city_mpg', 'popularity', 'year']
        df_features = df[features]
        imp_medium = SimpleImputer(missing_values=np.nan, strategy='median')
        imp_medium.fit(df_features)
        return df_features

    def rmse(self, y, y_pred):
        error = y_pred - y
        mse = (error ** 2).mean()
        return np.sqrt(mse)


<mask token>

<mask token>


class CarPrice:

    def __init__(self):
        self.df = pd.read_csv('data/data.csv')
        print(f'${len(self.df)} lines loaded')

    def trim(self):
        self.df.columns = self.df.columns.str.lower().str.replace(' ', '_')
        string_columns = list(self.df.dtypes[self.df.dtypes == 'object'].index)
        for col in string_columns:
            self.df[col] = self.df[col].str.lower().str.replace(' ', '_')

    def validate(self):
        np.random.seed(2)
        n = len(self.df)
        n_val = int(0.2 * n)
        n_test = int(0.2 * n)
        n_train = n - (n_val + n_test)
        idx = np.arange(n)
        np.random.shuffle(idx)
        df_shuffled = self.df.iloc[idx]
        X_train = df_shuffled.iloc[:n_train].copy()
        X_val = df_shuffled.iloc[n_train:n_train + n_val].copy()
        X_test = df_shuffled.iloc[n_train + n_val:].copy()
        y_train = X_train.msrp
        y_val = X_val.msrp
        y_test = X_test.msrp
        del X_train['msrp']
        del X_val['msrp']
        del X_test['msrp']
        data = {'X_train': X_train, 'X_val': X_val, 'X_test': X_test,
            'y_train': y_train, 'y_val': y_val, 'y_test': y_test}
        return data

    def linear_regression(self, X, y):
        ones = np.ones(X.shape[0])
        X = np.column_stack([ones, X])
        XTX = X.T.dot(X)
        XTX_inv = np.linalg.inv(XTX)
        w = XTX_inv.dot(X.T).dot(y)
        return w[0], w[1:]

    def prepare_X(self, df):
        from sklearn.impute import SimpleImputer
        features = ['engine_hp', 'engine_cylinders', 'highway_mpg',
            'city_mpg', 'popularity', 'year']
        df_features = df[features]
        imp_medium = SimpleImputer(missing_values=np.nan, strategy='median')
        imp_medium.fit(df_features)
        return df_features

    def rmse(self, y, y_pred):
        error = y_pred - y
        mse = (error ** 2).mean()
        return np.sqrt(mse)


<mask token>

<mask token>


class CarPrice:

    def __init__(self):
        self.df = pd.read_csv('data/data.csv')
        print(f'${len(self.df)} lines loaded')

    def trim(self):
        self.df.columns = self.df.columns.str.lower().str.replace(' ', '_')
        string_columns = list(self.df.dtypes[self.df.dtypes == 'object'].index)
        for col in string_columns:
            self.df[col] = self.df[col].str.lower().str.replace(' ', '_')

    def validate(self):
        np.random.seed(2)
        n = len(self.df)
        n_val = int(0.2 * n)
        n_test = int(0.2 * n)
        n_train = n - (n_val + n_test)
        idx = np.arange(n)
        np.random.shuffle(idx)
        df_shuffled = self.df.iloc[idx]
        X_train = df_shuffled.iloc[:n_train].copy()
        X_val = df_shuffled.iloc[n_train:n_train + n_val].copy()
        X_test = df_shuffled.iloc[n_train + n_val:].copy()
        y_train = X_train.msrp
        y_val = X_val.msrp
        y_test = X_test.msrp
        del X_train['msrp']
        del X_val['msrp']
        del X_test['msrp']
        data = {'X_train': X_train, 'X_val': X_val, 'X_test': X_test,
            'y_train': y_train, 'y_val': y_val, 'y_test': y_test}
        return data

    def linear_regression(self, X, y):
        ones = np.ones(X.shape[0])
        X = np.column_stack([ones, X])
        XTX = X.T.dot(X)
        XTX_inv = np.linalg.inv(XTX)
        w = XTX_inv.dot(X.T).dot(y)
        return w[0], w[1:]

    def prepare_X(self, df):
        from sklearn.impute import SimpleImputer
        features = ['engine_hp', 'engine_cylinders', 'highway_mpg',
            'city_mpg', 'popularity', 'year']
        df_features = df[features]
        imp_medium = SimpleImputer(missing_values=np.nan, strategy='median')
        imp_medium.fit(df_features)
        return df_features

    def rmse(self, y, y_pred):
        error = y_pred - y
        mse = (error ** 2).mean()
        return np.sqrt(mse)


if __name__ == '__main__':
    carPrice = CarPrice()
    carPrice.trim()
    data = carPrice.validate()
    X_train = carPrice.prepare_X(data['X_train'])
    w_0, w = carPrice.linear_regression(X_train, data['y_train'])
    X_test = carPrice.prepare_X(data['X_test'])
    y_pred = w_0 + X_test.dot(w)
    rmse = carPrice.rmse(data['y_test'], y_pred)
    print(rmse)
    result = X_test[['engine_hp', 'engine_cylinders', 'highway_mpg',
        'city_mpg', 'popularity', 'year']].head()
    result['msrp'] = data['y_test']
    result['msrp_pred'] = y_pred
    pd.options.display.max_columns = None
    pd.options.display.width = 0
    print(result)

import pandas as pd
import numpy as np

import seaborn as sns
from matplotlib import pyplot as plt

class CarPrice:

    def __init__(self):
        self.df = pd.read_csv('data/data.csv')
        print(f'${len(self.df)} lines loaded')

    def trim(self):
        self.df.columns = self.df.columns.str.lower().str.replace(' ', '_')
        string_columns = list(self.df.dtypes[self.df.dtypes == 'object'].index)
        for col in string_columns:
            self.df[col] = self.df[col].str.lower().str.replace(' ', '_')

    def validate(self):

        np.random.seed(2)

        n = len(self.df)

        n_val = int(0.2 * n)
        n_test = int(0.2 * n)
        n_train = n - (n_val + n_test)

        idx = np.arange(n)
        np.random.shuffle(idx)

        df_shuffled = self.df.iloc[idx]

        X_train = df_shuffled.iloc[:n_train].copy()
        X_val = df_shuffled.iloc[n_train:n_train+n_val].copy()
        X_test = df_shuffled.iloc[n_train+n_val:].copy()

        y_train = X_train.msrp
        y_val = X_val.msrp
        y_test = X_test.msrp

        del X_train['msrp']
        del X_val['msrp']
        del X_test['msrp']
        data = {
            'X_train': X_train,
            'X_val' : X_val,
            'X_test': X_test,
            'y_train': y_train,
            'y_val' : y_val,
            'y_test': y_test
        }
        return data

    def linear_regression(self, X, y):
        ones = np.ones(X.shape[0])
        X = np.column_stack([ones, X])

        XTX = X.T.dot(X)
        XTX_inv = np.linalg.inv(XTX)
        w = XTX_inv.dot(X.T).dot(y)
        return w[0], w[1:]

    def prepare_X(self, df):
        from sklearn.impute import SimpleImputer
        features = ['engine_hp', 'engine_cylinders', 'highway_mpg', 'city_mpg', 'popularity', 'year']
        df_features = df[features]
        # df_features=(df_features-df_features.min())/(df_features.max()-df_features.min())
        imp_medium = SimpleImputer(missing_values=np.nan, strategy='median')
        imp_medium.fit(df_features)
        return df_features

    def rmse(self, y, y_pred):
        error = y_pred - y
        mse = (error ** 2).mean()
        return np.sqrt(mse)

if __name__ == '__main__':
    carPrice = CarPrice()
    carPrice.trim()
    data = carPrice.validate()
    X_train = carPrice.prepare_X(data['X_train'])
    w_0, w = carPrice.linear_regression(X_train, data['y_train'])
    X_test = carPrice.prepare_X(data['X_test'])
    y_pred = w_0 + X_test.dot(w)
    rmse = carPrice.rmse(data['y_test'], y_pred)
    print(rmse)

    result = X_test[['engine_hp', 'engine_cylinders', 'highway_mpg', 'city_mpg', 'popularity', 'year']].head()
    result["msrp"] = data['y_test']
    result["msrp_pred"] = y_pred
    pd.options.display.max_columns = None
    pd.options.display.width = 0
    print(result)