<jupyter_start><jupyter_text>Predicting the price of the car based on the given Features<jupyter_code># Importing Libraries

import pandas as pd
import numpy as np
df = pd.read_csv('C:\\Dphi_Notes\\Examples\\car_data.csv')   # Reading our DataFrame as CSV file
df
df.head()       # First five rows of dataframe
df.shape       # To know number of Rows and Columns (rows, columns)
# To know the unique values of that particular variable/feature

print(df['Seller_Type'].unique())
print(df['Transmission'].unique())
print(df['Owner'].unique())
print(df['Fuel_Type'].unique())
# check missing or null values

df.isnull().sum()
# To know the details

df.describe()
df.columns
# car_name is removed because we can't predict target variable by the car_name

final_dataset = df[['Year', 'Selling_Price', 'Present_Price', 'Kms_Driven',
       'Fuel_Type', 'Seller_Type', 'Transmission', 'Owner']]
final_dataset.head()
final_dataset['Current_year'] = 2021  # Adding a new Feature to the DataFrame
final_dataset.head()   # checking current_year (Feature) is added or not
# To know how many years the car is old  

final_dataset['num_year'] = final_dataset['Current_year']-final_dataset['Year']
final_dataset.head()   # we can see the num_year Column
# Droping irrelevant column-'Year'

final_dataset.drop(['Year'], axis=1, inplace=True)
final_dataset.head()          # Year Column is dropped
# Droping another irrelevant column-'Current_year'

final_dataset.drop(['Current_year'], axis=1, inplace=True)
final_dataset.head()     # Current_year is droped
# Creating dummies for Fuel_Type, Seller_Type, Transmission, Owner Variables (One-Hot Encoding)

final_dataset = pd.get_dummies(final_dataset, drop_first=True) 
final_dataset.head()
# How one Feature is Correlated to other Feature

final_dataset.corr()
# Importing the Library Seaborn

import seaborn as sns
# we can see pair of each variables by using pairplot

sns.pairplot(final_dataset)
# importing Matplotlib Library

import matplotlib.pyplot as plt
%matplotlib inline
# to see the Correlation graph of pair of top features

corrmat=final_dataset.corr()
top_corr_features=corrmat.index
plt.figure(figsize=(10,10))         # size of graph

# plot heat map (From the graph Dark Green is positively correlated & Dark Red is Negatively correlated)

g=sns.heatmap(final_dataset[top_corr_features].corr(), annot=True, cmap="RdYlGn")
final_dataset.head()
# Y=Selling_Price is the dependent variable and remaining columns are independent variables i.e X

X = final_dataset.iloc[:,1:]
Y = final_dataset.iloc[:,0]
X.head()      # Independent variables/Input Variables
Y.head()     # Dependent variables/Target Variable
# Importing sklearn Library
# Feature Importance (to know which features are more important in the DataFrame)

from sklearn.ensemble import ExtraTreesRegressor
model = ExtraTreesRegressor()
model.fit(X,Y)
# To know the % of the Features which is more important (0.358...has heighest priority and so on)

print(model.feature_importances_)
# Plot graph of feature importances for better visualization

feat_importances = pd.Series(model.feature_importances_, index=X.columns)
feat_importances.nlargest(5).plot(kind='barh')            # 5=Number of Features
plt.show()
# Now train, test, split the data

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)    # here 0.2=test data
X_train.shape      # Shape of X_train and can check with the other features as well
# RandomForest Regressor is used for Hyperparameter Tuning (we can tune the parameters)

from sklearn.ensemble import RandomForestRegressor
rf_random = RandomForestRegressor()
# Hyperparameters

import numpy as np
n_estimators = [int(x) for x in np.linspace(start=100, stop=1200, num=12)]
print(n_estimators)
from sklearn.model_selection import RandomizedSearchCV
#Randomized Search CV (It will perform faster than 'Grid Search CV' so we prefer 'Randomized Search CV')

# Number of trees in random forest
n_estimators = [int(x) for x in np.linspace(start = 100, stop = 1200, num = 12)]

# Number of features to consider at every split
max_features = ['auto', 'sqrt']

# Maximum number of levels in tree
max_depth = [int(x) for x in np.linspace(5, 30, num = 6)]

# max_depth.append(None)
# Minimum number of samples required to split a node
min_samples_split = [2, 5, 10, 15, 100]

# Minimum number of samples required at each leaf node
min_samples_leaf = [1, 2, 5, 10]
# Create the random grid (Taking as a key value)
# Use the random grid to search for the best hyperparameters

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)      # Printing the random grid
# First create the base model to tune

rf = RandomForestRegressor()
# Random search of parameters, using 3 fold cross validation=cv (cv means among 5, 1 is given for test and remaining 4 to test)
# search across 100 different combinations

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)
rf_random.best_params_
rf_random.best_score_
# Predicting the output

predictions=rf_random.predict(X_test)
predictions
sns.distplot(Y_test-predictions)
# Scatterplot

plt.scatter(Y_test, predictions)   # by the graph we can see Plotting is Linearly available (i.e. prediction is good)
from sklearn import metrics
# Printing the MAE-Mean Absolute Error, MSE-Mean Squared Error, RMSE-Root Mean Squared Eoore

print('MAE:', metrics.mean_absolute_error(Y_test, predictions))
print('MSE:', metrics.mean_squared_error(Y_test, predictions))
print('RMSE:', np.sqrt(metrics.mean_squared_error(Y_test, predictions)))
import pickle

# open a file, where you want to store the data

file = open('random_forest_regression_model.pkl', 'wb')

# dump information to that file (Pickled file is a serialized file, used for deployment)

pickle.dump(rf_random, file)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.0923499919133107, 'fraction_numerical': 0.011644832605531296, 'mean_word_length': 5.177822177822178, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '388330', 'license': 'MIT', 'n_tokens_mistral': 2014, 'n_tokens_neox': 1904, 'n_words': 642, 'path': 'Price_prediction.ipynb', 'repo_name': 'NikhithaReddy0510/SellingPrice_Prediction_ML', 'size': '6183'}