<jupyter_start><jupyter_text>Prediction of time required for quality inspection of mercedes cars<jupyter_code>import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
df=pd.read_csv("G:/DATA/Python/Mercedes/train.csv")
df.head()
df.shape
# for displaying all columns
pd.set_option("display.max_columns",380)
df.info()
# dropping ID as it is an unique identifier
df.drop(["ID"],inplace=True,axis=1)
df.head()
df.y.max()
# checking for correlation which is really tough since we have so many variables
df.corr()
# since there were few categorical variables we are converting them into numeric by using label encoder
from sklearn.preprocessing import LabelEncoder
le=LabelEncoder()
df[df.select_dtypes(include="object").columns] =df[df.select_dtypes(include="object").columns].apply(le.fit_transform)
# sampling 
df_x= df.iloc[:,1:]
df_y=df.iloc[:,0]
from sklearn.model_selection import train_test_split
df_x_train, df_x_test, df_y_train, df_y_test= train_test_split(df_x,df_y, test_size=0.2)<jupyter_output><empty_output><jupyter_text>using Linear Regression<jupyter_code>from sklearn.linear_model import LinearRegression
reg=LinearRegression()
reg.fit(df_x_train,df_y_train)
reg.coef_
# now we check R2 and adjR2

Rsquare= reg.score(df_x_train,df_y_train)
print("values of Rsquare is ",Rsquare)
N=df_x_train.shape[0]
K=df_x_train.shape[1]

Adjusted_Rsquare= 1-(1-Rsquare)*(N-1)/(N-K-1)
print("value of Adjusted_Rsquare is ", Adjusted_Rsquare)
pred_linear= reg.predict(df_x_test)
pred_linear.max()
error_linear= df_y_test-pred_linear
from sklearn.metrics import mean_squared_error
Rmse= np.sqrt(mean_squared_error(df_y_test,pred_linear))
Rmse
#MAPE
# Here we can see that the mape value is very high also the RMSE value is really high
MAPE=np.mean(np.abs(error_linear/df_y_test)*100)
MAPE<jupyter_output><empty_output><jupyter_text>Using Randomforest<jupyter_code>from sklearn.ensemble import RandomForestRegressor
rf=RandomForestRegressor(n_estimators=300,max_depth=7)
rf.fit(df_x_train, df_y_train)
R2=rf.score(df_x_train,df_y_train)
print("Rsquare is ",R2)
N=df_x_train.shape[0]
K=df_x_train.shape[1]

Adjusted_Rsquare= 1-(1-R2)*(N-1)/(N-K-1)
print("value of Adjusted_Rsquare is ", Adjusted_Rsquare)
pred_rf=rf.predict(df_x_test)
error_rf=df_y_test-pred_rf
Rmse= np.sqrt(mean_squared_error(df_y_test,pred_rf))
Rmse
# Here we can see the mape value
MAPE=np.mean(np.abs(error_rf/df_y_test)*100)
MAPE
# accuracy using randomforest
accuracy = 100- MAPE
accuracy
# XGBOOST REGRESSOR
from xgboost import XGBRegressor
xg=XGBRegressor(booster="gbtree",learning_rate=0.15, max_depth=7,n_estimators=200)
xg.fit(df_x_train,df_y_train)
xg.feature_importances_
r2=xg.score(df_x_train,df_y_train)
print("Rsquare is ",r2)
N=df_x_train.shape[0]
K=df_x_train.shape[1]

Adjusted_Rsquare= 1-(1-r2)*(N-1)/(N-K-1)
print("value of Adjusted_Rsquare is ", Adjusted_Rsquare)
pred_xg= xg.predict(df_x_test)
pred_xg
error_xg=df_y_test-pred_xg
Rmse= np.sqrt(mean_squared_error(df_y_test,pred_xg))
Rmse
MAPE=np.mean(np.abs(error_xg/df_y_test)*100)
MAPE
accuracy= 100 - MAPE
accuracy
# the r2 adjusted R2 and the final accuracy is good for xgboost regressor

# now using test data file that was seperatly given
test=pd.read_csv("G:/DATA/Python/Mercedes/test.csv")
test2=pd.read_csv("G:/DATA/Python/Mercedes/test.csv")
test.head()
test.drop(["ID"],axis=1,inplace=True)
test[test.select_dtypes(include="object").columns] =test[test.select_dtypes(include="object").columns].apply(le.fit_transform)
pred_test= xg.predict(test)
test_solution=pd.DataFrame({"ID":test2.ID,"y":pred_test})
test_solution<jupyter_output><empty_output><jupyter_text>PCA<jupyter_code># PCA was applied just to check how dimensionality reduction affect the accuracy of model by using xgboost regressor
from sklearn import decomposition
from sklearn.decomposition import PCA

pca=decomposition.PCA()
pca_dfx=pca.fit(df_x) 
# variation explained by principal components
variation=pca.explained_variance_ratio_.cumsum() * 100
variation

plt.plot(variation, marker=".")
# Now we select the top 28 vaiarbles which have maximum variance retained from above graph
x_pca= pca.transform(df_x)
# Now we create new x values using the selected principal components
x_pca=pd.DataFrame(x_pca)
x_pca
x_pca=x_pca.iloc[:,0:28]
x_pca
# now we do train test split
x_pca_train, x_pca_test, y_pca_train, y_pca_test= train_test_split(x_pca,df_y, test_size=0.2)
xg.fit(x_pca_train,y_pca_train)
pred_pca=xg.predict(x_pca_test)
error_pca=y_pca_test-pred_pca
Rmse= np.sqrt(mean_squared_error(y_pca_test,pred_pca))
Rmse
MAPE=np.mean(np.abs(error_pca/y_pca_test)*100)
MAPE
# here we can clearly see that pca is actually not affecting the final accuracy to that extent in case of xgboost regressor
# Now applying pca on the given test file
test_pca_x=pca.fit(test)
test_pca=pca.transform(test)
test_pca= pd.DataFrame(test_pca)
test_pca
test_pca=test_pca.iloc[:,0:28]
pred_test_pca= xg.predict(test_pca)
test_solutionpca=pd.DataFrame({"ID":test2.ID,"y":pred_test_pca})
test_solutionpca<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11181351244567364, 'fraction_numerical': 0.01619913077834848, 'mean_word_length': 7.976950354609929, 'pattern_counts': {'":': 4, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '489804', 'license': 'Apache-2.0', 'n_tokens_mistral': 2088, 'n_tokens_neox': 2001, 'n_words': 398, 'path': 'mercedes data.ipynb', 'repo_name': 'abhinavthorat15/codewarehouse', 'size': '5062'}