2

I am trying to create sentence embedding by concatenating word vectors. Here is the code which I've written:

a=np.zeros(100, dtype=float)
    try:
        a=embedding_dict[ line[0] ] ## get first word embedding;
    except KeyError:
        print ( "Error")
    for k in range(1,len(line)):
        try:
            b=np.zeros(100, dtype=float)
            b=embedding_dict[ line[k] ]
            a=np.concatenate([a,b])
        except KeyError:
            print ( "Error")
        length=len(a)
    if length > 1500: 
        #Reduce Size
        print("Reduction of SIze")
        result_arr = a[:1500]
        # result_arr = truncated_arr[None, :]
    else:
        #Padd
        print("padding")
        result_arr=np.pad(a, (0, 1500 - len(a)%1500), 'constant')
    print(  " result_arr.shape before ",result_arr.shape )
    # test = result_arr.reshape(1, -1)
    # print(  " result_arr.shape before ",test.shape )

    sentence_Vectors.append(result_arr)

I am using naive bayes for classification:

nb = MultinomialNB()
nb.fit(X_train, y_train)
y_pred = nb.predict(X_val)
print('naive bayes using sentence embedding accuracy%s' % accuracy_score(y_pred, y_val))

and error is given at nb.fit method

Traceback (most recent call last):
  File "ConcatEmbedding.py", line 121, in <module>
    nb.fit(X_train, y_train)
  File "/usr/local/lib/python2.7/site-packages/sklearn/naive_bayes.py", line 585, in fit
    X, y = check_X_y(X, y, 'csr')
  File "/usr/local/lib/python2.7/site-packages/sklearn/utils/validation.py", line 756, in check_X_y
    estimator=estimator)
  File "/usr/local/lib/python2.7/site-packages/sklearn/utils/validation.py", line 552, in check_array
    "if it contains a single sample.".format(array))
ValueError: Expected 2D array, got 1D array instead:

I've already tried reshaping (1,-1) and reviewed this post but no use.

saeed foroughi
  • 1,662
  • 1
  • 13
  • 25
Ayman
  • 21
  • 2

1 Answers1

0

x.reshape(-1,1) not x.reshape(1,-1). You want 100 samples with 1 feature, not 1 sample with 100 features.

import numpy as np
from sklearn.naive_bayes import MultinomialNB

x = np.random.random(100)
y = np.random.randint(0,2,size=100)

x = x.reshape(-1,1)
print(x.shape)

nb = MultinomialNB()

nb.fit(x, y)
nb.predict(x)
user2653663
  • 2,818
  • 1
  • 18
  • 22