I have the X array shape (40*100) Y array contains 40 elements. IS it possible to do OLS, WLS?? how to do that? after the scatter plot. enter image description here

How to apply the least square to find the relationship between X and Y. for example I would like to generate the equation of X and Y .

Here I give the simple example.
   X=[[0.0,0.03,0.04,0.0,0.1,0.1,0.7,0.5,0.3,0.6],
      [0.0,0.0,0.4,0.5,0.1,0.1,0.03,0.04,0.0,0.1],
      [0.6,0.7,0.0,0.8,0.1,0.1,0.1,0.1,0.7,0.5],
      [0.3,0.6,0.1,0.5,0.6,0.1,0.4,0.5,0.1,0.1]]

    Y=[1,4,2,5]
1

There are 1 answers

0
ilanman On

Whether or not OLS or WLS is appropriate is one question (e.g. linear dependence among features is requires a different approach or if your response (Y variable) is discrete then you wouldn't use OLS, but instead use logistic regression or something else), but performing it in Python using your data is as follows:

import numpy as np
import numpy.linalg as la
X = np.array([[0.0,0.03,0.04,0.0,0.1,0.1,0.7,0.5,0.3,0.6],
            [0.0,0.0,0.4,0.5,0.1,0.1,0.03,0.04,0.0,0.1],
            [0.6,0.7,0.0,0.8,0.1,0.1,0.1,0.1,0.7,0.5],
            [0.3,0.6,0.1,0.5,0.6,0.1,0.4,0.5,0.1,0.1]])

Y = np.array([1,4,2,5])
OLS = la.lstsq(X,Y)[0]

print OLS
[-0.60940892  0.19707325  3.94166269  4.06073677  2.76949291  
 0.90614714 0.92161768  1.5417828  -1.87887552 -0.63917305]

Note that this yields a perfect solution:

np.allclose(X.dot(OLS),Y)
True