Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中是否有存储XY数据的标准方法?_Python_Numpy_Standards - Fatal编程技术网

在python中是否有存储XY数据的标准方法?

在python中是否有存储XY数据的标准方法?,python,numpy,standards,Python,Numpy,Standards,在python中是否有存储(x,y)、(x,y,z)或(x,y,z,t)数据的标准方法 我知道numpy数组经常用于类似的事情,但我想你也可以使用numpy矩阵 我已经看到两个列表被压缩在一起的使用,这两个方面完全取代了numpy的使用 XY_data = zip( [x for x in range(0,10)] , [y for y in range(0,10)] ) 有标准吗?如果不是,你最喜欢的方式是什么,或者你见过的最多的方式是什么?一个好的方式是使用一个。这提供了numpy阵列的所

在python中是否有存储(x,y)、(x,y,z)或(x,y,z,t)数据的标准方法

我知道numpy数组经常用于类似的事情,但我想你也可以使用numpy矩阵

我已经看到两个列表被压缩在一起的使用,这两个方面完全取代了numpy的使用

XY_data = zip( [x for x in range(0,10)] , [y for y in range(0,10)] )
有标准吗?如果不是,你最喜欢的方式是什么,或者你见过的最多的方式是什么?

一个好的方式是使用一个。这提供了numpy阵列的所有优点,但提供了一种方便的访问结构

要使numpy数组成为“结构化”数组,只需给它一个
dtype
参数。这为每个“字段”提供了名称和类型。如果您愿意,它们甚至可以具有更复杂的形状和层次结构,但以下是我保存x-y数据的方法:

In [175]: import numpy as np

In [176]: x = np.random.random(10)

In [177]: y = np.random.random(10)

In [179]: zip(x,y)
Out[179]: 
[(0.27432965895978034, 0.034808254176554643),
 (0.10231729328413885, 0.3311112896885462),
 (0.87724361175443311, 0.47852682944121905),
 (0.24291769332378499, 0.50691735432715967),
 (0.47583427680221879, 0.04048957803763753),
 (0.70710641602121627, 0.27331443495117813),
 (0.85878694702522784, 0.61993945461613498),
 (0.28840423235739054, 0.11954319357707233),
 (0.22084849730366296, 0.39880927226467255),
 (0.42915612628398903, 0.19197320645915561)]

In [180]: data = np.array( zip(x,y), dtype=[('x',float),('y',float)])

In [181]: data['x']
Out[181]: 
array([ 0.27432966,  0.10231729,  0.87724361,  0.24291769,  0.47583428,
        0.70710642,  0.85878695,  0.28840423,  0.2208485 ,  0.42915613])

In [182]: data['y']
Out[182]: 
array([ 0.03480825,  0.33111129,  0.47852683,  0.50691735,  0.04048958,
        0.27331443,  0.61993945,  0.11954319,  0.39880927,  0.19197321])

In [183]: data[0]
Out[183]: (0.27432965895978034, 0.03480825417655464)
其他人可能会建议使用,但如果您的数据相对简单,那么纯numpy可能更容易

如果愿意,您可以添加层次结构,但它通常比必要的更复杂

例如:

In [200]: t = np.arange(10)

In [202]: dt = np.dtype([('t',int),('pos',[('x',float),('y',float)])])

In [203]: alldata = np.array(zip(t, zip(x,y)), dtype=dt)

In [204]: alldata
Out[204]: 
array([(0, (0.27432965895978034, 0.03480825417655464)),
       (1, (0.10231729328413885, 0.3311112896885462)),
       (2, (0.8772436117544331, 0.47852682944121905)),
       (3, (0.242917693323785, 0.5069173543271597)),
       (4, (0.4758342768022188, 0.04048957803763753)),
       (5, (0.7071064160212163, 0.27331443495117813)),
       (6, (0.8587869470252278, 0.619939454616135)),
       (7, (0.28840423235739054, 0.11954319357707233)),
       (8, (0.22084849730366296, 0.39880927226467255)),
       (9, (0.429156126283989, 0.1919732064591556))], 
      dtype=[('t', '<i8'), ('pos', [('x', '<f8'), ('y', '<f8')])])

In [205]: alldata['t']
Out[205]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

In [206]: alldata['pos']
Out[206]: 
array([(0.27432965895978034, 0.03480825417655464),
       (0.10231729328413885, 0.3311112896885462),
       (0.8772436117544331, 0.47852682944121905),
       (0.242917693323785, 0.5069173543271597),
       (0.4758342768022188, 0.04048957803763753),
       (0.7071064160212163, 0.27331443495117813),
       (0.8587869470252278, 0.619939454616135),
       (0.28840423235739054, 0.11954319357707233),
       (0.22084849730366296, 0.39880927226467255),
       (0.429156126283989, 0.1919732064591556)], 
      dtype=[('x', '<f8'), ('y', '<f8')])

In [207]: alldata['pos']['x']
Out[207]: 
array([ 0.27432966,  0.10231729,  0.87724361,  0.24291769,  0.47583428,
        0.70710642,  0.85878695,  0.28840423,  0.2208485 ,  0.42915613])
[200]中的
:t=np.arange(10)
在[202]中:dt=np.dtype([('t',int),('pos',[('x',float),('y',float)])
[203]中:alldata=np.array(zip(t,zip(x,y)),dtype=dt)
In[204]:所有数据
出[204]:
数组([(0,(0.2743296589578034,0.03480825417655464)),
(1, (0.10231729328413885, 0.3311112896885462)),
(2, (0.8772436117544331, 0.47852682944121905)),
(3, (0.242917693323785, 0.5069173543271597)),
(4, (0.4758342768022188, 0.04048957803763753)),
(5, (0.7071064160212163, 0.27331443495117813)),
(6, (0.8587869470252278, 0.619939454616135)),
(7, (0.28840423235739054, 0.11954319357707233)),
(8, (0.22084849730366296, 0.39880927226467255)),
(9, (0.429156126283989, 0.1919732064591556))], 

dtype=[('t','你是专门询问空间坐标还是任何任意数据?对我来说,我是在询问空间坐标。但是,我相信,如果你有任何关于这两者的信息,互联网社区都会乐于学习。附带说明:
[x代表x的范围(0,10)]
与简单的
范围(10)相同
谢谢,我以前没有见过这种情况。因此,基本上同时使用zipping和numpy数组可以创建一种不同的数据类型,称为结构化数组?pandas比numpy有什么优势?我还没有使用pandas,但它在对数据集执行许多不同的分组和筛选时功能更强。如果你是一个家庭成员我喜欢裸体的方法,但发现它们缺乏,看看熊猫,但我会从普通的裸体开始。当然,其他人会不同意:)