মেশিন লারনিং ঃ Implement: Multivariate Regression: Python

মেশিন লারনিং ঃ Implement: Multivariate Regression: Python

শত ভাগ সঠিক নাউ হতে পারে।

Theory reference: https://www.cmpe.boun.edu.tr/~ethem/i2ml/slides/v1-1/i2ml-chap5-v1-1.pdf . This is an approximate solution to start with. Understand the theory and then adjust/fix/improve

import numpy as np

import random

print(‘Iterations: rows: Please enter the number of samples for each variable/dimension’)

n_number_of_samples_rows = int(input())

print(‘Columns: Dimensions: Please enter  number of variables’)

m_number_of_variables_cols = int(input())

# initialize the input data variables

print(m_number_of_variables_cols, n_number_of_samples_rows)

X = np.zeros((n_number_of_samples_rows, m_number_of_variables_cols))  

#Y_actually_R = np.zeros((n_number_of_samples_rows, m_number_of_variables_cols))

Y_actually_R = np.zeros((n_number_of_samples_rows * 1)) #m_number_of_variables_cols

#generate random data

for n in range (n_number_of_samples_rows):

   for m in range(m_number_of_variables_cols):

       X[n,m] = random.random()

   Y_actually_R[n] = random.random()

print(“X”)        

print(X)

print(“Y in row format”)

print(Y_actually_R)

print(“Y in column/vector format”)

Y_actually_R = np.transpose(Y_actually_R) #transpose

print(Y_actually_R)

#convert to matrix

X = np.matrix(X)

Y_actually_R = np.matrix(Y_actually_R)

print(‘————-matrix-print—X and then Y’)

print(X)

print(‘Y matrix’)

print(Y_actually_R)

#THE EQUATION: steps to calculate W matrix:  w = (((X.Transpose) * X).invert) * X.Transpose * r

#transpose X

X_transpose = np.transpose(X)

print(‘X_transpose’)

print(X_transpose)

#(X.Transpose) * X)   of [w = (((X.Transpose) * X).invert) * X.Transpose * r]

w_parameters =  np.dot(X_transpose, X) #does np.multiply work? probably need shapoing/reshaping

print(‘first dot’)

print(w_parameters)

#(X.Transpose) * X).invert

w_parameters = np.linalg.inv(w_parameters)

print(‘inverted’)

print(w_parameters)

#(((X.Transpose) * X).invert) * X.Transpose

w_parameters = np.dot(w_parameters, X_transpose)

print(‘2nd dot’)

print(w_parameters)

#(((X.Transpose) * X).invert) * X.Transpose * r

#Y_actually_R = np.transpose(Y_actually_R)

w_parameters = np.dot(w_parameters, np.transpose(Y_actually_R)) #np.dot( np.transpose(w_parameters), np.transpose(Y_actually_R))

#two times transpose – redundant. actually, we could avoid both transpose of Y upto this point  

print(‘w_matrix’)

print(w_parameters)

w_matrix = w_parameters  

#sum of ( rt – w0 – w1x1 – w2x2 ….. wd * xd )   rt = Y_Actually_R[t 1….N][variable_1….d]

#d eqv to m_number_of_variables_cols — i used m for that

#E(w 0 ,w 1 ,…,w d |X )

#Should it be a matrix or just one total sum? I assume one total sum

error_matrix = np.zeros(m_number_of_variables_cols)

error_sum = 0

#calculate error

Y_actually_R = np.transpose(Y_actually_R) #np.array(Y_actually_R)

for n in range(n_number_of_samples_rows):

   sum = 0

   for m in range(m_number_of_variables_cols):

       sum = Y_actually_R[m]

       #2nd part ie  w1x1 w2x2 wdxd of the equation: sum of ( rt – w0 – w1x1 – w2x2 ….. wd * xd )

       wpart = w_matrix[0]        

       for ii in range(1,m_number_of_variables_cols): # d = m_number_of_variables_cols, sum of w1x1, w2x2 to wdxd  

           wpart += w_matrix[ii] * X[n,m] #+ w_matrix[2][m] * X[n][m]

       #sum = sum – wpart

       sum = pow( (sum – wpart), 2)

   error_matrix[m] = 0.5 * pow (  (sum – wpart), 2)

   error_sum += sum #error_matrix[m] #pow (  (sum – wpart), 2)

error_sum = 0.5 * error_sum      

print(‘error matrix if supposed to be = number of variables’)  

print(error_matrix)        

print(‘Error if supposed to be one number i.e. sum of all errors’)

print(error_sum)