<jupyter_start><jupyter_text>Q  To prepare a prediction model for predicting Price of the car with just the following column features  Import libraries<jupyter_code>import pandas as pd
import numpy as np

import matplotlib.pyplot as plt
import seaborn as sns

from statsmodels.graphics.regressionplots import influence_plot
import statsmodels.formula.api as smf
import statsmodels.api as sm
from ml_metrics import rmse
import warnings
warnings.filterwarnings('ignore')<jupyter_output><empty_output><jupyter_text>Importing datset<jupyter_code>toyota = pd.read_csv('ToyotaCorolla.csv', encoding= 'unicode_escape')
toyota.head()<jupyter_output><empty_output><jupyter_text>Checking columns data<jupyter_code>toyota.columns
toyota1 = toyota[['Price','Age_08_04','KM','HP','cc','Doors','Gears','Quarterly_Tax','Weight']]
toyota1.head()<jupyter_output><empty_output><jupyter_text>Renaming the columns<jupyter_code>toyota2 = toyota1.rename({'Age_08_04':'Age','Quarterly_Tax':'QTax'},axis=1)
toyota2.head()<jupyter_output><empty_output><jupyter_text>Checking for null values<jupyter_code>toyota2.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 1436 entries, 0 to 1435
Data columns (total 9 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   Price   1436 non-null   int64
 1   Age     1436 non-null   int64
 2   KM      1436 non-null   int64
 3   HP      1436 non-null   int64
 4   cc      1436 non-null   int64
 5   Doors   1436 non-null   int64
 6   Gears   1436 non-null   int64
 7   QTax    1436 non-null   int64
 8   Weight  1436 non-null   int64
dtypes: int64(9)
memory usage: 101.1 KB<jupyter_text>Check for dupliacte values and drop if any<jupyter_code>toyota2[toyota2.duplicated(keep= False)]
toyota3 = toyota2.drop_duplicates()
toyota3.shape<jupyter_output><empty_output><jupyter_text>Correlation matrix<jupyter_code>toyota3.corr()<jupyter_output><empty_output><jupyter_text>From the above correlation matrix, it is clear that the independent variables do not have much colinearity  in the data  Model 1 Linear model<jupyter_code>model1 = smf.ols('Price~Age+KM+HP+cc+Doors+Gears+QTax+Weight', data = toyota3).fit()<jupyter_output><empty_output><jupyter_text>coeficients<jupyter_code>model1.params
model1.summary()
pred1 = model1.predict(toyota3.iloc[:,1:])
r1=rmse(pred1,toyota3.Price)
r1<jupyter_output><empty_output><jupyter_text>Residual analysis Test for normality of residuals (Q-Q Plot)<jupyter_code>qqplot=sm.qqplot(model1.resid,line='q')
plt.title("Normal Q-Q plot of residuals")
plt.show()
list(np.where(model1.resid<-4000))
list(np.where(model1.resid>4000))<jupyter_output><empty_output><jupyter_text>Residual plot for homoscedasticity<jupyter_code>def get_standardized_values( vals ):
    return (vals - vals.mean())/vals.std()
plt.scatter(get_standardized_values(model1.fittedvalues),
            get_standardized_values(model1.resid))

plt.title('Residual Plot')
plt.xlabel('Standardized Fitted values')
plt.ylabel('Standardized residual values')
plt.show()
toyota4= toyota3.drop(toyota3.index[[109,110,111,124,146,170,190,191,220,392,401,522,600,653,959,1057]],axis=0).reset_index()
toyota4= toyota4.drop(['index'],axis=1)
toyota4.shape<jupyter_output><empty_output><jupyter_text>Model 2<jupyter_code>model2 = smf.ols('Price~Age+KM+HP+cc+Doors+Gears+QTax+Weight', data = toyota4).fit()
model2.summary()
pred2 = model2.predict(toyota4.iloc[:,1:])
r2=rmse(pred2,toyota4.Price)
r2<jupyter_output><empty_output><jupyter_text>Detecting influencers/outliers Cook’s Distance<jupyter_code>model2_influence = model2.get_influence()
(c, _) = model2_influence.cooks_distance
fig = plt.subplots(figsize=(20, 7))
plt.stem(np.arange(len(toyota4)), np.round(c, 3))
plt.xlabel('Row index')
plt.ylabel('Cooks Distance')
plt.show()
#index and value of outliers
(np.argmax(c),np.max(c))<jupyter_output><empty_output><jupyter_text>Checking for an influence plot for detection of an outlier in the dataset Highly influencing point<jupyter_code>from statsmodels.graphics.regressionplots import influence_plot
influence_plot(model2)
plt.show()
k = toyota4.shape[1]
n = toyota4.shape[0]
leverage_cutoff = 3*((k + 1)/n)
leverage_cutoff<jupyter_output><empty_output><jupyter_text>Removing the data at index 80 from the dataset<jupyter_code>toyota5= toyota4.drop(toyota4.index[[80]],axis=0).reset_index()
toyota5= toyota5.drop(['index'],axis=1)<jupyter_output><empty_output><jupyter_text>Model 3<jupyter_code>model3 = smf.ols('Price~Age+KM+HP+cc+Doors+Gears+QTax+Weight', data = toyota5).fit()
model3.summary()
pred3 = model3.predict(toyota5.iloc[:,1:])
r3=rmse(pred3,toyota5.Price)
r3<jupyter_output><empty_output><jupyter_text>Cook's Distance<jupyter_code>model3_influence = model3.get_influence()
(c, _) = model3_influence.cooks_distance<jupyter_output><empty_output><jupyter_text>Plot the influencers values using stem plot<jupyter_code>fig = plt.subplots(figsize=(20, 7))
plt.stem(np.arange(len(toyota5)), np.round(c, 3))
plt.xlabel('Row index')
plt.ylabel('Cooks Distance')
plt.show()
#index and value of outliers
(np.argmax(c),np.max(c))<jupyter_output><empty_output><jupyter_text>From the above plot, it is evident that data point at index 974 is an outlier  We will remove the data at index 974 from the dataset<jupyter_code>toyota6= toyota5.drop(toyota4.index[[974]],axis=0).reset_index()
toyota6= toyota6.drop(['index'],axis=1)<jupyter_output><empty_output><jupyter_text>Model 4 Improve model using new data toyota6, where teh outliers/ highly influencing points are removed<jupyter_code>model4 = smf.ols('Price~Age+KM+HP+cc+Doors+Gears+QTax+Weight', data = toyota6).fit()
model4.summary()<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>pred4 = model4.predict(toyota6.iloc[:,1:])
pred4
actual = toyota6.Price
from ml_metrics import rmse
r4=rmse(pred4,actual)
r4<jupyter_output><empty_output><jupyter_text>Conclusion<jupyter_code>data=pd.DataFrame({"Model":pd.Series(['Model1','Model2','Model3','Model4']),
                   "rsquared":(model1.rsquared,model2.rsquared,model3.rsquared,model4.rsquared),
                   "AIC":(model1.aic,model2.aic,model3.aic,model4.aic),
                   "rmse":(r1,r2,r3,r4)
                  })
data<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13480627601665066, 'fraction_numerical': 0.04610951008645533, 'mean_word_length': 7.134114583333333, 'pattern_counts': {'":': 4, '<': 90, '<?xml version=': 0, '>': 90, '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': '22', 'dir': 'jupyter-structured-clean-dedup', 'id': '648417', 'license': 'Apache-2.0', 'n_tokens_mistral': 2587, 'n_tokens_neox': 2389, 'n_words': 389, 'path': 'Multiple Linear Regression/Multi Linear Regression (Toyota Corolla).ipynb', 'repo_name': 'santaaaaaa/Data-science---Illustrative-cases', 'size': '6246'}