I have a question about the classification prediction code. TypeError: only size-1 arrays can be converted to Python scalars

Asked 2 years ago, Updated 2 years ago, 67 views

After learning and storing the classification model, you want to apply new data to the model.

I brought new data and completed the padding step, but I don't know how to apply multiple lists at once, so I'm asking you a question!

I would like to apply the model to each of the following lists, derive the classification results and probabilities, and print them out in a table form.

score = float(loaded_model.predict(pad_new))

If you do this,

TypeError: only size-1 arrays can be converted to Python scalars

This error appears.

-Additional -

The code I referred to is the code for classifying one sentence as below.

def sentiment_predict(new_sentence):
  new_sentence = mecab.morphs(new_sentence) # tokenization
  new_sentence = [word for word in new_sentence if not word in stopwords] #Remove disjunctive
  encoded = tokenizer.texts_to_sequences([new_sentence]) # Integer encoding
  pad_new = pad_sequences (encoded, maxlen = max_len) # padding
  score = float(loaded_model.predict(pad_new)) #Prediction
  if(score > 0.5):
    print("{:.2f}% chance positive review."format(score * 100))
  else:
    print("{:.2f}% chance of negative review."format((1 - score) * 100))

sentimental_predict('I really like this product... I strongly recommend it. That's awesome)
98.88% chance positive review.



I'd appreciate it if you could help me with what I should do!

python machine-learning deep-learning

2022-09-20 18:57

2 Answers

Please understand by translating the error message.

Try without float().


2022-09-20 18:57

I got the same error in the same classification analysis, did you solve this?


2022-09-20 18:57

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.