相关文章推荐
腼腆的柠檬  ·  python ...·  2 周前    · 
有情有义的大白菜  ·  python ...·  2 周前    · 
完美的馒头  ·  python QTreeWidget ...·  2 周前    · 
失眠的烤红薯  ·  python qt textBrowser ...·  1 周前    · 
成熟的圣诞树  ·  getopts ...·  1 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
def classify(self, texts):
        vectors = self.dictionary.feature_vectors(texts)
        predictions = self.svm.decision_function(vectors)
        predictions = np.transpose(predictions)[0]
        predictions = predictions / 2 + 0.5
        predictions[predictions > 1] = 1
        predictions[predictions < 0] = 0
        return predictions

The error:

TypeError: 'numpy.float64' object does not support item assignment

occurs on the following line:

        predictions[predictions > 1] = 1

Does anyone has an idea of solving this problem? Thanks!

On which line does the error occur? You should always post your "traceback" from Python. – John Zwinck Jan 13, 2015 at 5:17 You made predictions a scalar when you assigned predictions = np.transpose(predictions)[0]. Therefore, you can't index it any more as you tried to do 2 and 3 lines further down. What are you trying to accomplish?! – Alex Martelli Jan 13, 2015 at 5:17 sorry,this line"predictions[predictions > 1] = 1": TypeError: 'numpy.float64' object does not support item assignment – chen Jan 13, 2015 at 5:33 FWIW, I was getting and error like this with numpy 0.17 and csaps 0.11. Updating to numpy 0.19 and csaps 1.0 fixed the issue. So an upgrade may help with this does not support item assignment error. – naught101 Mar 23, 2021 at 4:49

Try this testing code and pay attention to np.array([1,2,3], dtype=np.float64). It seems self.svm.decision_function(vectors) returns 1d array instead of 2d. If you replace [1,2,3] to [[1,2,3], [4,5,6]] everything will be ok.

import numpy as np
predictions = np.array([1,2,3], dtype=np.float64)
predictions = np.transpose(predictions)[0]
predictions = predictions / 2 + 0.5
predictions[predictions > 1] = 1
predictions[predictions < 0] = 0

Output:

Traceback (most recent call last):
  File "D:\temp\test.py", line 7, in <module>
    predictions[predictions > 1] = 1
TypeError: 'numpy.float64' object does not support item assignment

So, what your vectors are?

Hi Max, how to know what's the vectors are? I am using debug on Spider on the same code but I have no idea how to figure out what is inside the vectors variable – user1314404 May 28, 2015 at 9:06 This is numpy indexing (predictions[predictions > 1] = 1), this syntax can work in the correct context – Guillaume Lebreton Nov 8, 2019 at 22:54

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.