<jupyter_start><jupyter_text>Reading Data<jupyter_code># importing the dataset
sp = pd.read_csv('Car details v3csv')<jupyter_output><empty_output><jupyter_text>Understanding the datasetThis dataset contains information about used cars listed on www.cardekho.comThe columns in the given dataset are as follows:    - name of the car    - year bought    - selling_price of the car    - km_driven    - fuel    - seller_type    - transmission    - Owner    - mielage    - engine    - Max power    - torque    - No. of seats<jupyter_code>sp.head(5)
sp.shape
sp.info()
# printing total number of null values in every column

sp.isnull().sum()
sp.min()
sp.max()
sp.describe()<jupyter_output><empty_output><jupyter_text>Data processing<jupyter_code># creating different columns for car brand and car name
sp['brand']=sp['name'].apply(lambda x: ' '.join(x.split(' ')[:2]) if 'Land' in x else x.split(' ')[0])
sp['car']=sp['name'].apply(lambda x: ' '.join(x.split(' ')[2:]) if 'Land' in x else ' '.join(x.split(' ')[1:]))
sp.head()
# we will be making our predicts on car brands thus we can drop the car name and name coulums
sp = sp.drop(['name','car'],axis = 1)
sp.head(10)
# using dropna() function to remove missing values  
sp = sp.dropna(how='any',axis=0) 
#verifying that our dataframe has no null values
round(100*(sp.isnull().sum()/len(sp.index)), 2)
sp.shape
#list of transmission types
trans =list(sp['transmission'].unique())
trans
# list of seller types
seller = list(sp['seller_type'].unique())
seller
# fuel type
fuel = list(sp['fuel'].unique())
fuel
# ownership type
owner = list(sp['owner'].unique())
owner
#list of all brands
brands=list(sp['brand'].unique())
brands
#list of all years
years=list(sp['year'].unique())
years
# extracting features
sp['average_kmpl'] = sp['mileage'].str.split(' ').str[0]
sp['engine_cc'] = sp['engine'].str.split(' ').str[0]
sp['power_bhp'] = sp['max_power'].str.split(' ').str[0]
sp['torque'] = sp['torque'].str.lower()
sp['rpm'] = sp['torque'].str.split('nm').str[0]
sp['Torque'] = sp['rpm'].str.split('@').str[0]
sp['torque_nm'] = sp['Torque'].str.split(' ').str[0]
sp['Torque_nm'] = sp['torque_nm'].str.split('kgm').str[0]
sp['Torq_nm'] = sp['Torque_nm'].str.split('(').str[0]
#droping extra columns
sp = sp.drop(['mileage','engine','max_power','torque','rpm','Torque','torque_nm','Torque_nm'],axis =1)
sp.head()
#rounding price and km values
# price = xK and km = xK

sp['selling_price']=sp['selling_price'].apply(lambda val: round(val/1000,2))
pd.options.display.float_format = '{:,.2f}'.format

sp['km_driven']=sp['km_driven'].apply(lambda val: round(val/1000,2))
pd.options.display.float_format = '{:,.2f}'.format
sp.head()
sp.info()
# converting appropriate data types
sp['year'] = sp['year'].apply(str)
sp['seats'] = sp['seats'].apply(str)
sp['average_kmpl'] = sp['average_kmpl'].apply(float)
sp['engine_cc'] = sp['engine_cc'].apply(int)
sp['power_bhp'] = sp['power_bhp'].apply(float)
sp['Torq_nm'] = sp['Torq_nm'].apply(float)<jupyter_output><empty_output><jupyter_text>EDA<jupyter_code>#Finding correlation
cor = sp.corr()
cor
# visulaising correlation using heatmap
plt.subplots(figsize=(10, 10))
plt.title('Correlation between each data variable')
sns.heatmap(cor, xticklabels=cor.columns.values,
            yticklabels=cor.columns.values,annot= True,linecolor="black",linewidths=2, cmap="viridis")
plt.show()
#scatter plot for numerical data with yaxis fixed as selling_price

plt.figure(figsize=[18,12])

plt.subplot(3,3,1)
plt.scatter(sp.km_driven, sp.selling_price)
plt.title('km_driven vs selling_price')
plt.subplot(3,3,2)
plt.scatter(sp.average_kmpl, sp.selling_price)
plt.title('average_kmpl vs selling_price')

plt.subplot(3,3,3)
plt.scatter(sp.engine_cc, sp.selling_price)
plt.title('engine_cc vs selling_price')
plt.subplot(3,3,4)
plt.scatter(sp.power_bhp, sp.selling_price)
plt.title('power_bhp vs selling_price')
plt.subplot(3,3,5)
plt.scatter(sp.Torq_nm, sp.selling_price)
plt.title('Torq_nm vs selling_price')<jupyter_output><empty_output><jupyter_text>Visualising categorical data<jupyter_code>plt.figure(figsize=(30, 20))

plt.subplot(3,3,1)
sns.countplot(sp.fuel)

plt.subplot(3,3,2)
sns.countplot(sp.seller_type)

plt.subplot(3,3,3)
sns.countplot(sp.transmission)

plt.subplot(3,3,4)
sns.countplot(sp.owner)

plt.subplot(3,3,5)
sns.countplot(sp.seats)

plt.show()
plt.figure(figsize=(35,30))

plt.subplot(2,1,1)
sns.countplot(sp.brand)

plt.subplot(2,1,2)
sns.countplot(sp.year)

plt.show()
# visualising categorical data vs selling_price

plt.figure(figsize = (25,15))

plt.subplot(3,3,1)
sns.boxplot(x = 'fuel',y='selling_price', data = sp)

plt.subplot(3,3,2)
sns.boxplot(x = 'owner',y='selling_price', data = sp)

plt.subplot(3,3,3)
sns.boxplot(x = 'transmission',y='selling_price', data = sp)

plt.subplot(3,3,4)
sns.boxplot(x = 'seats',y='selling_price', data = sp)

plt.subplot(3,3,5)
sns.boxplot(x = 'seller_type',y='selling_price', data = sp)

plt.show()
plt.figure(figsize=(40,30))

plt.subplot(2,1,1)
sns.boxplot(x = 'brand',y='selling_price', data = sp)

plt.subplot(2,1,2)
sns.boxplot(x = 'year',y='selling_price', data = sp)

plt.show()
# removing the outlier where car price is very high
sp.drop(sp[sp['selling_price'] > 6000].index, inplace = True)<jupyter_output><empty_output><jupyter_text>Encoding categorical data<jupyter_code>#creating dummies for categorical data by defining fucntion
def dummies(x,sp):
    cp = pd.get_dummies(sp[x], drop_first = True)
    sp = pd.concat([cp, sp], axis = 1)
    return sp

#appliying dummies function to features
sp = dummies('fuel',sp)
sp = dummies('seller_type',sp)
sp = dummies('transmission',sp)
sp = dummies('owner',sp)
sp = dummies('seats',sp)
sp = dummies('brand',sp)
sp = dummies('year',sp)

sp.head()
#dropping orignal columns after dummies
#and dropping columns that are not required for the analysis

sp = sp.drop(['brand','fuel','seller_type','transmission','owner','seats','year'],axis = 1)
sp.head()<jupyter_output><empty_output><jupyter_text>Building Model<jupyter_code># Splitting data into test and train sets
sp_train,sp_test = train_test_split(sp ,train_size = 0.7, random_state = 100)
print(sp_train.shape)
print(sp_test.shape)
# scaling the dataset
scaler = MinMaxScaler()
#applying MinMax scaling to all the numerical data
num_var = ['selling_price','km_driven','average_kmpl','engine_cc','power_bhp','Torq_nm']

sp_train[num_var] = scaler.fit_transform(sp_train[num_var])
sp_test[num_var] = scaler.transform(sp_test[num_var])
sp_train.head()
#train data
y_train = sp_train.pop('selling_price')
X_train = sp_train

#test data
y_test = sp_test.pop('selling_price')
X_test = sp_test
# linear model

model_linear = SVR(kernel='linear')
model_linear.fit(X_train, y_train)

# predict
y_pred = model_linear.predict(X_test)

#checking mean square error
np.sqrt(mean_squared_error(y_test, y_pred))
# checking the goodness of the model
metrics.r2_score(y_test, y_pred)
# non-linear model
# using rbf kernel, C=1, default value of gamma

# model
non_linear_model = SVR(kernel='rbf')

# fit
non_linear_model.fit(X_train, y_train)

# predict
y_pred = non_linear_model.predict(X_test)
#checking mean square error
np.sqrt(mean_squared_error(y_test, y_pred))
# checking the goodness of the model
metrics.r2_score(y_test, y_pred)<jupyter_output><empty_output><jupyter_text>r2_score for non-linear model is very high compared to linear model thus we can conclude that our problem is non linear in nature ans we can work on hyper parameters for a non-linear model.  Grid Search: Hyperparameter TuningTuning the model to find the optimal values of C and gamma corresponding to an RBF kernel. using 5-fold cross validation.<jupyter_code># creating a KFold object with 5 splits 
folds = KFold(n_splits = 5, shuffle = True, random_state = 101)

# range of hyperparameters
# Set the parameters by cross-validation
hyper_params = [ {'gamma': [2e-1, 2e-2, 2e-3],
                     'C': [1, 10, 100, 1000]}]


# model
model = SVR(kernel="rbf")

# set up GridSearchCV()
model_cv = GridSearchCV(estimator = model, 
                        param_grid = hyper_params, 
                        scoring= 'r2', 
                        cv = folds, 
                        verbose = 1,
                        return_train_score=True)      

# fit the model
model_cv.fit(X_train, y_train)  
# cv results
cv_results = pd.DataFrame(model_cv.cv_results_)
cv_results
# converting C to numeric type for plotting on x-axis
cv_results['param_C'] = cv_results['param_C'].astype('int')

# # plotting
plt.figure(figsize=(16,6))

# subplot 1/3
plt.subplot(131)
gamma_01 = cv_results[cv_results['param_gamma']==0.2]

plt.plot(gamma_01["param_C"], gamma_01["mean_test_score"])
plt.plot(gamma_01["param_C"], gamma_01["mean_train_score"])
plt.xlabel('C')
plt.ylabel('r2_score')
plt.title("Gamma=0.2")
plt.ylim([0.60, 1])
plt.legend(['test score', 'train score'], loc='upper left')
plt.xscale('log')

# subplot 2/3
plt.subplot(132)
gamma_001 = cv_results[cv_results['param_gamma']==0.02]

plt.plot(gamma_001["param_C"], gamma_001["mean_test_score"])
plt.plot(gamma_001["param_C"], gamma_001["mean_train_score"])
plt.xlabel('C')
plt.ylabel('r2_score')
plt.title("Gamma=0.02")
plt.ylim([0.60, 1])
plt.legend(['test score', 'train score'], loc='upper left')
plt.xscale('log')


# subplot 3/3
plt.subplot(133)
gamma_0001 = cv_results[cv_results['param_gamma']==0.002]

plt.plot(gamma_0001["param_C"], gamma_0001["mean_test_score"])
plt.plot(gamma_0001["param_C"], gamma_0001["mean_train_score"])
plt.xlabel('C')
plt.ylabel('r2_score')
plt.title("Gamma=0.002")
plt.ylim([0.60, 1])
plt.legend(['test score', 'train score'], loc='upper left')
plt.xscale('log')<jupyter_output><empty_output><jupyter_text>The plots above show some useful insights:- Non-linear models (high gamma) perform *much better* than the linear ones- None of the models tend to overfit (even the complex ones), since the training and test accuracies closely follow each otherThis suggests that the problem and the data is **inherently non-linear** in nature, and a complex model will outperform simple, linear models in this case.<jupyter_code># printing the optimal accuracy score and hyperparameters
best_score = model_cv.best_score_
best_hyperparams = model_cv.best_params_

print("The best test score is {0} corresponding to hyperparameters {1}".format(best_score, best_hyperparams))<jupyter_output>The best test score is 0.8720316837944097 corresponding to hyperparameters {'C': 1000, 'gamma': 0.2}<jupyter_text>Building and Evaluating the Final ModelLet's now build and evaluate the final model, i.e. the model with highest test accuracy.<jupyter_code># model with optimal hyperparameters

# model
model = SVR(C=1000, gamma=0.2, kernel="rbf")

model.fit(X_train, y_train)
y_pred = model.predict(X_test)


#checking mean square error
np.sqrt(mean_squared_error(y_test, y_pred))

# checking the goodness of the model
metrics.r2_score(y_test, y_pred)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1552999178307313, 'fraction_numerical': 0.025928969232173835, 'mean_word_length': 5.919772583701832, 'pattern_counts': {'":': 0, '<': 40, '<?xml version=': 0, '>': 41, 'https://': 0, 'lorem ipsum': 0, 'www.': 1, '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': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '234777', 'license': 'Apache-2.0', 'n_tokens_mistral': 4265, 'n_tokens_neox': 3981, 'n_words': 887, 'path': 'Support Vector Regression.ipynb', 'repo_name': 'ridhimanwazir/SVM', 'size': '10953'}