# ---
# 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
# ---

# # Car Price Prediction

# +
#Reading the dataset

import pandas as pd
df = pd.read_csv('car data.csv')
# -

#Reading the columns an first five rows
df.head()

#checking total rows and columns, we have 8 features and 1 target column(selling price)
df.shape

#checking the unique values of the categorical features
print(df['Fuel_Type'].unique())
print(df['Seller_Type'].unique())
print(df['Transmission'].unique())
print(df['Owner'].unique())

#Check missing or null values
df.isnull().sum()

df.describe()

df.columns

#Removing the car name column as its irrelevant
final_dataset =df[[ 'Year', 'Selling_Price', 'Present_Price', 'Kms_Driven',
       'Fuel_Type', 'Seller_Type', 'Transmission', 'Owner']]

final_dataset.head()

#Adding a new feature as current year which is 2020, this is needed to calculate the age of the car.
final_dataset['Current_year']=2020

final_dataset.head()

#Arriving at the age of the car.
final_dataset['no_year']=final_dataset['Current_year']-final_dataset['Year']
final_dataset.head()

#Inplace=True is to make it a permanent operation
#Dropping the Year and current year column
final_dataset.drop(['Year'],axis=1,inplace=True)
final_dataset.drop(['Current_year'],axis=1,inplace=True)
final_dataset.head()

#pd.dummies is used to apply one hot encoding technique, drop first is used to drop the first column from creatng dummy variable trap.
final_dataset=pd.get_dummies(final_dataset,drop_first=True)
final_dataset.head()

#Checking the correlation between various features.
final_dataset.corr()

import seaborn as sns
#checking different vizualizations with the various features available  
sns.pairplot(final_dataset)

import matplotlib.pyplot as plt
# %matplotlib inline

#cormat variable is created to be used ffor creation of a heatmap vizualization 
corrmat=final_dataset.corr()
top_corr_features=corrmat.index
plt.figure(figsize=(10,10))
#plot heat map
g=sns.heatmap(final_dataset[top_corr_features].corr(),annot=True,cmap="RdYlGn" )

final_dataset.head()
#independent and dependent feature
x=final_dataset.iloc[:,1:]
y=final_dataset.iloc[:,0]

x.head()

y.head()

#Feature importance UNderstanding which feature is important
from sklearn.ensemble import ExtraTreesRegressor
model=ExtraTreesRegressor()
model.fit(x,y)

#checking the most important features
print(model.feature_importances_)

#plot graph for the most important feature
feat_importances=pd.Series(model.feature_importances_,index =x.columns)
feat_importances.nlargest(5).plot(kind='barh')
plt.show()

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)

from sklearn.ensemble import RandomForestRegressor
rf_random =RandomForestRegressor()

#Hyper parameters
#RandomizedSearchCV helps to find out find best parameters considering how many fmax features, depth etc is used.
from sklearn.model_selection import RandomizedSearchCV
import numpy as np
n_estimators =[int(x) for x in np.linspace(start=100, stop=1200,num=12)]
print(n_estimators)

#No of trees in the forest
n_estimators =[int(x) for x in np.linspace(start=100, stop=1200,num=12)]
#no of features to consider in every split
max_features=['auto','sqrt']
#max no of levels in a tree
max_depth=[int(x) for x in np.linspace(5,30,num=6)]
#max_depth.append(None)
#Min non of samples needed to split a node
min_samples_split=[2,5,10,15,100] 
#min no of samples needed at each leaf node
min_samples_leaf=[1,2,5,10]

#create a random grid
random_grid={'n_estimators':n_estimators,
             'max_features':max_features,
             'max_depth':max_depth,
             'min_samples_split':min_samples_split,
             'min_samples_leaf':min_samples_leaf}
print(random_grid)

from sklearn.model_selection import RandomizedSearchCV
rf=RandomForestRegressor()

# + active=""
# rf_random = RandomizedSearchCV(estimator=rf, param_distributions=random_grid, scoring='neg_mean_squared_error', n_iter=10, cv=5, verbose=2, random_state=42, n_jobs=1)
# -

rf_random.fit(x_train,y_train)

predictions = rf_random.predict(x_test)

predictions

#Checking the difference from our pedictions to our actuals
sns.distplot(y_test-predictions)

#Creating vizualizations comaring our predictions with our actual values.
plt.scatter(y_test,predictions)

# +
import pickle 
#open a file where you can store the data
#file =open('random_forest_regression_model.pkl','wb')

#dump information to that file
pickle.dump(rf_random,open('random_forest_regression_model.pkl','wb'))
# -



StarcoderdataJupyterScriptsDedupFiltered