ValueError:预期的2D数组,取而代之的是1D数组。训练模式

时间:2019-02-14 23:03:21

标签: python scikit-learn sentiment-analysis

我想训练12种模型来进行情感分析,因为我有4个方面和3个极性(积极,中立,消极)。但是我有这样的错误

ValueError: Expected 2D array, got 1D array instead:
array=[].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

我在这里更新了我的代码以使其更加清晰, 这是我的代码:

fix = []
for review in dump:
    review['text'] = prep(review['text'])
    temp = {}
    temp['text'] = review['text']
fix.append(temp)

count_vec = []
def count_ngram(index, numb, wrd):
if numb>ngram:
    return
new_word= ""
for elem in word_list[index]:
    if len(wrd)>0:
        new_word = elem+" "+wrd
    else:
        new_word = elem
    if index>0 and numb<ngram:
        count_ngram(index-1, numb+1, new_word)
    if new_word in vec_index:
        count_vec[vec_index[new_word]]+= 1

for elem in fix:
    count_vec = [0]*features
    words = elem['text'].split()
    word_list = []
    for word in words:
        word_set = set()
        length = len(word)
        word_set.add(word)
        word_list.append(word_set)
    l = len(word_list)
    for i in range(l):
        count_ngram(i, 1, "")
    elem['vec'] = count_vec

x_data=[[],[],[],[],[],[],[],[],[],[],[],[],[]]
y_data=[[],[],[],[],[],[],[],[],[],[],[],[],[]]

def check(a):
    x_data[a].append(elem['vec'])
    if elem[str(a)]>0:
        x_data[4+a].append(elem['vec'])
        if elem[str(a)]>1:
           y_data[4+a].append(1)
        else:
           y_data[4+a].append(0)
        y_data[a].append(1)
    else:
      y_data[a].append(0)
for elem in fix:
    for i in range(4):
        check(i+1)

for i in range(12):
    model=svm.SVC(kernel='linear')
    print("training",i+1)
    model.fit(x_data[i+1],y_data[i+1])

我制作模型的方式有什么问题吗?最初,此代码用于制作8个模型。因此,而不是

1 个答案:

答案 0 :(得分:0)

在不知道elem变量中包含什么的情况下,使用svm之前的代码在很大程度上没有帮助。但是假设X和Y中的每个元素都将是一个列表,我将尝试将变量传递给model.fit():

model.fit([x_data[i+1]],[y_data[i+1]])

这与第一个示例不同 https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html,他们也使用numpy数组。因此,再次取决于X和Y的组成,您可能还需要将它们转换为适当的numpy数组。