# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# #  CONTENTS
#
# ##### 1. Introduction: (#1)
#
# ##### 2. Loading Data and Explanation of Features: (#2)
#
# ##### 3. Exploratory Data Analysis (EDA): (#3)
#
# ##### 4. Applying Regression Models: (#4)
#
# ##### 5. Conclusion: (#5)

# # 1. Introduction
#  
# This dataset contains information about used cars listed on www.cardekho.com.
#
# We are going to use for finding predictions of price with the use of various regression models.
#
# The datasets consist of several independent variables include:
#
# 1.Car_Name
#
# 2.Year
#
# 3.Selling_Price
#
# 4.Present_Price
#
# 5.Kms_Driven
#
# 6.Fuel_Type
#
# 7.Seller_Type
#
# 8.Transmission
#
# 9.Owner

# We are going to use some of the variables which we need for regression models.
#
# # 2. Loading Data and Explanation of Features

# +
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O 
import seaborn as sns 
import matplotlib.pyplot as plt
# %matplotlib inline

#ignore warning 
import warnings
warnings.filterwarnings("ignore")
from statsmodels.graphics.tsaplots import plot_acf #Auto-Correlation Plots
from statsmodels.graphics.tsaplots import plot_pacf #Partial-Auto Correlation Plots

sns.set()
# -

# Input data files are available in the directory where the file is present.
import os
for dirname, _, filenames in os.walk('/Users/KIIT/MACHINE LEARNING FILES'):
    for filename in filenames:
        print(os.path.join(dirname, filename))

car = pd.read_csv("/Users/KIIT/MACHINE LEARNING FILES/car data.csv")

car.head()

car.info()

car.isna().any()

# Looks like our data is totally complete one. 
#
# There is no NaN(null) values and also feature's types are proper.
#
# Lets see value counts of the features which are the object type.

print(car.Fuel_Type.value_counts(),"\n")
print(car.Seller_Type.value_counts(),"\n")
print(car.Transmission.value_counts())

# So from this observation,
#
# I am going to chance these object values to numerical values to make it proper for the regression models.

# ##### Fuel_Type ==> 1 = Petrol , 0 = Diesel , 2 = CNG
# ##### Transmission ==> 1 = Manual , 0 = Automatic 
# ##### Seller_Type ==> 1 = Dealer , 0 = Individual

car.Fuel_Type.replace(regex={"Petrol":"0","Diesel":"1","CNG":"2"},inplace=True)
car.Seller_Type.replace(regex={"Dealer":"0","Individual":"1"},inplace=True)
car.Transmission.replace(regex={"Manual":"0","Automatic":"1"},inplace=True)
car[["Fuel_Type","Seller_Type","Transmission"]]=car[["Fuel_Type","Seller_Type","Transmission"]].astype(int)

# # 3. Exploratory Data Analysis (EDA)
#
# Before applying the regression models, lets take a look at the features and also relationship with each other by visually.

sns.pairplot(car,diag_kind="kde", diag_kws=dict(shade=True, bw=0.05, vertical=False))
plt.show()

# +
from mpl_toolkits.mplot3d import Axes3D

fig = plt.figure(figsize=(16,10))
ax  = fig.gca(projection = "3d")

plot =  ax.scatter(car["Year"],
           car["Present_Price"],
           car["Kms_Driven"],
           linewidth=1,edgecolor ="k",
           c=car["Selling_Price"],s=150,cmap="cool")

ax.set_xlabel("Year")
ax.set_ylabel("Present_Price")
ax.set_zlabel("Kms_Driven")

lab = fig.colorbar(plot,shrink=0.7,aspect=5)
lab.set_label("Selling_Price",fontsize = 20)

plt.title("3D plot for Year, Present price and Kms driven",color="purple")
plt.show()
# -

# On applying the 3D plot features that effect selling price,
# we can see most of the cars accumulate around and after 2010 year, with low present price and low kms driven.
#
# Now its time to apply regression models.
#
# # 4. Applying Regression Models
#
# Firstly lets separate Selling price from the data and drop out all the unnecessary features.

y=car.Selling_Price
x=car.drop(["Selling_Price","Car_Name"],axis=1)

# Spliting data to train and test sizes.

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2,random_state=1)
print("x train: ",x_train.shape)
print("x test: ",x_test.shape)
print("y train: ",y_train.shape)
print("y test: ",y_test.shape)

# Secondly, we are going to load the libraries which we need for the calculation of scores for the various regression models.
#
# Then apply function which fit the models, get the scores and at last plot our predictions .
#
#

from sklearn.metrics import r2_score
from sklearn.model_selection import cross_val_score

# +
cv=5 # CV value
r_2 = [] # List for r 2 score
CV = [] # list for CV scores mean

# Main function for models
def model(algorithm,x_train_,y_train_,x_test_,y_test_): 
    algorithm.fit(x_train_,y_train_)
    predicts=algorithm.predict(x_test_)
    prediction=pd.DataFrame(predicts)
    R_2=r2_score(y_test_,prediction)
    cross_val=cross_val_score(algorithm,x_train_,y_train_,cv=cv)
    
    # Appending results to Lists 
    r_2.append(R_2)
    CV.append(cross_val.mean())
    
    # Printing results  
    print(algorithm,"\n") 
    print("r_2 score :",R_2,"\n")
    print("CV scores:",cross_val,"\n")
    print("CV scores mean:",cross_val.mean())
    
    # Plot for prediction vs originals
    test_index=y_test_.reset_index()["Selling_Price"]
    ax=test_index.plot(label="originals",figsize=(12,6),linewidth=2,color="r")
    ax=prediction[0].plot(label = "predictions",figsize=(12,6),linewidth=2,color="b")
    plt.legend(loc='upper right')
    plt.title("ORIGINALS VS PREDICTIONS")
    plt.xlabel("index")
    plt.ylabel("values")
    plt.show()


# -

# ##### 1. Linear Regression

from sklearn.linear_model import LinearRegression
lr = LinearRegression()
model(lr,x_train,y_train,x_test,y_test)

# ##### 2. Lasso
#
# Before applying Lasso model, I am going to assign an alpha range that effects the  model and choose the best estimator for model.

# +
from sklearn.linear_model import Lasso
from sklearn.model_selection import GridSearchCV

alphas = np.logspace(-3,3,num=14) # range for alpha

grid = GridSearchCV(estimator=Lasso(), param_grid=dict(alpha=alphas))
grid.fit(x_train, y_train)

print(grid.best_score_)
print(grid.best_estimator_.alpha)
# -

ls = Lasso(alpha = grid.best_estimator_.alpha, normalize = True) # applied the best estimator
model(ls,x_train,y_train,x_test,y_test)

# ##### 3. Ridge
#
# We are going to do same operation for Ridge

# +
from sklearn.linear_model import Ridge

alphas = np.logspace(-3,3,num=14) # range for alpha

grid2 = GridSearchCV(estimator=Ridge(), param_grid=dict(alpha=alphas)) 
grid2.fit(x_train, y_train)

print(grid2.best_score_)
print(grid2.best_estimator_.alpha)
# -

ridge = Ridge(alpha = 0.01, normalize = True) # applied the best estimator
model(ridge,x_train,y_train,x_test,y_test)

# ##### 4. Decision Tree Regressor

from sklearn.tree import DecisionTreeRegressor
dtr = DecisionTreeRegressor()
model(dtr,x_train,y_train,x_test,y_test)

# ##### 5. Random Forest Regressor

from sklearn.ensemble import RandomForestRegressor
rf = RandomForestRegressor(n_estimators = 100, random_state = 42)
model(rf,x_train,y_train,x_test,y_test)

# Lets see the results together in dataframe

Model = ["LinearRegression","Lasso","Ridge","DecisionTreeRegressor","RandomForestRegressor"]
results=pd.DataFrame({'Model': Model,'R Squared': r_2,'CV score mean': CV})
results

# ## 5. Conclusion
#
# We applied couple of regression models on dataset. From the final dataframe, it gives opinion about the score of models and also the plots help us to understand which models is more succesful.The dataset is being predicted by each regression models by different regression methods and thus the results variate on the basis of their way of approach.

# # THANK YOU
#
# ### NAME - <NAME>
#
# ### CLASS - CSSE 2
#
# ### ROLL NUMBER - 1828102
#



StarcoderdataJupyterScriptsDedupFiltered