如果您在 Python 中创建一维数组,使用 NumPy 包有什么好处吗?
Hortitude
问问题
36736 次
3 回答
5
为任何可能觉得这很有用的人的利益而进行的小型引导(遵循@dF的出色回答。):
import numpy as np
from array import array
# Fixed size numpy array
def np_fixed(n):
q = np.empty(n)
for i in range(n):
q[i] = i
return q
# Resize with np.resize
def np_class_resize(isize, n):
q = np.empty(isize)
for i in range(n):
if i>=q.shape[0]:
q = np.resize(q, q.shape[0]*2)
q[i] = i
return q
# Resize with the numpy.array method
def np_method_resize(isize, n):
q = np.empty(isize)
for i in range(n):
if i>=q.shape[0]:
q.resize(q.shape[0]*2)
q[i] = i
return q
# Array.array append
def arr(n):
q = array('d')
for i in range(n):
q.append(i)
return q
isize = 1000
n = 10000000
输出给出:
%timeit -r 10 a = np_fixed(n)
%timeit -r 10 a = np_class_resize(isize, n)
%timeit -r 10 a = np_method_resize(isize, n)
%timeit -r 10 a = arr(n)
1 loop, best of 10: 868 ms per loop
1 loop, best of 10: 2.03 s per loop
1 loop, best of 10: 2.02 s per loop
1 loop, best of 10: 1.89 s per loop
似乎 array.array 稍微快一些,并且“api”为您节省了一些麻烦,但如果您需要的不仅仅是存储双精度数,那么 numpy.resize 毕竟不是一个糟糕的选择(如果使用正确的话)。
于 2017-05-09T09:21:46.723 回答
0
出于存储目的,numpy array 优于 array.array。这是用于比较 4 字节无符号整数的存储大小的基准代码。其他数据类型也可用于比较。还添加了列表和元组的数据以进行比较。对于小于 10 的数字,可以使用列表和元组。
a = [100400, 1004000, 10040, 100400, 100400, 100400, 400, 1006000, 8, 800999]
print("size per element for list, tuple, numpy array, array.array:===============")
for i in range(1, 15):
aa = a*i#list size multiplier
n = len(aa)
list_size = getsizeof(aa)
d = list_size
tup_aa = tuple(aa)
tup_size = getsizeof(tup_aa)
nparr = np.array(aa, dtype='uint32')
np_size = getsizeof(nparr)
arr = array('L', aa)#4 byte unsigned integer
arr_size = getsizeof(arr)
print('number of element:%s, list %.2f, tuple %.2f, np.array %.2f, arr.array %.2f' % \
(len(aa), list_size/n, tup_size/n, np_size/n, arr_size/n))
这在我的机器中产生了以下输出:
size per element for list, tuple, numpy array, array.array:===============
number of element:10, list 14.40, tuple 12.80, np.array 13.60, arr.array 14.40
number of element:20, list 11.20, tuple 10.40, np.array 8.80, arr.array 11.20
number of element:30, list 10.13, tuple 9.60, np.array 7.20, arr.array 10.13
number of element:40, list 9.60, tuple 9.20, np.array 6.40, arr.array 9.60
number of element:50, list 9.28, tuple 8.96, np.array 5.92, arr.array 9.28
number of element:60, list 9.07, tuple 8.80, np.array 5.60, arr.array 9.07
number of element:70, list 8.91, tuple 8.69, np.array 5.37, arr.array 8.91
number of element:80, list 8.80, tuple 8.60, np.array 5.20, arr.array 8.80
number of element:90, list 8.71, tuple 8.53, np.array 5.07, arr.array 8.71
number of element:100, list 8.64, tuple 8.48, np.array 4.96, arr.array 8.64
number of element:110, list 8.58, tuple 8.44, np.array 4.87, arr.array 8.58
number of element:120, list 8.53, tuple 8.40, np.array 4.80, arr.array 8.53
number of element:130, list 8.49, tuple 8.37, np.array 4.74, arr.array 8.49
number of element:140, list 8.46, tuple 8.34, np.array 4.69, arr.array 8.46
于 2021-04-16T20:26:06.890 回答