As posted on StackOverflow: http://stackoverflow.com/questions/27128688/how-to-use-least-squares-with-weight-matrix-in-python

import numpy as np # Solve for X in AX = B A = np.array([[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,1,1],[1,1,0,0]]) B = np.array([1,1,1,1,1]) W = np.array([1,2,3,4,5]) Aw = A * np.sqrt(W[:,np.newaxis]) Bw = B * np.sqrt(W) X = np.linalg.lstsq(Aw, Bw)[0]

Advertisements