-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUltraLinReg.py
More file actions
45 lines (41 loc) · 1.17 KB
/
UltraLinReg.py
File metadata and controls
45 lines (41 loc) · 1.17 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Feb 19 19:53:14 2024
@author: jeffreyhu
"""
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
fileName = "Data/" + input("What is the file name? ")
data = pd.read_csv(fileName)
xcols = []
yName = input("What is the name of your dependent variable column ")
for i in range(data.shape[1]):
if not (data.columns[i] == yName):
xcols.append(data.columns[i])
xValues = data[xcols].values
yValues = data[yName].values
xMatrix = np.concatenate([np.ones((xValues.shape[0],1)), xValues], axis = 1)
#print(xMatrix)
xTranspose = xMatrix.T
xDot = xTranspose @ xMatrix
inverseX = np.linalg.inv(xDot)
xTY= xTranspose @ yValues
final = xTY @ inverseX
for halp in range(len(xcols)):
print(xcols[halp] + ": " + str(final[halp]))
MSE = 0
for i in range(len(yValues)):
temp = 0
for j in range(len(xcols)):
temp += final[j] * data[xcols[j]].values[i]
MSE += ((yValues[i]-temp)**2)/len(yValues)
print(str(MSE))
print(final[0])
#var1 = final[0]
#var2 = final[1]
#var3 = final[2]
#print(f"{data.columns[1]}: {var1}")
#print(f"{data.columns[2]}: {var2}")
#print(f"{data.columns[3]}: {var3}")