<jupyter_start><jupyter_text>Question 1: Find the factors which are responsible for Price of the car ,                                                                   Question 2: Write the equation to find price of a car	,							                                                                Question 3: How the factors are affecting the price		,						                                                                      Question 4: What is the R^2 and adj. R^2 ? What is the values of both for these module		,						Question 4: What is RMSE. How to reduce it ?<jupyter_code>import pandas as pd 
import numpy as np
from sklearn.preprocessing import StandardScaler,LabelEncoder
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import SGDRegressor,LinearRegression,GammaRegressor
from sklearn.model_selection import KFold, cross_val_score, train_test_split # Model evaluation
from sklearn.preprocessing import LabelEncoder, OneHotEncoder, StandardScaler # Preprocessing
from sklearn.linear_model import Lasso, Ridge, ElasticNet, RANSACRegressor, SGDRegressor, HuberRegressor, BayesianRidge # Linear models
from sklearn.ensemble import RandomForestRegressor, BaggingRegressor, AdaBoostRegressor, GradientBoostingRegressor, ExtraTreesRegressor  # Ensemble methods
from sklearn.svm import SVR, SVC, LinearSVC  # Support Vector Regression
from sklearn.tree import DecisionTreeRegressor # Decision Tree Regression
from sklearn.neighbors import KNeighborsRegressor
from sklearn.metrics import mean_squared_error,r2_score
import warnings
warnings.filterwarnings('ignore')
data=pd.read_excel(r'C:\Users\91708\Downloads\Regression-20210605T094615Z-001\Regression\Case1.xlsx')
data<jupyter_output><empty_output><jupyter_text>EDA<jupyter_code>data.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 11914 entries, 0 to 11913
Data columns (total 16 columns):
 #   Column             Non-Null Count  Dtype  
---  ------             --------------  -----  
 0   Make               11914 non-null  object 
 1   Model              11914 non-null  object 
 2   Year               11914 non-null  int64  
 3   Engine Fuel Type   11911 non-null  object 
 4   Engine HP          11845 non-null  float64
 5   Engine Cylinders   11884 non-null  float64
 6   Transmission Type  11914 non-null  object 
 7   Driven_Wheels      11914 non-null  object 
 8   Number of Doors    11908 non-null  float64
 9   Market Category    8172 non-null   object 
 10  Vehicle Size       11914 non-null  object 
 11  Vehicle Style      11914 non-null  object 
 12  highway MPG        11914 non-null  int64  
 13  city mpg           11914 non-null  int64  
 14  Popularity         11914 non-null  int64  
 15  Price              11914 non-null  int64  
dtypes: float64(3), int64(5[...]<jupyter_text>MISSING VALUES<jupyter_code>missing=data.isnull().sum()<jupyter_output><empty_output><jupyter_text>The list of variables having missing values greater than zero<jupyter_code>missing[missing>0]
cat_var=data.select_dtypes(include=object).columns.to_list()#categorical variable
num_var=data.select_dtypes(include=np.number).columns.to_list()#numeric variable
print(data[cat_var].isnull().sum())
print(data[num_var].isnull().sum())
data['Engine Fuel Type'].value_counts()
data['Market Category'].value_counts()
data['Engine Fuel Type']=data['Engine Fuel Type'].fillna('regular unleaded')
data['Market Category']=data['Market Category'].fillna('Crossover')
data[cat_var].isnull().sum()
def missing_treat(x):
    x=x.fillna(x.median())
    return x
data[num_var]=data[num_var].apply(lambda x: missing_treat(x))
data[num_var].isnull().sum()<jupyter_output><empty_output><jupyter_text>UNIVARIATE ANALYSIS<jupyter_code>i=1
plt.figure(figsize=(15,20))
for j in num_var:
    plt.subplot(4,2,i)
    sns.histplot(data=data,x=data[j],kde=True,color='red')
    i+=1<jupyter_output><empty_output><jupyter_text>none of the graph is not exactly normaly distributed ,so we impute missing values by its median  OUTLIERS<jupyter_code>num_var1=[
 'Engine HP',
 'Engine Cylinders',
 'Number of Doors',
 'highway MPG',
 'city mpg',
 'Popularity']
i=1
plt.figure(figsize=(15,20))
for j in num_var1:
    plt.subplot(4,3,i)
    sns.boxplot(data=data,x=data[j])
    i+=1<jupyter_output><empty_output><jupyter_text>as we can see above the variables is containing outlier<jupyter_code>def outlier_treat(x):
    upper=x.quantile(.95)
    lower=x.quantile(.05)
    x=np.where(x>upper,upper,x)
    x=np.where(x<lower,lower,x)
    return x
data[num_var1]=data[num_var1].apply(lambda x: outlier_treat(x))
i=1
plt.figure(figsize=(15,20))
for j in num_var1:
    plt.subplot(4,3,i)
    sns.boxplot(data=data,x=data[j])
    i+=1<jupyter_output><empty_output><jupyter_text>oulier are removed  BIVARIATE ANALYSIS<jupyter_code>print(cat_var)
print(num_var)
f,ax=plt.subplots(1,1,figsize=(20,8))
sns.barplot(data=data,x='Make',y='Price',ax=ax)
plt.xticks(rotation=90)
plt.show()<jupyter_output><empty_output><jupyter_text>we can see that the buggati has the highest mean price<jupyter_code>f,ax=plt.subplots(1,1,figsize=(20,8))
sns.barplot(data=data,x='Transmission Type',y='Price',ax=ax,hue='Engine Cylinders')
plt.xticks(rotation=90)
plt.show()<jupyter_output><empty_output><jupyter_text>we can see that automated_manual transmission type with 8 cylinder has the highest price<jupyter_code>f,ax=plt.subplots(1,1,figsize=(20,8))
sns.barplot(data=data,x='Engine Fuel Type',y='Price',ax=ax,hue='Engine Cylinders')
plt.xticks(rotation=90)
plt.show()<jupyter_output><empty_output><jupyter_text>we can see that flex-fuel (premium unleaded required/E85) engine feul type with 8 cylinder has the highest price.<jupyter_code>f,ax=plt.subplots(1,1,figsize=(20,8))
sns.barplot(data=data,x='Driven_Wheels',y='Price',ax=ax,hue='Transmission Type')
plt.xticks(rotation=90)
plt.show()<jupyter_output><empty_output><jupyter_text>all wheel drive with automated_manual has the highest price<jupyter_code>f,ax=plt.subplots(1,1,figsize=(20,8))
sns.barplot(data=data,x='Vehicle Size',y='Price',ax=ax,hue='Transmission Type')
plt.xticks(rotation=90)
plt.show()<jupyter_output><empty_output><jupyter_text>we can see that large vehicle size with automated_manual drive has the highest price<jupyter_code>i=1
plt.figure(figsize=(15,20))
for j in num_var1:
    plt.subplot(4,2,i)
    sns.scatterplot(y=data['Price'],x=data[j])
    i+=1
sns.heatmap(data.corr(),linewidths=1,cmap='autumn_r')
plt.show()<jupyter_output><empty_output><jupyter_text>we can see that as Engine HP increases Price is increase                                                                                                                                                                                                                                                                            -as MPG increase price decreases  FEATURE ENGINEERING<jupyter_code>lb=LabelEncoder()
cat_var
data1=data.copy()
data1['Model']=data1['Model'].apply(lambda x: str(x))#convert model's unique value to str
for i in cat_var:
    data1[i]=lb.fit_transform(data1[i])
data1
sc=StandardScaler()
scaled=sc.fit_transform(data1.drop(columns='Price'))
scaled=pd.DataFrame(scaled,columns=data1.drop(columns='Price').columns)
scaled
data1.corr()[['Price']].sort_values(by='Price').plot(kind='bar',color='red')<jupyter_output><empty_output><jupyter_text>We can see the importance of feature importance of each feature in determinig of price of car  Model Selection<jupyter_code>X=scaled
Y=data[['Price']]
x_train,x_test,y_train,y_test=train_test_split(X,Y,test_size=0.3,random_state=0)

sgd=SGDRegressor()
lasso=Lasso()
ridge=Ridge()
rf=RandomForestRegressor()
dt=DecisionTreeRegressor()
gboost=GradientBoostingRegressor()
bagging=BaggingRegressor()
adboost=AdaBoostRegressor()
knn=KNeighborsRegressor()
etr=ExtraTreesRegressor()
gamma=GammaRegressor()
algo=[sgd,lasso,ridge,rf,dt,gboost,bagging,adboost,knn,etr]
model=[]
accuracy=[]
for i in range(len(algo)):
    algo[i].fit(x_train,y_train)
    accuracy.append(algo[i].score(x_test,y_test))
    model.append(algo[i])
    print(model[i],accuracy[i])
etr1=ExtraTreesRegressor()
etr1.fit(x_train,y_train)
etr1.score(x_test,y_test)
cv_score=cross_val_score(etr1,x_train,y_train,cv=10)
print(cv_score.mean())# mean cross-validation score is .9467
y_pred=etr1.predict(x_test)
plt.barh(X.columns,etr1.feature_importances_)<jupyter_output><empty_output><jupyter_text>Here we cann see the feature importance in determining the price of car  R-Square<jupyter_code>r2_score(y_test,y_pred)<jupyter_output><empty_output><jupyter_text>Adjusted R-Square Score=1-(1-R^2)* (n-1)/(n-k-1)<jupyter_code>print(1-(1-r2_score(y_test,y_pred))* (len(Y)-1)/(len(Y)-X.shape[1]-1))# so we have got adjusted r2 
                                                                      #as .9467<jupyter_output>0.9459485471577108<jupyter_text>MSE,RMSE<jupyter_code>print('mean square error is:',mean_squared_error(y_pred,y_test))
print('root mean square error is :',np.sqrt(mean_squared_error(y_pred,y_test)))
x_value=range(len(y_test))
plt.figure(figsize=(15,5))
plt.plot(x_value,y_test,color='blue',label='Actual',lw=.6)
plt.plot(x_value,y_pred,color='green',label='predicted',lw=.6)
plt.legend()
plt.title('Actual Price VS Predicted Price')
plt.show()
y_test1=y_test.copy()
y_test1['predicted_price']=y_pred
y_test1.corr()# accuracy is 97.30%<jupyter_output><empty_output><jupyter_text>Linear Model<jupyter_code>X.columns
lr=LinearRegression()
X1=scaled
X1
x_train,x_test,y_train,y_test=train_test_split(X1,Y,test_size=0.3,random_state=0)
lr.fit(x_train,y_train)
lr.score(x_test,y_test)
lr.intercept_
lr.coef_
X1.columns<jupyter_output><empty_output><jupyter_text>equation<jupyter_code># Price=41070.34 -5535.06*'Popularity' +9746.33*'city mpg'-3947.73*'highway MPG'+1774.37*'Vehicle Style'
# -5453.30*'Vehicle Size'-6442.30*'Market Category-8219.3*Number of Doors-2057.56*'Driven_Wheels
#-6658.32*'Transmission Type-8812.33*'Engine Cylinders+47729.9*'Engine HP'-436.3*'Engine Fuel Type
#-4343.23*'Year'<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1245771144278607, 'fraction_numerical': 0.040696517412935326, 'mean_word_length': 4.621364653243848, 'pattern_counts': {'":': 0, '<': 81, '<?xml version=': 0, '>': 82, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 25, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '588430', 'license': 'Apache-2.0', 'n_tokens_mistral': 3912, 'n_tokens_neox': 3583, 'n_words': 647, 'path': 'car price prediction.ipynb', 'repo_name': 'NaveenKumarMaurya/my-datascience-end-to-end-project-portfolio', 'size': '10050'}