回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<ul>
<li><a href="https://stackoverflow.com/questions/3410147/define-dtypes-in-numpy-using-a-list">This is a similar question using dtypes in a list</a></li>
</ul>
<p>下面的代码片段创建了一个“典型的测试数组”,这个数组的目的是测试我的程序中的各种东西。有没有办法甚至有可能改变数组中元素的类型?</p>
<pre><code>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
</code></pre>