Linear regression : ValueError: operands could not be broadcast together with shapes (3,) (1338,)

I try to use linear regression for insurance data . But had error on the when try to call a function with features parameter. Here is my code:

def h(x):
    global w
    return np.sum(np.transpose(w)*x)
    raise NotImplementedError()

when try with a simple data it works fine,

w, x = [1,2,3], [2,3,4]
h(x)

the output is : 20

but when try to use the dataset, it errors:

features = dataset.drop(["charges"], axis=1).values
h(features )

it returns error:

ValueError: operands could not be broadcast together with shapes (3,) (1338,) 

so the features looks like this:

array([[0.1173913 , 0.        , 0.35698144, 0.        , 1.        ],
       [0.1       , 1.        , 0.48331988, 1.        , 0.        ],
       [0.27391304, 1.        , 0.46674738, 3.        , 0.        ],
       ...,
       [0.1       , 0.        , 0.5496099 , 0.        , 0.        ],
       [0.15217391, 0.        , 0.3117837 , 0.        , 0.        ],
       [0.84782609, 0.        , 0.38216303, 0.        , 1.        ]])

The data i used is insurance.csv from kaggle.com

Topic implementation linear-regression python machine-learning

Category Data Science


It looks like you're trying to multiply a matrix and a vector point-wise. Such an operation is not defined. I think you should use X.dot(w), where X is a feature $\bf matrix$ and w is the wights $\bf vector$. np.dot can operate with objects of different nature (like matrices and vectors). So in h I'd write return X.dot(w). Also, I'd call it only with a matrix X (for consistency), even if there's an object it would be $X \in \mathbb{R}^{1 \times d}$ in that case. Watch carefully after shapes of the objects which enter functions.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.