使用下面的函数可以更方便地创建随机整数数组。
def generate_random_strings(x, i, j):
return np.random.randint(0, 2, size=[x, i, j]).astype(np.float32)
print (generate_random_strings(3, 5, 4))
[[[0. 0. 0. 0.]
[0. 1. 1. 0.]
[0. 1. 1. 0.]
[0. 0. 0. 1.]
[0. 1. 0. 1.]]
[[0. 1. 0. 0.]
[1. 0. 1. 1.]
[0. 1. 1. 0.]
[1. 0. 1. 0.]
[0. 0. 0. 0.]]
[[0. 0. 1. 0.]
[1. 0. 1. 1.]
[0. 0. 0. 1.]
[0. 1. 0. 0.]
[1. 1. 0. 0.]]]
我尝试为字母(a-z)而不是整数构建类似的函数,但我找不到numpy或任何其他可用库的任何内置函数。
所以我用3 - for循环如下,
# Generate random letter
def randomword(len):
return random.choice(string.ascii_lowercase)
x= 3
i= 5
j= 4
buf = []
for _ in range(x):
bu = []
for i in range(i):
b = []
for j in range(j):
b.append(randomword(1))
bu.append(b)
buf.append(np.asarray(bu))
print(np.asarray(buf))
[[['u' 'w' 'w' 'x']
['b' 's' 'p' 'a']
['k' 'u' 'y' 'p']
['p' 'z' 'b' 'u']
['o' 'h' 'c' 'm']]
[['t' 'y' 'b' 'r']
['e' 's' 'e' 't']
['p' 'n' 'd' 'w']
['h' 'f' 'i' 'e']
['o' 'b' 'q' 'r']]
[['x' 'z' 'd' 'x']
['r' 'b' 'f' 'b']
['d' 'h' 'e' 'g']
['p' 'g' 'u' 'x']
['k' 'j' 'z' 'd']]]
那么,现在我的问题是,是否有任何函数作为字符串/字母的np.random.randint()
,如果没有那么,是否有任何pythonic方法来减少(for循环)代码长度。
你可以在所有ascii小写字母上使用numpy.choice
:
import string
import numpy as np
np.random.choice(list(string.ascii_lowercase), size=(3, 5,4))
如果你想坚持numpy,你可以使用'S1'
数据类型,它只是长度为1个字节的字符串。因此,ord
对应于与8位无符号整数相同的数字。因此,您可以使用numpy.random.randint
生成随机无符号8位整数,并将它们转换为字节字符串:
>>> ord('a'), ord('z')
(97, 122)
>>> np.random.randint(97, 123, (3, 5, 4), dtype=np.uint8).view('S1')
array([[[b'p', b'q', b'b', b'x'],
[b'm', b'x', b'e', b'f'],
[b'u', b'h', b'e', b'd'],
[b'o', b'n', b'w', b'v'],
[b'z', b'q', b'g', b'e']],
[[b'f', b'o', b'c', b'j'],
[b'z', b'x', b'l', b'x'],
[b'u', b'f', b'w', b'r'],
[b'q', b'z', b'm', b'o'],
[b't', b'e', b'm', b'e']],
[[b'f', b'i', b'x', b'k'],
[b'z', b'w', b'm', b'g'],
[b't', b'f', b'u', b'q'],
[b'e', b'w', b'w', b'r'],
[b'e', b'q', b'a', b'g']]],
dtype='|S1')
请注意,它的速度是原来的两倍,需要四分之一的内存:
In [8]: %timeit np.random.choice(list(string.ascii_lowercase), size=(10, 10, 10))
24.8 µs ± 311 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [9]: %timeit np.random.randint(97, 123, (10, 10, 10), dtype=np.uint8).view('S1')
7.45 µs ± 95.1 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
In [10]: %timeit np.random.choice(list(string.ascii_lowercase), size=(10, 100, 10))
116 µs ± 2.33 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [11]: %timeit np.random.randint(97, 123, (10, 100, 10), dtype=np.uint8).view('S1')
53.4 µs ± 641 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)
In [12]: %timeit np.random.choice(list(string.ascii_lowercase), size=(10, 100, 100))
993 µs ± 8.34 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [13]: %timeit np.random.randint(97, 123, (10, 100, 100), dtype=np.uint8).view('S1')
503 µs ± 6.13 µs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
In [14]: %timeit np.random.choice(list(string.ascii_lowercase), size=(100, 100, 100))
9.99 ms ± 58.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [15]: %timeit np.random.randint(97, 123, (100, 100, 100), dtype=np.uint8).view('S1')
5.06 ms ± 129 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
根据需要创建任意数量的随机字母,并将它们重塑为一个numpy数组:
import numpy as np
import string
import random
def randomLetters(amount:int):
return random.choices(string.ascii_lowercase, k=amount)
i=5
j=4
x=3
d = np.array(randomLetters(x*i*j)).reshape((x,i,j))
print [[[chr(randint(ord('a'), ord('z'))) for col in range(4)]for row in range(5)] for x in range(3)]
输出:
[
[
['e', 'y', 'y', 'a'],
['p', 'o', 'k', 'z'],
['j', 'p', 'n', 'p'],
['d', 'y', 'k', 's'],
['k', 'c', 'k', 'o']
],
[
['v', 'w', 't', 'a'],
['f', 'a', 't', 'm'],
['h', 'w', 'i', 'x'],
['a', 'w', 's', 'z'],
['x', 'f', 'b', 'b']
],
[
['x', 'f', 'm', 'y'],
['b', 'u', 'z', 's'],
['j', 'p', 'x', 'l'],
['a', 'p', 'b', 'i'],
['z', 's', 'v', 'k']
]
]