我希望能够将2D numpy
水平分成两部分(80%和20%)。我曾尝试使用np.vsplit()
,但似乎并非针对这种情况。例如,假设我有以下大小为(6,3)的矩阵。我想将其水平拆分为80%和20%[大约(5,3),(1,3)],所以我尝试了以下操作:
M = [[1,2,3],[4,5,6],[7,8,9], [10,11,12], [77,54,11], [424,78,98]]
M = np.asarray(M)
arr1 = np.vsplit(M, int(M.shape[0]* 0.8))[0] # 80% of data goes to arr1
arr2 = np.vsplit(M, int(M.shape[0]* 0.2))[1] # 20% of data goes to arr2
我知道这种尝试是不正确的,但我无法解决(实际上仍在学习python)。如果有人可以帮助您修改此代码,请。谢谢
答案 0 :(得分:1)
您可以使用索引(或使用train_test_split)来做到这一点:
M = [[1,2,3],[4,5,6],[7,8,9], [10,11,12], [77,54,11], [424,78,98]]
M = np.asarray(M)
split_horizontally_idx = int(M.shape[0]* 0.8) # integer for line selection (horizontal selection)
array1 = M[:split_horizontally_idx , :] # indexing/selection of the 80%
array2 = M[split_horizontally_idx: , :] # indexing/selection of the remaining 20%
答案 1 :(得分:0)
您可以根据自己的意愿对列表进行切片。
M = [[1,2,3],[4,5,6],[7,8,9], [10,11,12], [77,54,11], [424,78,98]]
M = np.asarray(M)
a80=M[:(round(0.8*len(M[:,2]))),:]
a20=M[:(round(0.2*len(M[:,2]))),:]
print(a80,"\n\n",a20)