步长
import numpy as np
x = np.arange(8, dtype = np.int8)
x
x.strides
str(x.data)
x.shape = 2, 4
x
'''
array([[0, 1, 2, 3],
[4, 5, 6, 7]], dtype=int8)
'''
x.strides
str(x.data)
x.shape = 1,4,2
x.strides
str(x.data)
'''
对于连续数组(flags 中为连续):
strides[ndim - 1] = itemsize
strides[i] = strides[i + 1] * shape[i + 1]
def calc_strides(shape, itemsize):
ndim = len(shape)
strides = [0] * ndim
strides[-1] = itemsize
for i in xrange(ndim - 2, -1, -1):
strides[i] = strides[i + 1] * shape[i + 1]
return strides
'''
x = np.ones((10000,))
y = np.ones((10000 * 100, ))[::100]
x.shape, y.shape
x == y
x.flags
'''
C_CONTIGUOUS : True
F_CONTIGUOUS : True
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
由于 x 是一维数组,所以行和列都连续
'''
y.flags
'''
C_CONTIGUOUS : False
F_CONTIGUOUS : False
OWNDATA : False
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
y 是由切片产生的,所以行和列都不连续
并且没有自己的数据
'''
x.strides, y.strides
'''
不连续数组由于不是缓存友好的
访问也较慢
%timeit x.sum()
100000 loops, best of 3: 13.8 µs per loop
%timeit y.sum()
10000 loops, best of 3: 25.9 µs per loop
结构化数组
x = np.array([(1, 0.5, 'NumPy'), (10, -0.5, 'Essential')],
dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', 'S10')])
x[0]
x['f2']
y = x['f0']
y
y[:] = y * 10
y
y[:] = y + 0.5
y
x
'''
array([(10, 0.5, 'NumPy'), (100, -0.5, 'Essential')],
dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', 'S10')])
'''
z = np.ones((2,), dtype = ('3i4, (2,3)f4'))
z
'''
array([([1, 1, 1], [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]]),
([1, 1, 1], [[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]])],
dtype=[('f0', '<i4', (3,)), ('f1', '<f4', (2, 3))])
'''
x.dtype.names
x.dtype.names = ('id', 'value', 'note')
x
'''
array([(10, 0.5, 'NumPy'), (100, -0.5, 'Essential')],
dtype=[('id', '<i4'), ('value', '<f4'), ('note', 'S10')])
'''
dict_ex = np.zeros((2,), dtype = {'names':['id', 'value'], 'formats':['i4', '2f4']})
dict_ex
'''
array([(0, [0.0, 0.0]), (0, [0.0, 0.0])],
dtype=[('id', '<i4'), ('value', '<f4', (2,))])
'''
x[['id', 'note']]
'''
array([(10, 'NumPy'), (100, 'Essential')],
dtype=[('id', '<i4'), ('note', 'S10')])
'''
日期时间
x = np.datetime64('2015-04-01')
y = np.datetime64('2015-04')
x.dtype, y.dtype
y = np.datetime64('2015-04', 'D')
y, y.dtype
x = np.arange('2015-01', '2015-04', dtype = 'datetime64[M]')
x
y = np.datetime64('2015-04-01', 's')
x
y = np.datetime64('2015-01-01')
x - y
np.datetime64('2015') + np.timedelta64(12, 'M')
np.timedelta64(1, 'W') / np.timedelta64(1, 'D')
x
x.tolist()
'''
[datetime.date(2015, 1, 1),
datetime.date(2015, 2, 1),
datetime.date(2015, 3, 1)]
'''
[element.item() for element in x]
'''
[datetime.date(2015, 1, 1),
datetime.date(2015, 2, 1),
datetime.date(2015, 3, 1)]
'''
NumPy 文件 IO
id = np.arange(1000)
value = np.random.random(1000)
day = np.random.random_integers(0, 365, 1000) * np.timedelta64(1,'D')
date = np.datetime64('2014-01-01') + day
rec_array = np.core.records.fromarrays([id, value, date], names='id, value, date', formats='i4, f4, a10')
rec_array[:5]
'''
rec.array([(0, 0.07019801437854767, '2014-07-10'),
(1, 0.4863224923610687, '2014-12-03'),
(2, 0.9525277614593506, '2014-03-11'),
(3, 0.39706873893737793, '2014-01-02'),
(4, 0.8536589741706848, '2014-09-14')],
dtype=[('id', '<i4'), ('value', '<f4'), ('date', 'S10')])
'''
np.savetxt('./record.csv', rec_array, fmt='%i,%.4f,%s')
read_array = np.genfromtxt('./record.csv', dtype='i4,f4,a10', delimiter=',', skip_header=0)
read_array[:5]
'''
array([(0, 0.07020000368356705, '2014-07-10'),
(1, 0.486299991607666, '2014-12-03'),
(2, 0.9524999856948853, '2014-03-11'),
(3, 0.3971000015735626, '2014-01-02'),
(4, 0.8536999821662903, '2014-09-14')],
dtype=[('f0', '<i4'), ('f1', '<f4'), ('f2', 'S10')])
'''
read_array.dtype.names = ('id', 'value', 'date')
mask = read_array['value'] >= 0.75
from numpy.lib.recfunctions import append_fields
read_array = append_fields(read_array, 'mask', data=mask, dtypes='i1')
read_array[:5]
'''
masked_array(data = [(0, 0.07020000368356705, '2014-07-10', 0)
(1, 0.486299991607666, '2014-12-03', 0)
(2, 0.9524999856948853, '2014-03-11', 1)
(3, 0.3971000015735626, '2014-01-02', 0)
dtype = [('id', '<i4'), ('value', '<f4'), ('date', 'S10'), ('mask','i1')])
'''