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.
weights = np.array(weights)instead ofweights = np.array([weights])?[-576.67947107 77.45913349 31.50189177] 31.501891773638363but notnp.array([...])print(arr)shows thestrformat of the array, which omits the workarrayand commas. Thereprgives more details.