Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 numpy数组中每个对象的数据类型_Python_Arrays_Numpy_Scipy - Fatal编程技术网

指定python numpy数组中每个对象的数据类型

指定python numpy数组中每个对象的数据类型,python,arrays,numpy,scipy,Python,Arrays,Numpy,Scipy,下面的代码片段创建了一个“典型测试数组”,这个数组的目的是测试我的程序中的各种东西。是否有方法或甚至可能更改数组中元素的类型 import numpy as np import random from random import uniform, randrange, choice # ... bunch of silly code ... def gen_test_array( ua, low_inc, med_inc, num_of_vectors ): #typical_array

下面的代码片段创建了一个“典型测试数组”,这个数组的目的是测试我的程序中的各种东西。是否有方法或甚至可能更改数组中元素的类型

import numpy as np
import random
from random import uniform, randrange, choice

# ... bunch of silly code ...

def gen_test_array( ua, low_inc, med_inc, num_of_vectors ):
  #typical_array = [ zone_id, ua, inc, veh, pop, hh, with_se, is_cbd, re, se=0, oe]
  typical_array = np.zeros( shape = ( num_of_vectors, 11 ) )

  for i in range( 0, num_of_vectors ):
    typical_array[i] = [i, int( ua ), uniform( low_inc / 2, med_inc * 2 ), uniform( 0, 6 ),
                        randrange( 100, 5000 ), randrange( 100, 500 ),
                        choice( [True, False] ), choice( [True, False] ),
                        randrange( 100, 5000 ), randrange( 100, 5000 ),
                        randrange( 100, 5000 ) ]

  return typical_array

引用本手册第1章的第一行


因此,数组的每个成员都必须是相同类型的。与常规Python列表相比,这里失去了通用性,这是一种折衷,允许在数组上进行高速操作:循环可以在不测试每个成员类型的情况下运行。

在numpy中实现这一点的方法是使用

然而,在许多使用异构数据的情况下,简单的python列表是更好的选择。(或者,尽管在编写此答案时没有广泛使用,但是
pandas.DataFrame
绝对适合此场景。)

无论如何,您上面给出的示例将作为“普通”numpy数组完美地工作。在你给出的例子中,你可以把所有的东西都变成浮点数。(除了两列浮点之外,所有内容都是int……布尔可以很容易地表示为int。)

尽管如此,为了说明如何使用结构化数据类型

import numpy as np

ua = 5 # No idea what "ua" is in your code above...
low_inc, med_inc = 0.5, 2.0 # Again, no idea what these are...

num = 100
num_fields = 11

# Use more descriptive names than "col1"! I'm just generating the names as placeholders
dtype = {'names':['col%i'%i for i in range(num_fields)],
                 'formats':2*[np.int] + 2*[np.float] + 2*[np.int] + 2*[np.bool] + 3*[np.int]}
data = np.zeros(num, dtype=dtype)

# Being rather verbose...
data['col0'] = np.arange(num, dtype=np.int)
data['col1'] = int(ua) * np.ones(num)
data['col2'] = np.random.uniform(low_inc / 2, med_inc * 2, num)
data['col3'] = np.random.uniform(0, 6, num)
data['col4'] = np.random.randint(100, 5000, num)
data['col5'] = np.random.randint(100, 500, num)
data['col6'] = np.random.randint(0, 2, num).astype(np.bool)
data['col7'] = np.random.randint(0, 2, num).astype(np.bool)
data['col8'] = np.random.randint(100, 5000, num)
data['col9'] = np.random.randint(100, 5000, num)
data['col10'] = np.random.randint(100, 5000, num)

print data
这将生成一个包含11个字段的100元素数组:

array([ (0, 5, 2.0886534380436226, 3.0111285613794276, 3476, 117, False, False, 4704, 4372, 4062),
       (1, 5, 2.0977199579338115, 1.8687472941590277, 4635, 496, True, False, 4079, 4263, 3196),
       ...
       ...
       (98, 5, 1.1682309811443277, 1.4100766819689299, 1213, 135, False, False, 1250, 2534, 1160),
       (99, 5, 1.746554619056416, 5.210411489007637, 1387, 352, False, False, 3520, 3772, 3249)], 
      dtype=[('col0', '<i8'), ('col1', '<i8'), ('col2', '<f8'), ('col3', '<f8'), ('col4', '<i8'), ('col5', '<i8'), ('col6', '|b1'), ('col7', '|b1'), ('col8', '<i8'), ('col9', '<i8'), ('col10', '<i8')])
数组([(0,5,2.0886534380436226,3.01112858613794276,3476,117,False,False,4704,4372,4062),
(1,5,2.0977199579338115,1.8687472941590277463496,对,错,407942633196),
...
...
(98,5,1.1682309811443277,1.41007668196892991213135,假,假,125025341160),
(99,5,1.746554619056416,5.2104114890076371387352,假,假,35203773249)],

dtype=[('col0','使用np.array还有其他选择吗?你到底想做什么?我认为numpy可以加速数学:矩阵乘法,或者获取一大堆输入的余弦。在不了解更多关于你在做什么的情况下,我所能建议的只是一个常规的Python列表。
array([ (0, 5, 2.0886534380436226, 3.0111285613794276, 3476, 117, False, False, 4704, 4372, 4062),
       (1, 5, 2.0977199579338115, 1.8687472941590277, 4635, 496, True, False, 4079, 4263, 3196),
       ...
       ...
       (98, 5, 1.1682309811443277, 1.4100766819689299, 1213, 135, False, False, 1250, 2534, 1160),
       (99, 5, 1.746554619056416, 5.210411489007637, 1387, 352, False, False, 3520, 3772, 3249)], 
      dtype=[('col0', '<i8'), ('col1', '<i8'), ('col2', '<f8'), ('col3', '<f8'), ('col4', '<i8'), ('col5', '<i8'), ('col6', '|b1'), ('col7', '|b1'), ('col8', '<i8'), ('col9', '<i8'), ('col10', '<i8')])