【问题标题】:How to change the output of numpy array如何更改numpy数组的输出
【发布时间】:2022-01-20 03:38:43
【问题描述】:

我有一个这样的数据集

我尝试使用以下代码对词汇进行编码

def norm_vector(sentence, model, stopwords):
    vecs = [model[word.lower()] for word in word_tokenize(sentence) if word not in stopwords]  
    norm_vecs = [vec / np.linalg.norm(vec) for vec in vecs if np.linalg.norm(vec) > 0]
    sent_vecs = np.mean(norm_vecs, axis=0)
    return sent_vecs  

vecs_train_LV = [norm_vector(sentence, model_level, sw_indo) for sentence in data.data]
vecs_train_LV = np.array(vecs_train_LV)
vecs_train_LV

这段代码的输出如下:

array([array([-4.35138009e-02, -2.87008341e-02,  9.86183342e-03, -8.87360424e-02,
        1.83405634e-02, -6.50617108e-02, -6.65896460e-02,  8.60413313e-02,
       -5.40735014e-02, -9.36852470e-02,  4.09044847e-02,  1.15336493e-01],
      dtype=float32),
      array([-0.05026853, -0.02421026, -0.03221055, -0.14009777,  0.02021943,
       -0.02261522, -0.08134355,  0.12336601,  0.07266331, -0.10568545,
        0.00092218, -0.03538591], dtype=float32)],
      dtype=object)

但我想要这样的输出:

array([[-0.02279311,  0.04733656,  0.02461601, ...,  0.02087441,
        -0.04001932,  0.00982925],
       [-0.03176997, -0.02204693,  0.01420259, ..., -0.07120648,
        -0.05186931, -0.04976927],
       [-0.01539093, -0.00272909, -0.02494676, ..., -0.02440629,
         0.01618153, -0.05321534],
       ...,
       [-0.05358865,  0.01567019, -0.03903598, ...,  0.01297285,
        -0.02813709, -0.08277859],
       [-0.00991264,  0.0100073 ,  0.04893894, ..., -0.03793963,
        -0.01426407, -0.04612683],
       [ 0.01175337, -0.02632044, -0.04074009, ..., -0.00123112,
         0.03152793, -0.00567225]], dtype=float32)

我该如何解决这种情况?因为使用数组输出我无法进行分类。

谢谢

【问题讨论】:

  • arr.astype(float)
  • 我试过了,还是报错

标签: python arrays pandas numpy


【解决方案1】:

不要使用数组构造函数来堆叠你的数组,但是:

vecs_train_LV = np.r_[[norm_vector(sentence, model_level, sw_indo) for sentence in data.data]]

或者:

vecs_train_LV = np.vstack([norm_vector(sentence, model_level, sw_indo) for sentence in data.data])

【讨论】:

  • 当我使用上面的代码时,我得到了这样的错误ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 128 and the array at index 835 has size 1,当我使用代码底部时,我仍然得到了相同的输出,如array([array([-4.35138009e-02, -2.87008341e-02, 9.86183342e-03, -8.87360424e-02, 1.83405634e-02, -6.50617108e-02, -6.65896460e-02, 8.60413313e-02,......
猜你喜欢
  • 2011-11-23
  • 2017-06-17
  • 1970-01-01
  • 2012-11-26
  • 2020-06-14
  • 2012-11-27
  • 1970-01-01
  • 2018-04-06
  • 1970-01-01
相关资源
最近更新 更多