【问题标题】:How to convert array([array([ ....],type=float32)],[array([ ....],type=float32)]) to array([...],[...])?如何将 array([array([ ....],type=float32)],[array([ ....],type=float32)]) 转换为 array([...],[... ])?
【发布时间】:2019-09-05 10:43:13
【问题描述】:

这是我的代码:

def extract_doc2vec(doc, w2v):
vecs = [w2v[word] for word in doc if word in w2v]
if len(vecs) > 0:
    vecs = np.asarray(vecs).sum(axis=0)
    return vecs
y = newsgroups_all['target']

x= []

for doc in newsgroups_all['data']:
    edv = extract_doc2vec(list(gensim.utils.tokenize(doc)),model.wv)
    x.append(edv)

我必须将 retrain_doc 转换成适合的格式

for train_index, test_index in sssd.split(x, y):
   X_train, X_test = x[train_index], x[test_index]
   y_train, y_test = y[train_index], y[test_index]

【问题讨论】:

  • 不清楚你要做什么
  • 标题问题的最后一个参数是一个列表数组。我假设您实际上想要一个二维数组?
  • @00 是的,我必须用 StratifiedShuffleSplit 拆分 x
  • 我没试过,但是np.vstack(myarray) 对你有用吗?
  • 我已经回滚了原始代码,因为不清楚 jdehesa 引入的缩进修复是否正确。也许它只是缺少缩进的部分,并且该编辑可能掩盖了块的末尾。但是,请务必通过editing 并应用适当的缩进来解决您的问题。

标签: python arrays numpy tensorflow


【解决方案1】:

将列表 x 转换为数据帧,然后将其转换为 numpy 数组并应用 shuffle split

data = pd.DataFrame(x).to_numpy()

【讨论】:

  • 请详细说明什么是 shuffle split 以及为什么需要为此同时转换为 pandas 和 numpy。
猜你喜欢
  • 2021-04-15
  • 2015-01-09
  • 2011-07-25
  • 1970-01-01
  • 1970-01-01
  • 2019-07-12
  • 1970-01-01
  • 2019-04-04
相关资源
最近更新 更多