Lin K - 9 months ago 29

Python Question

Ok, so I'm writing a function for linear least squares in python and it's pretty much just one equation. Yet for some reason, I'm getting a ValueError. My best guess is it has something to do with the

`.reshape`

`x_train`

`y_train`

`reshape`

Code (np. stands for numpy):

`def least_squares(x_train, y_train, M):`

'''

:param x_train: training input vector Nx1

:param y_train: training output vector Nx1

:param M: polynomial degree

:return: tuple (w,err), where w are model parameters and err mean squared error of fitted polynomial

'''

w = np.linalg.inv(design_matrix(x_train, M). * design_matrix(x_train, M)) * design_matrix(x_train, M).T * y_train

err = mean_squared_error(x_train, y_train, w)

return (w, err)

`design_matrix`

`mean_squared_error`

Traceback:

`ERROR: test_least_squares_err (test.TestLeastSquares)`

----------------------------------------------------------------------

Traceback (most recent call last):

File "\content.py", line 48, in least_squares

w = np.linalg.inv(design_matrix(x_train, M).T * design_matrix(x_train, M)) * design_matrix(x_train, M).T * y_train

ValueError: operands could not be broadcast together with shapes (7,20) (20,7)

Answer Source

Assuming that `design_matrix`

returns a matrix, this code

```
design_matrix(x_train, M).T * design_matrix(x_train, M)
```

most likely does not do what is intended since `*`

is performing element-wise multiplication (Hadamard product of two matrices). Because your matrices are not square, it thus complains about incompatible shape.

To obtain matrix-matrix product, one might do (assuming numpy was imported as `import numpy as np`

):

```
np.dot(design_matrix(x_train, M).T, design_matrix(x_train, M))
```

Similar reasoning then applies to the rest of the statement `* design_matrix(x_train, M).T * y_train`

...

Also, you might want to evaluate `design_matrix`

only once, e.g., to put something like

```
mat = design_matrix(x_train, M)
```

before the line calculating `w`

and then use merely `mat`

.