Repository navigation
Expand file tree
/
Copy pathtest.py
More file actions
86 lines (69 loc) · 2.16 KB
/
Copy pathtest.py
File metadata and controls
86 lines (69 loc) · 2.16 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
"""
ML Regression model predicting 3rd Exam grade based on 33 different variables
in the student-mat.csv. Data was pulled from the UCI Machine Learning Repository.
This was my first attempt at training a model using Python.
Author: Andrew Quartuccio
Credit to ML Tutorial on techwithtim.net
"""
"""
IMPORTS
"""
import pandas as pd
import numpy as np
import sklearn
import matplotlib.pyplot as pyplot
import pickle
from matplotlib import style
from sklearn import linear_model
from sklearn.utils import shuffle
style.use('ggplot')
# READ IN DATA (had semicolon as delimiter for some reason)
data = pd.read_csv('student-mat.csv', sep=";")
# 'Label'
# term used in ML for desired result
predict = "G3"
# Create array of weights - shuffle data
data = data[['G1', 'G2', 'studytime', 'failures', 'absences', 'G3']]
data = shuffle(data)
# Assign Data into separate arrays
x = np.array(data.drop([predict], 1))
y = np.array(data[predict])
x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x, y, test_size=0.1)
"""
MODEL TRAINING
"""
# Best Model Training and Saving
# **Turn off to keep model
# **Train model multiple times for best score
best = 0
for _ in range(1000):
x_train, x_test, y_train, y_test = sklearn.model_selection.train_test_split(x, y, test_size=0.1)
linear = linear_model.LinearRegression()
linear.fit(x_train, y_train)
accuracy = linear.score(x_test, y_test)
print("Accuracy: " + str(accuracy))
if accuracy > best:
best = accuracy
with open("studentmodel.pickle", "wb") as f:
pickle.dump(linear, f)
# Load Model
pickle_in = open("studentmodel.pickle", "rb")
linear = pickle.load(pickle_in)
print("----------------------")
print('Coefficients: \n', linear.coef_)
print('Intercept: \n', linear.intercept_)
print("----------------------")
predicted = linear.predict(x_test)
# Print Each Guess, Data & Score
for x in range(len(predicted)):
print(predicted[x], x_test[x], y_test[x])
"""
PLOTTING
"""
# Choose your x-axis
p = 'failures'
pyplot.scatter(data[p], data['G3'])
pyplot.legend(loc=4)
pyplot.xlabel(p)
pyplot.ylabel("Final Grade")
pyplot.show()