Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions least_squares_regression.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include <bits/stdc++.h>

using namespace std;


void regression(vector<float> x, vector<float> y)
{
int n = x.size();
float mx = accumulate(x.begin(), x.end(), 0)/n;
float my = accumulate(y.begin(), y.end(), 0)/n;

float sx = accumulate(x.begin(), x.end(), 0);
float sy = accumulate(y.begin(), y.end(), 0);
float sx_sy = 0;
float sx_x = 0;
for(int i =0; i<n; i++)
{
sx_sy += x[i]*y[i];
sx_x += x[i]*x[i];
}
float b = (n*sx_sy-sx*sy)/(n*sx_x-sx*sx);

float a = my - b*mx;

printf("y= %.3f + %.3f*x", a, b);
}

int main(void)
{
vector<float> x = { 5, 7, 12, 16, 20 };
vector<float> y = { 40, 120, 180, 210, 240 };
regression(x,y);
return 0;
}
39 changes: 39 additions & 0 deletions polynomial_regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import numpy as np
import matplotlib.pyplot as plt

def calculate_matrix(x, n):
m = len(x)
matrix = np.zeros((n+1, n+1))
for i in range(n+1):
for j in range(n+1):
matrix[i][j] = np.sum(x**(i+j))
return matrix

def calculate_vector(x, y, n):
m = len(x)
vector = np.zeros(n+1)
for i in range(n+1):
vector[i] = np.sum(y * x**i)
return vector

def calculate_b(x, y, n):
k = calculate_matrix(x, n)
l = calculate_vector(x, y, n)
return np.linalg.solve(k, l)

if __name__ == "__main__":
X = np.array([1, 2, 3, 4, 5, 6, 7])
Y = np.array([45000, 50000, 60000, 80000, 110000, 150000, 200000])

n = 3 # Adjust the degree of polynomial as needed
b_coeffs = calculate_b(X, Y, n)
x_vals = np.linspace(1, 7, 100)
y_vals = sum(b_coeffs[i] * x_vals**i for i in range(n+1))
plt.scatter(X, Y, color='blue')
plt.plot(x_vals, y_vals, label='Polynomial Regression')
plt.xlabel('x')
plt.ylabel('y')
plt.title('Plot of Polynomial Regression')
plt.legend()
plt.grid(True)
plt.show()