-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMulticollinearity_Sales Prediction.py
More file actions
108 lines (53 loc) · 1.67 KB
/
Copy pathMulticollinearity_Sales Prediction.py
File metadata and controls
108 lines (53 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#!/usr/bin/env python
# coding: utf-8
# In[1]:
#import required libraries
import numpy as np
import pandas as pd
from statsmodels.stats.outliers_influence import variance_inflation_factor
# In[2]:
# Reading the dataset of House Sales
df = pd.read_csv("G:\\Multicollinearity\\House Sales.csv")
# In[3]:
df.head()
# In[4]:
type(df)
# ### Calculating VIF scores for original data
# In[5]:
# Creating a function to calculate the VIF scores for all independant features with for loop
def vif_scores(df):
VIF_Scores = pd.DataFrame()
VIF_Scores["Independent Features"] = df.columns
VIF_Scores["VIF Scores"] = [variance_inflation_factor(df.values,i) for i in range(df.shape[1])]
return VIF_Scores
df1 = df.iloc[:,:-1]
vif_scores(df1)
# ### Fixing Multicollinearity - dropping variables
# In[6]:
#Copying the original dataframe
df2 = df.copy()
# In[7]:
# Dropping the features which are having high VIF values
df3 = df2.drop(['Interior(Sq Ft)','# of Rooms'], axis = 1)
# In[8]:
df3.head()
# In[9]:
#Calculating VIF scores after dropping the varaibles
def vif_scores(df3):
VIF_Scores = pd.DataFrame()
VIF_Scores["Independant Features"] = df3.columns
VIF_Scores["VIF Scores"] = [variance_inflation_factor(df3.values,i) for i in range(df3.shape[1])]
return VIF_Scores
df3 = df3.iloc[:,:-1]
vif_scores(df3)
# ### Fixing multicollinearity - Combining the variables
# In[10]:
df4= df3.copy()
# In[11]:
df4.head()
# In[12]:
#Combining the variables and calculating the VIF scores
df5 = df4.copy()
df5['Total Rooms'] = df4.apply(lambda x: x['# of Bed'] + x['# of Bath'],axis=1)
X = df5.drop(['# of Bed','# of Bath'],axis=1)
vif_scores(X)