[英]How to repeat a series of numbers with shape (n,1) as an array of n unique numbers repeated by 4 times with a shape of (n,1)
I think this is a relatively straightfoward question but I've been struggling with getting this to the right shape.我认为这是一个相对简单的问题,但我一直在努力使其达到正确的形状。 I have a series/dataframe column structured as:我有一个系列/数据框列的结构如下:
0 0.127883
1 0.129979
2 0.130000
...
1000 0.090000
I want to turn this into:我想把它变成:
[[array([0.12788259, 0.12788259, 0.12788259, 0.12788259])]
[array([0.12997902, 0.12997902, 0.12997902, 0.12997902])]
[array([0.13, 0.13, 0.13, 0.13])]
...
[array([0.09, 0.09, 0.09, 0.09])]
[array([0.09, 0.09, 0.09, 0.09])]
[array([0.09, 0.09, 0.09, 0.09])]]
Essentially, I am trying to create a matrix with shape (n,1) containing the input number repeated by 4 times, but wrapped in an array.本质上,我试图创建一个形状为 (n,1) 的矩阵,其中包含重复 4 次的输入数字,但包装在一个数组中。 I have only been able to get to the following:我只能得到以下内容:
arr_out = np.array(np.tile(np.array(a).reshape(-1,1),4))
and the corresponding result, which while looks the same, is missing the comma in between variables and without the 'array' wrapper:相应的结果虽然看起来相同,但缺少变量之间的逗号且没有“数组”包装器:
[[1.12788259 1.12788259 1.12788259 1.12788259]
[1.12997902 1.12997902 1.12997902 1.12997902]
[1.13 1.13 1.13 1.13 ]
...
[1.09 1.09 1.09 1.09 ]
[1.09 1.09 1.09 1.09 ]
[1.09 1.09 1.09 1.09 ]]
Thank you in advance!先感谢您!
How about this method:这个方法怎么样:
import numpy as np
ser = np.arange(5, dtype = float) # Edit: added argument 'dtype = float'
arr = ser.repeat(4).reshape(-1, 4)
l = list(arr)
ob_arr = np.array([None for i in range(len(l))], dtype = object)
for i in range(len(ob_arr)):
ob_arr[i] = l[i]
output:输出:
array([array([0., 0., 0., 0.]), array([1., 1., 1., 1.]),
array([2., 2., 2., 2.]), array([3., 3., 3., 3.]),
array([4., 4., 4., 4.])], dtype=object)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.