<jupyter_start><jupyter_text>Reading the Data<jupyter_code>df = pd.read_csv('Cleaned.csv')
df.head()
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 90346 entries, 0 to 90345
Data columns (total 13 columns):
 #   Column               Non-Null Count  Dtype  
---  ------               --------------  -----  
 0   Make                 90346 non-null  object 
 1   Model                90346 non-null  object 
 2   Model_Year           90346 non-null  int64  
 3   Fuel_Type            90346 non-null  object 
 4   Transmission         90346 non-null  object 
 5   Engine_Capacity(cc)  90346 non-null  int64  
 6   Kms_Driven           90346 non-null  int64  
 7   Color                90346 non-null  object 
 8   Assembly             90346 non-null  object 
 9   Body_Type            90346 non-null  object 
 10  Price                90346 non-null  int64  
 11  Capacity_KWh         90346 non-null  float64
 12  Condition            90346 non-null  object 
dtypes: float64(1), int64(4), object(8)
memory usage: 9.0+ MB<jupyter_text>Checking Null values<jupyter_code>df.isnull().sum()<jupyter_output><empty_output><jupyter_text>Data Cleaning<jupyter_code>print(df['Fuel_Type'].unique())
print(df['Transmission'].unique())
print(df['Color'].unique())
print(df['Assembly'].unique())
print(df['Body_Type'].unique())
print(df['Condition'].unique())
df['Color'].value_counts()
re.search(r'white',df['Color'][0])
white = list(df['Color'][df['Color'].str.lower().str.contains(r'white')])
blue = list(df['Color'][df['Color'].str.lower().str.contains(r'blue')])
red = list(df['Color'][df['Color'].str.lower().str.contains(r'red')])
black = list(df['Color'][df['Color'].str.lower().str.contains(r'black')])
silver = list(df['Color'][df['Color'].str.lower().str.contains(r'silver')])
grey = list(df['Color'][df['Color'].str.lower().str.contains(r'grey')])
golden = list(df['Color'][df['Color'].str.lower().str.contains(r'golden')])
green = list(df['Color'][df['Color'].str.lower().str.contains(r'green')])
brown = list(df['Color'][df['Color'].str.lower().str.contains(r'brown')])
rose = list(df['Color'][df['Color'].str.lower().str.contains(r'rose')])
gun = list(df['Color'][df['Color'].str.lower().str.contains(r'gun')])
gray = list(df['Color'][df['Color'].str.lower().str.contains(r'gray')])
metallic = list(df['Color'][df['Color'].str.lower().str.contains(r'metallic')])
biege = list(df['Color'][df['Color'].str.lower().str.contains(r'beige')])
df['Color'].replace(white,'White', inplace=True)
df['Color'].replace(blue,'Blue', inplace=True)
df['Color'].replace(red,'Red', inplace=True)
df['Color'].replace(black,'Black', inplace=True)
df['Color'].replace(silver,'Silver', inplace=True)
df['Color'].replace(grey,'Grey', inplace=True)
df['Color'].replace(golden,'Golden', inplace=True)
df['Color'].replace(green,'Green', inplace=True)
df['Color'].replace(brown,'Brown', inplace=True)
df['Color'].replace(rose,'Rose', inplace=True)
df['Color'].replace(gun,'Gun Metallic', inplace=True)
df['Color'].replace(gray,'Grey', inplace=True)
df['Color'].replace(metallic,'Metallic', inplace=True)
df['Color'].replace(biege,'Beige', inplace=True)
df['Color'].str.lower().replace(to_replace=r'Rose.+', value='Rose', regex=True, inplace=True)
df['Color'].replace('Turqouise','Turquoise', inplace=True)
pd.options.display.max_rows = None
df['Color'].value_counts()
golden = ['Cooper Gold', 'Flexin Mica', 'Gold Wight','Dorado Gold','Goldan','Sand Gold','G','Gooldan','Flaxen Mica',
         'Light Gold']
beige = ['Sand Biege', 'Sand Baige','Cool Bebige', 'Bage','Pearl Bedge','Sandbeig','Beig','Jerma Badge','Sand Beigh']
grey = ['Charcoal', 'Graphite', 'Mouse Couler','Rich Espresso','Grefite','Surmai', 
        'Indica, Graphite','Surmayi','Graphite Metalic', 'Smoke Gr', 'Gary','Italic','Sedan','Smoke']
gun = ['Gan Matalik', 'Ganmatlic', 'Dalmatalak', 'Gan Matelk','Gan Matalik Garr']
orange = ['Light Orange Or Peach Color', 'Rust Orange']
rose = ['Ross Matlk', 'Shalimar Roze','Roz Mataiq']
yellow = ['Dull Yellow', 'Lemon', 'Lime Yellow','Light Yellow','Neon Glittery Yellow']
maroon = ['Boardox', 'Mehroon']
silver = ['S','Silwer','Ssss','Sliver','Yslr','Yslr']
red = ['Rid Wien','Peral Wine','Champagne','Wine','Vine','Sky Bue','Shapain','Champagne ']
blue = ['Saloon', 'Cerulean B','Peal Bule','???','Trim','Spacio','Salon','Beach Color','Ferozi','Sea Pearl',
       'Peral','Pearl Colour','Bule Mint','Shade','Mateluck','Moonglow Pearl','Abc','Skye ','By6','Sky','Sky ',
       'Aq Jade']
magenta = ['Magneta', 'Magenta','B']
purple = ['Light Purple', 'Levander', 'Pearl Purple ','Purple Lite','Lavendar','Purpal','Voilet Metalic','Levander',
         'Lavander','Bluish Purple','Lavender','Levander ']
pink = ['P With', 'Tea Pink ', 'Light Pin','Pinc','Baby Pink','Piyazi','Onion Color Type','Tee Pink',
       'Copper Pink','Tea Pink','Light Pink']
brown = ['Coffee', 'Biscuit', 'Skin','بسکوٹی','Army ','Brawn','Bhatti Colour','Chocolate Colo','Char Coal',
        'Biscut','Fol Bage','Bronze Micca','Malti','Bhati Japani Color','Cheku Colour', 'Army Clr',
        'Musterd','Army Desert Color','Mat Flax M ','Foan', 'Mustard','Bronze Mica','Bowla','Meca Bronze',
        'Copper','Camel','Chocolate Color','Evo','Cream', 'Creem Color', 'Peach', 'None', 'Indica','Mist','Creem',
        'Mustard ','Cream Color','Carem','Careem', 'Camel ']
green = ['Grinish', 'Mehndi Colour', 'Angori','Jade','Grenish', 'Dark Gren', 'Leith Mahande','Granite',
        'Grips Gareen','Sea Gerrn','Angroi','Ovals','Mehndi Color','Olive','Grapes Color','Olive Mica',
        'Smoky Greeen','Smoke Garen','Lit Gareen','Gr Vr Matl','Mandi Color','Moigia Color','Grin','Gm','Levander',
        'Shalimar']
black = ['Dark', 'Cool Batch','Mica Dark','Mande','Moongia','Shalimar Stone']
metallic = ['Matalic', 'Panthera Metal', 'Metalic','Two Toned Color','Matlic','Matlic Color','Double Sheet',
           'Penthra Metal','Pentha Metal','Modern Steel 22,85,','Matelic','Metal Stream Met','Modren Steel',
           'Mettalic Steel','Metalic F','Penthera Metal','Polish Metalic','Modern Steel Matlic',
           'Bold Matalic Beig', 'Sea Matalic', 'Metalic Cooper','Anthera Metal Color', 'Polish Metal Metalic',
           'Polish Matilic','T','Bronze Mitallic','Metallik','R Metelic','Modern Steel','Modern Steel Matelic'
            , 'Gun Metallic', 'Two Tone','Matellic Steel'] 
white = ['Pearl','All','Other','Moonlight','All Colours Are Availabl','Unknown','Genuine Colour', 'Pics',
        'Same As Picture', 'Bright','Watch Pictures','As Seen In The Pics','Check Pictures',
        'All Colors Are Available','See In Pics','Titania','Wite','Apky Samny','Special Edition',
        'Mehran','Not Defined','Any Color Available','Family Used Car Good Looking',' ', 
         'Outer Shawer Iner Jenean','Unkhown','Good','As You Can See Pictures','Tose Miss','Champion',
        'As You Can See','Palwheth','Hvhhb','No Idea','Metalic S','Steel','Shown In Pix','All Colours Available',
        'Parl Wahit','Pearl Whte','14/ 16','Nice','Defined','Imported','One Pis Tachinig','Multi',
        'All Colours Are Available','All Colours']
urban =  ['Urban Titaniu', 'Titanium', 'Urban Titinum', 'Titanium', 'Urban Titenum','Arabian Titanium','Titanium Colour',
'Urban Titan','Urban','Titanium Mettalli','Urban Titnum','Urban Titenium','Arban Titenium','Urbantitanium',
'Titanium Mettalic ','Arabic Matalic','Titanium','Urban Titanium ','Titanium ','Urban ','Titanium Mettallic',
         'Titanium Mettalic','Urban Titanium']
colors = {tuple(golden):'Golden', tuple(beige) :'Beige', tuple(grey):'Grey', tuple(gun):'Gun Metallic', tuple(orange):'Orange',
          tuple(rose):'Rose', tuple(yellow): 'Yellow', tuple(maroon):'Maroon', tuple(silver):'Silver',
          tuple(red):'Red', tuple(blue):'Blue', tuple(magenta):'Magenta', tuple(purple):'Purple', 
          tuple(pink):'Pink', tuple(brown):'Brown',  tuple(green):'Green',  tuple(black):'Black', tuple(metallic):'Metallic', tuple(white):'White'
          ,tuple(urban):'Urban Titanium'}

for i in colors:
#     print((i))
    df['Color'].replace(i,colors[i], inplace=True)
df['Color'].value_counts()
df['Color'].replace('Unlosted','Unlisted', inplace=True)
df['Color'].replace('Unlisted','White', inplace=True)
df['Color'].replace('Golden','Gold', inplace=True)
df['Color'].replace('Beige','Brown', inplace=True)
df['Color'].replace('Turquoise','Blue', inplace=True)
df['Color'].replace('Navy','Blue', inplace=True)
df['Color'].replace('Indigo','Purple', inplace=True)
df['Color'].replace('Magenta','Pink', inplace=True)
df['Color'].replace('Rose','Pink', inplace=True)
df['Color'].replace('Bronze','Brown', inplace=True)


df['Color'].replace('Urban Titanium','Metallic', inplace=True)

df['Color'].value_counts()
print(df['Fuel_Type'].unique())
print(df['Transmission'].unique())
print(df['Color'].unique())
print(df['Assembly'].unique())
print(df['Body_Type'].unique())
print(df['Condition'].unique())
df.head()
df= pd.read_csv('Cleaned2.csv')<jupyter_output><empty_output><jupyter_text>Data Preparation<jupyter_code>df.shape
df['Price'].max()
df['Price'].min()
plt.figure(figsize=(15,10))
sns.histplot(x='Price',data=df, kde=True )
print(df['Price'].min())
print(df['Price'].median())
print(df['Price'].max())
pd.options.display.max_rows = False
df[df['Price'] > 70000000]
plt.figure(figsize=(15,6))
sns.set_theme(style="whitegrid")
sns.boxplot(x= df['Price'])
data_new.head()
df.head()
df.drop(['Make', 'Model'], axis=1,  inplace=True)
check_df = df.copy()
check_df['Body_Type'].value_counts()
df.info()
obj_col = df.select_dtypes(object).columns
obj_col
df[obj_col] = df[obj_col].astype('category')
for i in obj_col:
    df[i] = df[i].cat.codes
df.head()
# sns.displot(df)
print(df['Fuel_Type'].unique())
print(df['Transmission'].unique())
print(df['Color'].unique())
print(df['Assembly'].unique())
print(df['Body_Type'].unique())
print(df['Condition'].unique())
# print(df['Category'].unique())<jupyter_output>[5 1 3 0 2 4]
[0 1]
[13  0  1  5 12  4 11  3  2  9  6  7 10  8 14]
[0 1]
[16  6 15 10 13  4  8  3  1  0  5 18 22 19  9  7 17 14 11 12  2 20 21]
[0 4 2 1 3]<jupyter_text>Rescaling<jupyter_code>df.shape
df.columns
df.head()
print(df['Fuel_Type'].unique())
print(df['Transmission'].unique())
print(df['Color'].unique())
print(df['Assembly'].unique())
print(df['Body_Type'].unique())
print(df['Condition'].unique())
# print(df['Category'].unique())
df = df.sample(frac=1, random_state=123)
df.head()
df.shape
df.shape[0]*0.7
train = df[:63242]
test  =df[63242:]<jupyter_output><empty_output><jupyter_text>Correlation Between Variables Linear regression assumes the independent variables are not related with each other. If the correlation degree is high, it will cause problems when we fit the model.To check multicollinearity, I will use heatmap and VIF.<jupyter_code>plt.figure(figsize = (20, 15))
sns.heatmap(train.corr(method ='pearson'), annot=True, linewidths=.5)
plt.show()
plt.figure(figsize=(5,10))

cor_df = pd.DataFrame({'Price' : df.corr()['Price'].values},
                     index = df.corr()['Price'].index)

sns.heatmap(cor_df, annot=True, cmap='viridis')

plt.show()
from scipy import stats

num_columns = train.select_dtypes(exclude='object').columns

for i in list(num_columns):
    pearson_coeff, p_value = stats.pearsonr(train[i], train['Price'])
    print(i.capitalize())
    print(f'Pearson Co-relation: {pearson_coeff}')
    print(f'P-Value: {p_value}')
    if p_value<0.05:
        print('Correlation is Significant')
    else:
        print('Correlation is Insignificant')
    print('')

drop = []
for i in list(num_columns):
    pearson_coeff, p_value = stats.pearsonr(train[i], train['Price'])
    if p_value > 0.05:
        drop.append(i)

drop
train.drop(['Capacity_KWh'], axis=1, inplace=True)
test.drop(['Capacity_KWh'], axis=1, inplace=True)
df.head()
hetro = train.copy()
hetro = (hetro-hetro.min())/(hetro.max()- hetro.min())
var = hetro.var().sort_values(ascending = False)
var
train.drop(['Model_Year'], axis=1, inplace=True)
test.drop(['Model_Year'], axis=1, inplace=True)<jupyter_output><empty_output><jupyter_text>Building the Model<jupyter_code>!pip install cloudpickle
train.columns
features = train.columns.drop('Price')
target = ['Price']
from sklearn.preprocessing import QuantileTransformer

transform = QuantileTransformer(n_quantiles=90346)
trns = transform.fit(train[features])
import pickle

pickle.dump(trns, open('q_transform.pkl', 'wb'))
X_train = trns.transform(train[features])
X_test = trns.transform(test[features])

X_train = pd.DataFrame(X_train, columns=features)
X_test = pd.DataFrame(X_test, columns=features)
y_train = train[target]
y_test = test[target]
from sklearn.metrics import mean_absolute_error

LG_ = LinearRegression()
SV_ = SVR()
KN_ = KNeighborsRegressor()
DT_ = DecisionTreeRegressor(random_state=123)
GB_ = GradientBoostingRegressor(random_state=123)
RF_ = RandomForestRegressor(random_state=123)

models = [LG_, SV_, KN_, DT_, GB_, RF_,]
model_name = [ 'Linear Regression', 'Support Vector Regression', 'K Nearest Neighbor', 
              'Decision Tree', 'Gradient Boost', 'Random Forest' ]


means = []
r2_score_ = []
kf = KFold(5, True, random_state=123)

for i in range(len(models)):
    maes = []
    r2s = []
    model = models[i]


    model.fit(X_train, y_train)
    prediction = model.predict(X_test)
    mae = mean_absolute_error(prediction, y_test)
    r2 = r2_score(y_test, prediction)
    maes.append(mae)
    r2s.append(r2)
        
    means.append(np.mean(maes))
    r2_score_.append(np.mean(r2s))
    
    
mod_comp_def = pd.DataFrame({'Models' : model_name, 'Mean Absolute Error' : means,
                            'R2_Score' : r2_score_}).set_index('Models')
mod_comp_def
parameter_space_LG = {
    'fit_intercept' : [True, False] ,
    'normalize' : [True, False] ,
    'copy_X' : [True, False] ,
    'positive' : [True, False]
}

parameter_space_SV = {
    "kernel": ["poly", "linear", "rbf", "sigmoid"],
        "degree": [3, 5],
        "coef0": [0, 3, 7],
        "gamma":[1e-3, 1e-1, 1/X_train.shape[1]],
        "C": [1, 10, 100],
}

parameter_space_KN = {
    'n_neighbors' : [1,20,50],
    'weights' : ['uniform', 'distance'],
    'algorithm' : ['auto', 'kd_tree'],
    'leaf_size' : [1,20,100],
    'p' : [1,2],
}

parameter_space_DT = {
    'criterion' : ['mse', 'mae'] ,
    'splitter' : ['best', 'random'],
    'max_depth' : [5,20,50],
}

parameter_space_GB = {
    'loss' : ['ls', 'lad', 'quantile'],
    'learning_rate' : [0.1,0.2, 0.5],
    'n_estimators' : [80, 100, 180],
    'criterion' : ['mse', 'mae'],
}

parameter_space_RF = {
    'n_estimators' : [100,120],
    'criterion' : ['mse', 'mae'],
    'max_depth' : [10,15,30],
}

from sklearn.model_selection import RandomizedSearchCV


LG = LinearRegression()
SV = SVR()
KN = KNeighborsRegressor()
DT = DecisionTreeRegressor(random_state=123)
GB = GradientBoostingRegressor(random_state=123)
RF = RandomForestRegressor(random_state=123)

# kf = KFold(5, True, random_state=123)

models = [LG, DT, GB, RF,]
model_name = [ 'Linear Regression', 
              'Decision Tree', 'Gradient Boost', 'Random Forest' ]
parameter_space = [parameter_space_LG, 
                  parameter_space_DT, parameter_space_GB, parameter_space_RF]

for i in range(4):
    clf = RandomizedSearchCV(models[i],parameter_space[i] , scoring='neg_mean_absolute_error',
                            n_jobs=-1, cv=3)

    clf.fit(X_train, y_train)
    print(f'{model_name[i]}:')
    print("Best parameters:")
    print(clf.best_params_)
    print('')
features = ['wheelbase', 'carwidth', 'enginesize', 'boreratio', 'aspiration_turbo',
       'carbody_hatchback', 'carbody_sedan', 'drivewheel_fwd',
       'drivewheel_rwd', 'enginelocation_rear', 'enginetype_l',
       'enginetype_ohc', 'enginetype_ohcv', 'cylindernumber_five',
       'cylindernumber_four', 'cylindernumber_six', 'fuelsystem_2bbl',
       'fuelsystem_mpfi', 'Category_Medium_Range', 'Category_Expensive_Cars']
target = ['price']
Linear Regression:
Best parameters:
{'positive': False, 'normalize': False, 'fit_intercept': True, 'copy_X': True}

Decision Tree:
Best parameters:
{'splitter': 'best', 'max_depth': 10, 'criterion': 'mse'}

Gradient Boost:
Best parameters:
{'n_estimators': 180, 'loss': 'huber', 'learning_rate': 0.5, 'criterion': 'mse'}

Random Forest:
Best parameters:
{'n_estimators': 100, 'max_depth': 15, 'criterion': 'mae'}

from sklearn.metrics import mean_absolute_error

LG_ = LinearRegression(copy_X=True, fit_intercept=True, normalize=False, positive=False,)
DT_ = DecisionTreeRegressor(criterion='mse', max_depth=10, splitter='best',random_state=123)
GB_ = GradientBoostingRegressor(criterion='mse', learning_rate=0.5, 
                                loss='huber', n_estimators=180, random_state=123)
RF_ = RandomForestRegressor(criterion='mae', max_depth=15, n_estimators=100,random_state=123)

models = [LG_, DT_, GB_, RF_,]
model_name = [ 'Linear Regression', 'Decision Tree', 'Gradient Boost', 'Random Forest' ]


means = []
r2_score_ = []
kf = KFold(5, True, random_state=123)

for i in range(len(models)):
    maes = []
    r2s = []
    model = models[i]

    model.fit(X_train, y_train)
    prediction = model.predict(X_test)
    mae = round(mean_absolute_error(prediction, y_test),2)
    r2 = round(r2_score(y_test, prediction),2)
    maes.append(mae)
    r2s.append(r2)
        
    means.append(np.mean(maes))
    r2_score_.append(np.mean(r2s))
    
    
with_rndm = pd.DataFrame({'Models' : model_name, 'Mean Absolute Error' : means,
                            'R2_Score' : r2_score_}).set_index('Models')
with_rndm
mod_comp_def.loc[['Linear Regression', 'Decision Tree', 'Gradient Boost', 'Random Forest']]<jupyter_output><empty_output><jupyter_text>Finally<jupyter_code>model_name = 'Randome Forest'
model = RandomForestRegressor(criterion='mae', max_depth=15, n_estimators=100,random_state=123)
# model.predict(X_test)
model.fit(X_train, y_train)
# model.fit(X_train, y_train)
print(model_name)
print(f'r2 score: {round(r2_score(y_test, model.predict(X_test)), 2)}')
print(f'Mean absolute error: {round(mean_absolute_error(model.predict(X_test), y_test),2)}')
import pickle
pickle_out = open('model-pakwheels-carprice.pkl', "wb")
pickle.dump(model, pickle_out)
pickle_out.close()<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup