0

i want to train 12 models for my sentiment analysis since i have 4 aspects and 3 polarities (positive, neutral, negative). but i got error like this

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.

i updated my code in here to make it more clear, here is my code:

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])

is there anything wrong with the way i make the model? originally this code was made for making 8 models. so, instead of

Ai Chan
  • 33
  • 6

1 Answers1

0

Without knowing what's inside your elem variable, the code before the svm usage is largely unhelpful. But assuming that each element in X and Y is going to be a list, I would try passing the variable to model.fit() as:

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

This is going off the first example in https://scikit-learn.org/stable/modules/generated/sklearn.svm.SVC.html, where they also use numpy arrays. So depending again of what X and Y are composed of, you may need to convert them to proper numpy arrays too.

deefunkt
  • 331
  • 2
  • 12
  • thanks for your response, i have updated my code to make it more clear. can u figure out what happen with my code? i have tried your suggestion but i got another different error. – Ai Chan Feb 25 '19 at 05:50