<jupyter_start><jupyter_text>Multiple Linear Regression<jupyter_code>import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np
import pylab as pl
%matplotlib inline
import os

source = 'https://www.kaggle.com/antfarol/car-sale-advertisements/download'

link = os.path.expanduser("~/Downloads/car_ad.csv")
print(link)
df = pd.read_csv(link, encoding="latin1")
df.head(10)
df.shape

df.dtypes
df.describe(include = "all")
df.replace("?", np.nan, inplace = True)
df.head(2)
missing_data = df.isnull() # True - null data (NaN, 0)
missing_data.head(2)
for column in missing_data.columns.values.tolist():
    print(column)
    print (missing_data[column].value_counts())
    print("")
df.columns
features = ['car', 'price', 'body', 'mileage', 'engV', 'engType', 'registration', 'year', 'model', 'drive']
for el in features:
    print(f'=============================== {el} =============================')
    print(df[el].value_counts(dropna=False))
df['drive'].replace(np.nan, "other", inplace=True)
df[['price']] = df[['price']].astype("int")
print(df['price'].value_counts(dropna=False))
df.dropna(subset=['engV'], axis=0, inplace=True)
df.reset_index(drop=True, inplace=True)

df['engV'].value_counts(dropna=False)
features = ['car', 'price', 'body', 'mileage', 'engV', 'engType', 'registration', 'year', 'model', 'drive']

for el in features:
    print('')
    label = el
    el = df[el]
    plt.figure(figsize=(20,10))
    try:
        plt.scatter(el, df.price)
    except:
        print(Error)
    plt.xlabel(label.upper())
    plt.ylabel("PRICE")
    plt.show()
    print('')

df1 = df[['price', 'mileage', 'engV', 'year']]
msk = np.random.rand(len(df)) < 0.8
train = df1[msk]
test = df1[~msk]
from sklearn import linear_model
regr = linear_model.LinearRegression()
x = np.asanyarray(train[['mileage', 'engV', 'year']])
y = np.asanyarray(train[['price']])
regr.fit (x, y)

print ('Coefficients: ', regr.coef_)
y_hat= regr.predict(test[['mileage', 'engV', 'year']])
x = np.asanyarray(test[['mileage', 'engV', 'year']])
y = np.asanyarray(test[['price']])
print("Residual sum of squares: %.2f"
      % np.mean((y_hat - y) ** 2))

# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % regr.score(x, y))
df1.shape
df2 = df1[['price', 'mileage', 'engV', 'year']]
features = ['price', 'mileage', 'engV', 'year']
for el in features:
    print(f'=============================== {el} =============================')
    print(df2[el].value_counts(dropna=False))
features = ['price', 'mileage', 'engV', 'year']

for el in features:
    print('')
    label = el
    el = df2[el]
    plt.figure(figsize=(20,10))
    try:
        plt.scatter(el, df2.price)
    except:
        print(Error)
    plt.xlabel(label.upper())
    plt.ylabel("PRICE")
    plt.show()
    print('')
df2 = df2.loc[df2['price'] != 0] 
df2.reset_index(drop=True, inplace=True)
df2['price'].value_counts(dropna=False)
df2.shape
df2 = df2.loc[df2['price'] < 200000] 
df2.reset_index(drop=True, inplace=True)
df2['price'].value_counts(dropna=False)
df2.shape
features = ['price', 'mileage', 'engV', 'year']

for el in features:
    print('')
    label = el
    el = df2[el]
    plt.figure(figsize=(20,10))
    try:
        plt.scatter(el, df2.price)
    except:
        print(Error)
    plt.xlabel(label.upper())
    plt.ylabel("PRICE")
    plt.show()
    print('')
df2.shape
df2 = df2.loc[df2['engV'] > 0.5] 
df2.reset_index(drop=True, inplace=True)
df2['engV'].value_counts(dropna=False)
df2.shape
df2 = df2.loc[df2['engV'] < 10] 
df2.reset_index(drop=True, inplace=True)
df2['engV'].value_counts(dropna=False)
df2.shape
df2 = df2.loc[df2['year'] > 1980] 
df2.reset_index(drop=True, inplace=True)
df2['year'].value_counts(dropna=False)
df2.shape
df2 = df2.loc[df2['mileage'] > 1] 
df2.reset_index(drop=True, inplace=True)
df2['mileage'].value_counts(dropna=False)
df2.shape
df2 = df2.loc[df2['mileage'] < 400] 
df2.reset_index(drop=True, inplace=True)
df2['mileage'].value_counts(dropna=False)
df2.shape
features = ['mileage', 'engV', 'year']

for el in features:
    print('')
    label = el
    el = df2[el]
    plt.figure(figsize=(20,10))
    try:
        plt.scatter(el, df2.price)
    except:
        print(Error)
    plt.xlabel(label.upper())
    plt.ylabel("PRICE")
    plt.show()
    print('')
msk = np.random.rand(len(df2)) < 0.8
train = df2[msk]
test = df2[~msk]
regr = linear_model.LinearRegression()
x = np.asanyarray(train[['mileage', 'engV', 'year']])
y = np.asanyarray(train[['price']])
regr.fit (x, y)

print ('Coefficients: ', regr.coef_)
y_hat= regr.predict(test[['mileage', 'engV', 'year']])
x = np.asanyarray(test[['mileage', 'engV', 'year']])
y = np.asanyarray(test[['price']])
print("Residual sum of squares: %.2f"
      % np.mean((y_hat - y) ** 2))

# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % regr.score(x, y))<jupyter_output>Residual sum of squares: 140596473.44
Variance score: 0.50<jupyter_text>drop ['engV']<jupyter_code>regr = linear_model.LinearRegression()
x = np.asanyarray(train[['mileage', 'year']])
y = np.asanyarray(train[['price']])
regr.fit (x, y)

print ('Coefficients: ', regr.coef_)
y_hat= regr.predict(test[['mileage', 'year']])
x = np.asanyarray(test[['mileage', 'year']])
y = np.asanyarray(test[['price']])
print("Residual sum of squares: %.2f"
      % np.mean((y_hat - y) ** 2))

# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % regr.score(x, y))<jupyter_output>Residual sum of squares: 225617307.04
Variance score: 0.21<jupyter_text>NON LINEAR REGRESSION    [NICE guide !!!](https://www.pluralsight.com/guides/non-linear-regression-trees-scikit-learn) <-- link<jupyter_code>df2 = df2[['price', 'engV', 'year', 'mileage']]<jupyter_output><empty_output><jupyter_text><jupyter_code>df2.describe()<jupyter_output><empty_output><jupyter_text>normalization<jupyter_code>target_column = ['price'] 
predictors = list(set(list(df2.columns))-set(target_column))
df2[predictors] = df2[predictors]/df2[predictors].max()
df2.describe()
from sklearn.model_selection import train_test_split
X = df2[predictors].values
y = df2[target_column].values
y = np.ravel(y)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.30, random_state=40)
print(X_train.shape); print(X_test.shape)<jupyter_output>(5677, 3)
(2433, 3)<jupyter_text>DecisionTreeRegressor<jupyter_code>from sklearn.tree import DecisionTreeRegressor

dtree = DecisionTreeRegressor(max_depth=8, min_samples_leaf=0.13, random_state=3)

dtree.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Once the model is built on the training set, we can make the predictions. The first line of code below predicts on the training set. The second and third lines of code prints the evaluation metrics - RMSE and R-squared - on the training set. The same steps are repeated on the test dataset in the fourth to sixth lines.<jupyter_code>from sklearn.metrics import mean_squared_error
from math import sqrt
from sklearn.metrics import r2_score

# Code lines 1 to 3
pred_train_tree= dtree.predict(X_train)
print(np.sqrt(mean_squared_error(y_train,pred_train_tree)))
print(r2_score(y_train, pred_train_tree))

# Code lines 4 to 6
pred_test_tree= dtree.predict(X_test)
print(np.sqrt(mean_squared_error(y_test,pred_test_tree))) 
print(r2_score(y_test, pred_test_tree))
# Code Lines 1 to 4: Fit the regression tree 'dtree1' and 'dtree2' 
dtree1 = DecisionTreeRegressor(max_depth=2)
dtree2 = DecisionTreeRegressor(max_depth=5)
dtree1.fit(X_train, y_train)
dtree2.fit(X_train, y_train)

# Code Lines 5 to 6: Predict on training data
tr1 = dtree1.predict(X_train)
tr2 = dtree2.predict(X_train) 

#Code Lines 7 to 8: Predict on testing data
y1 = dtree1.predict(X_test)
y2 = dtree2.predict(X_test)
# Print RMSE and R-squared value for regression tree 'dtree1' on training data
print(np.sqrt(mean_squared_error(y_train,tr1))) 
print(r2_score(y_train, tr1))

# Print RMSE and R-squared value for regression tree 'dtree1' on testing data
print(np.sqrt(mean_squared_error(y_test,y1))) 
print(r2_score(y_test, y1)) 
# Print RMSE and R-squared value for regression tree 'dtree2' on training data
print(np.sqrt(mean_squared_error(y_train,tr2))) 
print(r2_score(y_train, tr2))

# Print RMSE and R-squared value for regression tree 'dtree2' on testing data
print(np.sqrt(mean_squared_error(y_test,y2))) 
print(r2_score(y_test, y2))<jupyter_output>6963.026787744462
0.8182363257771048
7384.213708958069
0.7497612533936456<jupyter_text>RandomForestRegressor<jupyter_code>from sklearn.ensemble import RandomForestRegressor

#RF model
model_rf = RandomForestRegressor(n_estimators=500, oob_score=True, random_state=100)
model_rf.fit(X_train, y_train) 
pred_train_rf= model_rf.predict(X_train)
print(np.sqrt(mean_squared_error(y_train,pred_train_rf)))
print(r2_score(y_train, pred_train_rf))

pred_test_rf = model_rf.predict(X_test)
print(np.sqrt(mean_squared_error(y_test,pred_test_rf)))
print(r2_score(y_test, pred_test_rf))<jupyter_output>3065.2998035776723
0.9647745235566882
6740.1034006555
0.7915129137266729<jupyter_text>With Boosting  lightgbm<jupyter_code>import lightgbm as lgb

lgbm = lgb.LGBMRegressor()
lgbm.fit(X_train,y_train)

pred_train_lgbm=lgbm.predict(X_train)
print(np.sqrt(mean_squared_error(y_train,pred_train_lgbm)))
print(r2_score(y_train, pred_train_lgbm))

pred_test_lgbm = lgbm.predict(X_test)
print(np.sqrt(mean_squared_error(y_test,pred_test_lgbm)))
print(r2_score(y_test, pred_test_lgbm))<jupyter_output>5375.373391826913
0.8916751152712279
6310.347586847126
0.8172520116495273<jupyter_text>xgboost<jupyter_code>import xgboost as xgb

xgbm = xgb.XGBRegressor()
xgbm.fit(X_train, y_train)

pred_train_xgb=xgbm.predict(X_train)
print(np.sqrt(mean_squared_error(y_train,pred_train_xgb)))
print(r2_score(y_train, pred_train_xgb))

pred_test_xgb = xgbm.predict(X_test)
print(np.sqrt(mean_squared_error(y_test,pred_test_xgb)))
print(r2_score(y_test, pred_test_xgb))<jupyter_output>3404.8331843453
0.9565387073492619
6672.0404398873625
0.7957023451048562<jupyter_text>catboost<jupyter_code>import catboost as ctb

ctbm = ctb.CatBoostRegressor(verbose=False)
ctbm.fit(X_train, y_train)

pred_train_cat=ctbm.predict(X_train)
print(np.sqrt(mean_squared_error(y_train,pred_train_cat)))
print(r2_score(y_train, pred_train_cat))

pred_test_cat = ctbm.predict(X_test)
print(np.sqrt(mean_squared_error(y_test,pred_test_cat)))
print(r2_score(y_test, pred_test_cat))<jupyter_output>4528.22389095845
0.9231282541419514
6560.541385000363
0.8024734854560835
{'fraction_non_alphanumeric': 0.1634433067880167, 'fraction_numerical': 0.05318543799772469, 'mean_word_length': 6.310464310464311, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 40, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '11', 'dir': 'jupyter-structured-clean-dedup', 'id': '221272', 'license': 'MIT', 'n_tokens_mistral': 4557, 'n_tokens_neox': 4200, 'n_words': 753, 'path': 'Multiple Linear Regression.ipynb', 'repo_name': 'kolasdevpy/Regressions', 'size': '10548'}