ValueError in sklearn: shapes not aligned

Question:

I am attempting to return the test scores I ran through my program, but no matter what I try, I keep getting the error:

ValueError                                Traceback (most recent call last)
<ipython-input-19-4b49cb58c813> in <module>()
----> 1 print(model_final.score(X_train, y_train))
      2 print(model_final.score(X_test, y_test))

3 frames
/usr/local/lib/python3.6/dist-packages/sklearn/utils/extmath.py in safe_sparse_dot(a, b, dense_output)
    140         return ret
    141     else:
--> 142         return np.dot(a, b)
    143 
    144 

ValueError: shapes (353,10) and (172,) not aligned: 10 (dim 1) != 172 (dim 0)

The full code can be found here.

Asked By: Vendetta

||

Answers:

The issue is in the line

model_final = model

This statement will not create a replica of model, it just does a shallow copy so when you change your model even the model_final will get changed. To avoid this you need to do a deepcopy as

from copy import deepcopy
model_final = deepcopy(model)

this will do a deep copy and whatever changes you make to model will not affect model_final.

Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.