numpy 中的数据转换
- 输入数据是一维 numpy 数组的列表,例如 x[0] = [ array([1.0,1.0,1.0]), array([2.0,2.0,2.0]), ...]
len(x)
约为几千(行),而len(x[n])
是固定数字(列),但可能会因运行而改变运行(所以我不想对许多列进行硬编码)。- 函数
f(x[n][col])
将每个数组
转换为单个数字 - 所需的结果是转换后的列的列表
列表用于绘图,因此它们可以是numpy 数据结构。 下面是一些设置测试数据和概念转换的代码:
import numpy
# create test data set
def datagen(number):
return numpy.array([number,number,number])
x=[]
for rows in range(20):
dataline = [ datagen(n) for n in range(5)]
x.append(dataline)
#define transformation of array to single number
def f(in_array):
return in_array.sum()
期望的结果——以 numpy、python 的方式获得:
[ array([0,0,0,...0]), array([3,3,3,....,3]), array([6,6,6,...,6]), ..etc]
在这种情况下,每个数组有 20 个元素(每行数据一个),并且有 5 个数组列表(每列一个)。
这是我目前的解决方案:
trans = []
for dataline in x:
trans.append([f(a) for a in dataline])
trans = numpy.array(trans)
answer = [ trans[:,col] for col in range(len(x[0])) ]
不太破旧,但我头疼,我感觉这可以做得更好。 ???
在现实生活中,f(a) = numpy.sqrt(numpy.vdot(a,a))。
- Input data are lists of 1-D numpy arrays e.g.
x[0] = [ array([1.0,1.0,1.0]), array([2.0,2.0,2.0]), ...]
len(x)
is on the order of a few thousand (rows) whilelen(x[n])
is a fixed number (columns), but may change from run to run (so I don't want to hard-code a number of columns).- Function
f(x[n][col])
transforms eacharray
into a single number - Desired result is a list of transformed columns
The lists are for plotting, so they could be a numpy data structure.
Here is some code to set up test data and notional transformation:
import numpy
# create test data set
def datagen(number):
return numpy.array([number,number,number])
x=[]
for rows in range(20):
dataline = [ datagen(n) for n in range(5)]
x.append(dataline)
#define transformation of array to single number
def f(in_array):
return in_array.sum()
Desired result-- get in a numpy, pythonic sort of way:
[ array([0,0,0,...0]), array([3,3,3,....,3]), array([6,6,6,...,6]), ..etc]
where in this case each array has 20 elements (one for each row of data) and there are 5 arrays in the list (one for each column).
Here is my current solution:
trans = []
for dataline in x:
trans.append([f(a) for a in dataline])
trans = numpy.array(trans)
answer = [ trans[:,col] for col in range(len(x[0])) ]
Not too shabby but my head hurts and I have a feeling this can be done better. ???
In real life f(a) = numpy.sqrt(numpy.vdot(a,a))
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
怎么样:
How about: