분류 예측 코드 질문드립니다. TypeError: only size-1 arrays can be converted to Python scalars
조회수 4076회
분류 모델을 학습하여 저장한 후에, 새로운 데이터를 모델에 적용시키려고 합니다.
새 데이터를 가져와 패딩 단계까지 완료했는데, 여러개 리스트를 한번에 적용시키는 방법을 몰라서 질문드립니다!
아래와 같은 여러개 리스트 각각에 모델을 적용하고, 분류 결과와 확률을 도출하고 표 형태로 출력하고 싶습니다.
score = float(loaded_model.predict(pad_new))
이렇게 시행하면
TypeError: only size-1 arrays can be converted to Python scalars
이런 에러가 뜹니다.
-추가-
제가 참고한 코드는 아래와 같이 한 문장을 분류하는 코드입니다.
def sentiment_predict(new_sentence):
new_sentence = mecab.morphs(new_sentence) # 토큰화
new_sentence = [word for word in new_sentence if not word in stopwords] # 불용어 제거
encoded = tokenizer.texts_to_sequences([new_sentence]) # 정수 인코딩
pad_new = pad_sequences(encoded, maxlen = max_len) # 패딩
score = float(loaded_model.predict(pad_new)) # 예측
if(score > 0.5):
print("{:.2f}% 확률로 긍정 리뷰입니다.".format(score * 100))
else:
print("{:.2f}% 확률로 부정 리뷰입니다.".format((1 - score) * 100))
sentiment_predict('이 상품 진짜 좋아요... 저는 강추합니다. 대박')
98.88% 확률로 긍정 리뷰입니다.
어떻게 하면 좋을지 도와주시면 감사드리겠습니다!
댓글 입력