【问题标题】:Python spacy issue when training :ValueError: Found array with 0 feature(s) (shape=(n, 0)) while a minimum of 1 is required by SVC训练时的 Python spacy 问题:ValueError:找到具有 0 个特征(形状=(n,0))的数组,而 SVC 需要至少 1 个
【发布时间】:2022-12-05 18:35:24
【问题描述】:

我遇到了我的数组不正确的值错误,这非常奇怪,因为我已经确认我的数组不为零。我打印出了每个特征和训练集的“len”。找到具有 0 个特征 (shape=(7, 0)) 的数组,而 SVC 至少需要 1 个特征。我正在使用 spacy 3.4.1 和 python 3.8.10。我究竟做错了什么?

import spacy
from sklearn import svm

nlp = spacy.load("en_core_web_trf")

train_x = [
        "good characters and plot progression", 
        "check out the book", 
        "good story. would recommend", 
        "novel recommendation", 
        "need to make a deposit to the bank", 
        "balance inquiry savings", 
        "save money"
        ]


train_y = [
            "BOOKS", 
            "BOOKS", 
            "BOOKS", 
            "BOOKS", 
            "BANK", 
            "BANK", 
            "BANK", 
            ]


docs = [nlp(text) for text in train_x]
train_x_vectors = [doc.vector for doc in docs]

print (len(train_x_vectors))
print (len(train_y))

clf_svm = svm.SVC(kernel='linear')
clf_svm.fit(train_x_vectors, train_y)

【问题讨论】:

  • 默认情况下,Doc.vector 将在 spaCy 中使用 trf 管道为空。你确定你不是在发送空向量吗?

标签: python-3.x nlp spacy-3


【解决方案1】:

spacy 管道包“en_core_web_trf”没有自带词向量 您需要使用 en_core_web_lg 才能使用“.vector”

【讨论】:

    猜你喜欢
    • 2019-02-02
    • 2021-03-06
    • 2019-09-25
    • 2022-01-16
    • 2016-10-04
    • 2019-04-24
    • 2023-02-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多