gpt4 book ai didi

python - 在python中连接多个数组

转载 作者:行者123 更新时间:2023-12-01 03:09:20 25 4
gpt4 key购买 nike

我有这个代码用于连接两个数组。

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 个数组。我怎样才能完成相同的过程?

import numpy as np
from hmmlearn import hmm
model = hmm.MultinomialHMM(n_components=3, n_iter=10,algorithm='map',tol=0.00001)
sample = np.array([])
lengths = []
for i in range(1:10)
?????????????
model.fit(sample,lengths)

最佳答案

为了连接多个数组,只需将该数组与所有先前数组的连接连接起来即可。

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

关于python - 在python中连接多个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43026536/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com