-
Notifications
You must be signed in to change notification settings - Fork 4
/
Cross_Validation_Model.py
49 lines (31 loc) · 1.27 KB
/
Cross_Validation_Model.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
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import K_Fold_Cross
class model:
def __init__(self):
pass
def algo(self, train_data, test_data):
# Instantiate object of class 'k_fold'
k_cross = K_Fold_Cross.k_fold()
plot, correct_K, correct_error = k_cross.cross(train_data)
print("Estimated Error Rate = ",correct_error," with K= ",correct_K)
plt.show(plot)
#Separating the predictors and labels for train and test data
train_x = np.array(train_data.iloc[:,:-1])
train_y = train_data.iloc[:,-1]
test_x = np.array(test_data.iloc[:,:-1])
test_y = test_data.iloc[:,-1]
#Predicting actual error rate for Test Set
from sklearn.linear_model import LinearRegression
clf = LinearRegression()
clf.fit(train_x,train_y)
pred_y = clf.predict(test_x)
actual_error_rate = np.square(np.subtract(test_y,pred_y)).mean()
print("Actual Error Rate = ",actual_error_rate)
data = pd.read_csv('student_scores.csv')
cross_validation = model()
#Dividing the train and test data
train = data.iloc[:20,:]
test = data.iloc[20:,:]
cross_validation.algo(train, test)