在python中连接许多数组
我有这段代码用于连接两个数组在python中连接许多数组,python,arrays,numpy,Python,Arrays,Numpy,我有这段代码用于连接两个数组 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([seq
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)
你可以用
就是
如果tup包含
至少是二维的
将数组存储为列表,例如array\u list
print np.vstack(array_list)
样本:
import numpy as np
sequence3 = np.array([[2, 1]]).T
sequence4 = np.array([[2, 5]]).T
sequence5 = np.array([[4, 5]]).T
sequence6 = np.array([[6, 7]]).T
array_list=[sequence3,sequence4,sequence5,sequence6]
sample = np.concatenate([sequence3, sequence4])
lengths = [len(sequence3), len(sequence4)]
print np.vstack(array_list)
[[2]
[1]
[2]
[5]
[4]
[5]
[6]
[7]]
希望有帮助 为了连接多个数组,只需将该数组与之前所有数组的连接连接起来即可
# 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))
因此,您可能需要新的数组
sample
以及列表length
中的数组长度?您可以一次连接多个数组。把它们都放在参数列表里。但是维度必须匹配。它仍然会给我这个错误“ValueError:需要一个多项式分布的样本。”在这条直线模型中。fit(样本,长度)只需使用concatenate(数组)
现在我在这条直线模型中遇到了这个错误“ValueError:长度数组[3,3,3]中超过1个样本。fit(np.concatenate(数组),长度)它现在正在和我一起工作。问题是数组应该是。数组=[np.array([[1,2,3]]),np.array([[4,5,6]]),np.array([[7,8,9]]),我使用了np.concatenate(数组),直接使用,因为数组已经是2d,我建议使用axis=0进行连接。