【问题标题】:naive bayes fit method Reshape array朴素贝叶斯拟合方法重塑数组
【发布时间】:2020-02-12 13:15:43
【问题描述】:

我正在尝试通过连接词向量来创建句子嵌入。这是我写的代码:

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)

我正在使用朴素贝叶斯进行分类:

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

nb.fit方法处给出错误

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:

我已经尝试过重塑 (1,-1) 并审查了 this post 但没有用。

【问题讨论】:

    标签: python


    【解决方案1】:

    x.reshape(-1,1) 不是x.reshape(1,-1)。您需要 100 个样本和 1 个特征,而不是 1 个样本和 100 个特征。

    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)
    

    【讨论】:

      猜你喜欢
      • 2018-02-14
      • 1970-01-01
      • 2012-02-21
      • 2011-12-28
      • 2016-05-24
      • 2013-09-09
      • 2015-01-03
      • 2017-02-09
      • 2015-08-27
      相关资源
      最近更新 更多