0

Code:

testdata  = np.array([[20.5, 765], [32.7, 826], [51.0, 873], [73.2,942], [95.7,1032]])

def LinearLeastSquares(data):
    x=np.array(data[:,:1])
    y=np.array(data[:,1:2])
    n=len(data)

    A=np.sum(np.array([[x**2 , x ], [x , n]]))
    B=np.sum(np.array([x*y , y]))
    print(A,B) #To check outputs
    return

The result I'm looking for is a 2x2 array and a 2x1 array, where each position is represented by summing through each value of testdata array (depending on what is required) and outputting numbers. My primary issue is that I am expecting a 2x2 array and a 2x1 array to be returned, but instead I am getting the following for A and B respectively when I run LinearLeastSquares(testdata):

[[ 466.25]
 [1139.69]
 [2708.  ]
 [5509.64]
 [9354.89]] 259370.5

On top of the output not having the correct dimensions, the calculations aren't correct either although there are some numbers that are relatively close, but not all of them. Any help is appreciated.

1 Answer 1

1

The problem is that you are concatenating the arrays and scalars with np.array and applying the sum for a given axis of it. If I understand your ambitions correctly, I recommend you to perform the sum operations for each array, and later join the results into a numpy array:

A = np.array([np.sum(arr) for arr in [x**2, x, x, n]]).reshape((2,2))
B = np.array([np.sum(arr) for arr in [x*y , y]]).reshape((2,1))

The reshapes are necessary to transform A and B into a 2x2 and 2x1 arrays.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.