-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP7-Grade-Predictor.cpp
More file actions
142 lines (121 loc) · 4.66 KB
/
P7-Grade-Predictor.cpp
File metadata and controls
142 lines (121 loc) · 4.66 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include <vector>
#include <cmath>
#include <fstream>
#include <cstdlib>
using namespace std;
struct DataPoint {
double studyHours;
double attendance;
double assignments;
double grade;
};
vector<DataPoint> dataset = {
{2, 80, 70, 50}, {3, 85, 75, 55}, {4, 90, 80, 60},
{5, 95, 85, 65}, {6, 98, 90, 70}, {7, 100, 95, 75},
{8, 100, 98, 80}, {9, 100, 100, 85}, {10, 100, 100, 90},
{11, 100, 100, 95}
};
double theta0 = 0, theta1 = 0, theta2 = 0, theta3 = 0;
double learningRate = 0.0001;
int iterations = 5000;
double minHours = 2, maxHours = 11;
double minAttendance = 80, maxAttendance = 100;
double minAssignments = 70, maxAssignments = 100;
double minGrade = 50, maxGrade = 95;
double normalize(double value, double minVal, double maxVal) {
return (value - minVal) / (maxVal - minVal);
}
double denormalize(double value, double minVal, double maxVal) {
return value * (maxVal - minVal) + minVal;
}
double predict(double hours, double attendance, double assignments) {
double normHours = normalize(hours, minHours, maxHours);
double normAttendance = normalize(attendance, minAttendance, maxAttendance);
double normAssignments = normalize(assignments, minAssignments, maxAssignments);
double normResult = theta0 + theta1 * normHours + theta2 * normAttendance + theta3 * normAssignments;
return denormalize(normResult, minGrade, maxGrade);
}
double computeCost() {
double cost = 0;
int m = dataset.size();
for (const auto& data : dataset) {
double normHours = normalize(data.studyHours, minHours, maxHours);
double normAttendance = normalize(data.attendance, minAttendance, maxAttendance);
double normAssignments = normalize(data.assignments, minAssignments, maxAssignments);
double normGrade = normalize(data.grade, minGrade, maxGrade);
double prediction = theta0 + theta1 * normHours + theta2 * normAttendance + theta3 * normAssignments;
cost += pow((prediction - normGrade), 2);
}
return cost / (2 * m);
}
void trainModel() {
int m = dataset.size();
for (int iter = 0; iter < iterations; iter++) {
double sumError0 = 0, sumError1 = 0, sumError2 = 0, sumError3 = 0;
for (const auto& data : dataset) {
double normHours = normalize(data.studyHours, minHours, maxHours);
double normAttendance = normalize(data.attendance, minAttendance, maxAttendance);
double normAssignments = normalize(data.assignments, minAssignments, maxAssignments);
double normGrade = normalize(data.grade, minGrade, maxGrade);
double prediction = theta0 + theta1 * normHours + theta2 * normAttendance + theta3 * normAssignments;
sumError0 += (prediction - normGrade);
sumError1 += (prediction - normGrade) * normHours;
sumError2 += (prediction - normGrade) * normAttendance;
sumError3 += (prediction - normGrade) * normAssignments;
}
theta0 -= learningRate * (sumError0 / m);
theta1 -= learningRate * (sumError1 / m);
theta2 -= learningRate * (sumError2 / m);
theta3 -= learningRate * (sumError3 / m);
if (iter % 1000 == 0) {
cout << "Iteration " << iter << " | Cost: " << computeCost() << endl;
}
}
cout << "Training Complete! Final Cost: " << computeCost() << endl;
}
void testModel() {
double hours, attendance, assignments;
cout << "Enter study hours: ";
cin >> hours;
cout << "Enter attendance percentage: ";
cin >> attendance;
cout << "Enter assignment completion percentage: ";
cin >> assignments;
double predictedGrade = predict(hours, attendance, assignments);
cout << "Predicted Grade: " << predictedGrade << endl;
}
void saveModel() {
ofstream file("model.txt");
if (!file) return;
file << theta0 << " " << theta1 << " " << theta2 << " " << theta3;
file.close();
}
void loadModel() {
ifstream file("model.txt");
if (!file) {
cout << "No saved model found. Training a new one...\n";
trainModel();
saveModel();
return;
}
file >> theta0 >> theta1 >> theta2 >> theta3;
file.close();
cout << "Model loaded successfully!\n";
}
int main() {
loadModel();
int choice;
while (true) {
cout << "\n1. Predict Grade\n2. Train Model Again\n3. Save Model\n4. Exit\n";
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1: testModel(); break;
case 2: trainModel(); saveModel(); break;
case 3: saveModel(); break;
case 4: return 0;
default: cout << "Invalid choice! Try again.\n";
}
}
}