在python中连接多个数组

2024-10-01 22:37:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我有连接两个数组的代码。在

import numpy as np
from hmmlearn import hmm
model = hmm.MultinomialHMM(n_components=3, n_iter=10,algorithm='map',tol=0.00001)
sequence3 = np.array([[2, 1, 0, 1]]).T
sequence4 = np.array([[2, 1, 0, 1, 1]]).T
sample = np.concatenate([sequence3, sequence4])
lengths = [len(sequence3), len(sequence4)]
model.fit(sample,lengths)

而且工作正常。但现在如果我有两个以上的数组。假设我有10个数组。我怎么能做同样的过程?在

^{pr2}$

Tags: sample代码fromimportnumpymodellenas
3条回答

为了连接多个数组,您只需将该数组与所有先前的数组串联起来。在

# Create arrays
arrays=[
    np.array([1,2,3]),
    np.array([4,5,6]),
    np.array([7,8,9])
]

# Create an array to return to
sample = np.array([])

for array in arrays:
    sample = np.concatenate([sample, array])

# Print results
print('sample', sample)
print('length', len(sample))

您可以使用vstack

也就是说

Equivalent to np.concatenate(tup, axis=0) if tup contains arrays that are at least 2-dimensional.

将数组存储为一个列表,比如array_list

print np.vstack(array_list) 

样品:

^{pr2}$

希望有帮助!在

With Ramda

const one = [1, 2, 3];
const two = [4, 5, 6];
const three = [7, 8, 9];

R.reduce(R.concat, [], [one, two, three])

// [1,2,3,4,5,6,7,8,9]

相关问题 更多 >

    热门问题