-
Notifications
You must be signed in to change notification settings - Fork 2
/
Automated_VIF_Spark.py
66 lines (61 loc) · 2.79 KB
/
Automated_VIF_Spark.py
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
# ------------------------------------------------------------------------------
# Importing required libraries
# ------------------------------------------------------------------------------
from pyspark.sql.types import Row
# Importing required libraries for VIF Calculation
from pyspark.ml.regression import LinearRegression
from pyspark.ml.linalg import DenseVector
from pyspark.ml.linalg import Vectors
from pyspark.ml.evaluation import RegressionEvaluator
# ------------------------------------------------------------------------------
# Creating an Pyspark dataframe from a hive table
# ------------------------------------------------------------------------------
basedata = spark.table("my_database.my_table")
# ------------------------------------------------------------------------------
# Calculating VIF
# Assigning the threshold for VIF in the first line
# This may be changed to any other value as per requirement
# ------------------------------------------------------------------------------
vif_threshold = 5 #Threshold for VIF
def vif_cal_iter(inputdata,vif_threshold):
xvar_names = inputdata.columns
global vif_max
global colnum_max
colnum_max = 10000 # Initialising with a fake value
vif_max = vif_threshold + 1
def vif_cal(inputdata, xvar_names, vif_max, colnum_max, vif_threshold):
print("Dimension of table at this level")
print("================================")
print(inputdata.count(), len(inputdata.columns))
print("List of X Variables")
print("===================")
print(xvar_names)
vif_max = vif_threshold
for i in range(2,len(xvar_names)):
train_t = inputdata.rdd.map(lambda x: [Vectors.dense(x[2:i]+x[i+1:]), x[i]]).toDF(['features', 'label'])
lr = LinearRegression(featuresCol = 'features', labelCol = 'label')
lr_model = lr.fit(train_t)
predictions = lr_model.transform(train_t)
evaluator = RegressionEvaluator(predictionCol='prediction', labelCol='label')
r_sq=evaluator.evaluate(predictions, {evaluator.metricName: "r2"})
vif=1/(1-r_sq)
if vif_max < vif:
vif_max = vif
colnum_max = i
return vif_max, colnum_max
while vif_max > 5:
vif_max, colnum_max = vif_cal(inputdata, xvar_names, vif_max, colnum_max, vif_threshold)
if vif_max > vif_threshold:
print("Start of If Block")
inputdata = inputdata.drop(inputdata[colnum_max])
xvar_names = inputdata.columns
print("Dimension of table after this iteration")
print("=======================================")
print(inputdata.count(), len(inputdata.columns))
print("List of X Variables remaining")
print("=============================")
print(xvar_names)
else:
return inputdata
train = vif_cal_iter(basedata,vif_threshold)
print(train.count(), len(train.columns))