Assignment 1: for Q10 more guidance required weights =?

For the weights =???. i was thiinking this would be weights = val_inputs
not sure if this is correct or should i be using another dataframe here. Any suggestions or guidance her would be appreciated.

1 Like

As given in Question the weights are also known as coefficients…
And in the Hint a document is Given…
Just go through it t find something related to coefficients
Most probably you will find it…

The weight are the coefficients. You can simply use the scikit learn library for your Ridge model e.g
weight = model.coef_

Talking about weights a big negative coef. means that there are a big correlation?

When the values are tending toward zero the correlation is less. When the values are away from zero the correlation is more.

I got the values for weights by running
weights = model.coef_
But the given piece of code is not working
weights_df = pd.DataFrame({
‘columns’: train_inputs.columns,
‘weight’:weights}).sort_values(‘weight’, ascending = False)

It gives the error ’ Data must be one dimensional’
weights.shape gives [1, 304]
trian_inputs.column.shape= [304, ]
I tried taking transpose of the weights but it still isn’t working.

Hey, the error is already mentioned in the error message, if you see the weights have a shape of [1, 304] ie. the weights look like this [[a1, a2, a3, a4, .... a304]] but it should be single dimensional array not a 2d array. You have to convert the weights somehow. I think weights.toarray() might work or unsqueeze()

I tried this and it worked
weights = model.coef_
a = weights.reshape(-1)

weights_df = pd.DataFrame({
‘columns’: train_inputs.columns,
‘weight’:a}).sort_values(‘weight’, ascending = False)

Thank you for your help…!


Anyone else have this issue:
ValueError: Data must be 1-dimensional

with weights = model.coef_

You probably have more than one target column, or you have used a list of target columns, instead of a single string value.