Triss
Triss

Reputation: 581

How can I print a Numpy Array as a result from a function?

I have this function which gets 3 inputs and does some matrix calculations.

import numpy as np

def func(input_x, output_y, lambda_param):
        
    if input_x.shape[0]<input_x.shape[1]:
        input_x = np.transpose(input_x)
    
    input_x = np.c_[np.ones(input_x.shape[0]),input_x]
    
    lambda_param = np.identity(input_x.shape[1])*lambda_param
    

    a = np.linalg.inv(lambda_param+np.matmul(np.transpose(input_x),input_x))
    b = np.matmul(np.transpose(input_x),output_y)

    weights = np.matmul(a,b)
    weights = np.array([weights])
    return weights

The function works well but I have a problem with the data type of the result. For example I have the inputs yy, xx and lamb:


yy = np.array([208500, 181500, 223500, 
                                140000, 250000, 143000, 
                                307000, 200000, 129900, 
                                118000])
                                
xx = np.array([[1710, 1262, 1786, 
                                1717, 2198, 1362, 
                                1694, 2090, 1774, 
                                1077], 
                               [2003, 1976, 2001, 
                                1915, 2000, 1993, 
                                2004, 1973, 1931, 
                                1939]])
lamb = 10

result = func(xx, yy, lamb)

print(result) #--> np.array([-576.67947107,   77.45913349,   31.50189177])
#print(result[2]) #--> 31.50189177

print(result) gives me [[-576.67947107 77.45913349 31.50189177]] but should return a numpy.array like np.array([-576.67947107, 77.45913349, 31.50189177])

and print(result[2]) should return 31.50189177 but gives an error because its not a np.array?

I hope you can help me! Thanks in advance.

Upvotes: 0

Views: 749

Answers (2)

jhihan
jhihan

Reputation: 559

You don't need this line: weights = np.array([weights]) weights is already a 1D numpy array after weights = np.matmul(a,b). The redundant line will give an extra dimension to weights and the shape of weights becomes (1,3). You can check these by using print(weights.shape)

Upvotes: 2

Jack Roberts
Jack Roberts

Reputation: 101

Removing the second to last line of func (weights = np.array([weights])) should fix it for you. You're unintentionally creating a two-dimensional array with that line. For example:

x = np.array([0.25, 0.50, 0.75])
print(x.shape)  #--> (3,) 
print(x[2])  #--> 0.75

y = np.array([x])
print(y.shape) #--> (1, 3)
print(y[2])  #--> IndexError

y above is a 2D array with length 1 in the first index and length 3 in the second index, so y[2] (which applies to the first index) doesn't exist.

You could do np.array(weights) instead (without putting weights in a list), but that's obsolete as weights is already a numpy array.

In terms of what's printed - you can check that weights really is a numpy array by doing print(type(weights)). It is a numpy array even if this isn't clear from print(weights).

Upvotes: 1

Related Questions