<jupyter_start><jupyter_text>Import necessary libraries<jupyter_code>import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline<jupyter_output><empty_output><jupyter_text>Load the dataset<jupyter_code>car_data = pd.read_csv('dataset/car data.csv')
car_data.head()<jupyter_output><empty_output><jupyter_text>Data profiling<jupyter_code>from pandas_profiling import ProfileReport
profile = ProfileReport(car_data, title='Car Price Data Report')
profile.to_file('Car_Price_Data_Report.html')<jupyter_output><empty_output><jupyter_text>EDA<jupyter_code>car_data.info()
car_data.describe()
sns.pairplot(car_data)
car_data.columns
sns.heatmap(car_data.corr(), annot=True)
sns.boxplot(x='Owner', y='Selling_Price', hue='Owner', data=car_data)
car_data = car_data[car_data['Selling_Price'] < 13]
sns.boxplot(x='Owner', y='Selling_Price', hue='Owner', data=car_data)
sns.boxplot(x='Transmission', y='Selling_Price', hue='Transmission', data=car_data)
sns.boxplot(x='Seller_Type', y='Selling_Price', hue='Seller_Type', data=car_data)
sns.boxplot(x='Fuel_Type', y='Selling_Price', hue='Fuel_Type', data=car_data)
final_car_data = car_data[['Year', 'Selling_Price', 'Present_Price', 'Kms_Driven',
       'Fuel_Type', 'Seller_Type', 'Transmission', 'Owner']]
final_car_data.head()<jupyter_output><empty_output><jupyter_text>Converting year to number of years<jupyter_code>final_car_data['Current_Year'] = 2021
final_car_data['Number_Of_Years'] = final_car_data['Current_Year'] - final_car_data['Year']
final_car_data.head()
final_car_data.drop(['Year', 'Current_Year'], axis=1, inplace=True)
final_car_data.head()<jupyter_output><empty_output><jupyter_text>Converting categorical data into numerical data using encoding<jupyter_code>final_car_data = pd.get_dummies(final_car_data, drop_first=True)
final_car_data.head()
sns.pairplot(final_car_data)
final_car_data.columns<jupyter_output><empty_output><jupyter_text>Dividing data into features and target columns<jupyter_code>X = final_car_data[['Present_Price', 'Kms_Driven', 'Owner',
       'Number_Of_Years', 'Fuel_Type_Diesel', 'Fuel_Type_Petrol',
       'Seller_Type_Individual', 'Transmission_Manual']]
y = final_car_data['Selling_Price']<jupyter_output><empty_output><jupyter_text>Maximum and Minimum Selling Price<jupyter_code>print("Maximum Selling Price: ", max(y))
print("Minimum Selling Price: ", min(y))<jupyter_output>Maximum Selling Price:  12.9
Minimum Selling Price:  0.1<jupyter_text>Split data into train and test<jupyter_code>from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)<jupyter_output><empty_output><jupyter_text>Create a model using Decision Tree Regressor<jupyter_code>from sklearn.tree import DecisionTreeRegressor
des_tree_model = DecisionTreeRegressor()
des_tree_model.fit(X_train, y_train)<jupyter_output><empty_output><jupyter_text>Predict from the model<jupyter_code>y_pred = des_tree_model.predict(X_test)
y_pred<jupyter_output><empty_output><jupyter_text>Performance measure of the model<jupyter_code>sns.distplot((y_test - y_pred))
from sklearn import metrics

def performance_measure(true, predict):
    mae = metrics.mean_absolute_error(true, predict)
    mse = metrics.mean_squared_error(true, predict)
    rmse = np.sqrt(mse)
    r2_score = metrics.r2_score(true, predict)
    print('===============================================')
    print('MAE:', mae)
    print('MSE:', mse)
    print('RMSE:', rmse)
    print('R2 Score:', r2_score)
    print('===============================================')
performance_measure(y_test, y_pred)
from sklearn.model_selection import cross_val_score

def cv_score(model, X, y):
    pred = cross_val_score(model, X, y, cv=10)
    print(pred)
    print('===============================================')
    print('Cross Validation Score:', pred.mean())
    print('===============================================')<jupyter_output><empty_output><jupyter_text>Cross Validation Score for all the data<jupyter_code>cv_score(des_tree_model, X, y)<jupyter_output>[ 0.85759822  0.80775282  0.63866002 -6.09212884 -5.94402246 -2.71594005
  0.95550493  0.86862409  0.8587604   0.82571991]
===============================================
Cross Validation Score: -0.8939470965119554
===============================================<jupyter_text>Cross Validation Score for the train data<jupyter_code>cv_score(des_tree_model, X_train, y_train)<jupyter_output>[0.91778893 0.95784968 0.92566586 0.95690705 0.95204251 0.84547602
 0.89694141 0.93146168 0.78334475 0.92400887]
===============================================
Cross Validation Score: 0.9091486770749666
===============================================<jupyter_text>Cross Validation Score for the test data<jupyter_code>cv_score(des_tree_model, X_test, y_test)<jupyter_output>[ 0.84157143  0.90749258  0.69145298  0.94875515 -0.20804119  0.80328538
  0.93223426  0.93080104  0.33391698  0.87605181]
===============================================
Cross Validation Score: 0.7057520426374397
===============================================
{'fraction_non_alphanumeric': 0.20073915580626336, 'fraction_numerical': 0.06652402256370356, 'mean_word_length': 9.243027888446216, 'pattern_counts': {'":': 0, '<': 58, '<?xml version=': 0, '>': 57, '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': '15', 'dir': 'jupyter-structured-clean-dedup', 'id': '614155', 'license': 'MIT', 'n_tokens_mistral': 2054, 'n_tokens_neox': 1801, 'n_words': 292, 'path': 'Decision_Trees/Car_Price/Decision_Trees.ipynb', 'repo_name': 'yash0307jain/ml_practice', 'size': '5141'}