数组索引和切片
x = np.random.random((100, 100))
y = x[42, 87]
print(x[k, :])
a = np.array([[10 * y + x for x in range(6)] for y in range(6)])
'''
+--+--+--+--+--+--+
| 0| 1| 2| 3| 4| 5|
+--+--+--+--+--+--+
|10|11|12|13|14|15|
+--+--+--+--+--+--+
|20|21|22|23|24|25|
+--+--+--+--+--+--+
|30|31|32|33|34|35|
+--+--+--+--+--+--+
|40|41|42|43|44|45|
+--+--+--+--+--+--+
|50|51|52|53|54|55|
+--+--+--+--+--+--+
'''
a[0, 3:5]
'''
array([3, 4])
+--+--+--+--+--+--+
| | | | 3| 4| |
+--+--+--+--+--+--+
| | | | | | |
+--+--+--+--+--+--+
| | | | | | |
+--+--+--+--+--+--+
| | | | | | |
+--+--+--+--+--+--+
| | | | | | |
+--+--+--+--+--+--+
| | | | | | |
+--+--+--+--+--+--+
'''
a[4: ,4:]
'''
array([[44, 45],
[54, 55]])
+--+--+--+--+--+--+
| | | | | | |
+--+--+--+--+--+--+
| | | | | | |
+--+--+--+--+--+--+
| | | | | | |
+--+--+--+--+--+--+
| | | | | | |
+--+--+--+--+--+--+
| | | | |44|45|
+--+--+--+--+--+--+
| | | | |54|55|
+--+--+--+--+--+--+
'''
a[:, 2]
'''
array([ 2, 12, 22, 32, 42, 52])
+--+--+--+--+--+--+
| | | 2| | | |
+--+--+--+--+--+--+
| | |12| | | |
+--+--+--+--+--+--+
| | |22| | | |
+--+--+--+--+--+--+
| | |32| | | |
+--+--+--+--+--+--+
| | |42| | | |
+--+--+--+--+--+--+
| | |52| | | |
+--+--+--+--+--+--+
'''
a[2::2, ::2]
'''
array([[20, 22, 24],
[40, 42, 44]])
+--+--+--+--+--+--+
| | | | | | |
+--+--+--+--+--+--+
| | | | | | |
+--+--+--+--+--+--+
|20| |22| | |24|
+--+--+--+--+--+--+
| | | | | | |
+--+--+--+--+--+--+
|40| |42| | |44|
+--+--+--+--+--+--+
| | | | | | |
+--+--+--+--+--+--+
'''
内存布局
print x.flags
'''
C_CONTIGUOUS : True
F_CONTIGUOUS : False
OWNDATA : True
WRITEABLE : True
ALIGNED : True
UPDATEIFCOPY : False
C_CONTIGUOUS:是否为 C 风格连续,也就是行为主,最后一个维度是连续的
F_CONTIGUOUS:是否为 F 风格连续,也就是列为主,第一个维度是连续的
OWNDATA:是否拥有数据,视图不拥有数据
WRITEABLE:是否可写
ALIGNED:是否对齐
UPDATEIFCOPY:
'''
c_array = np.random.rand(10000, 10000)
f_array = np.asfortranarray(c_array)
def sum_row(x):
'''
计算第零行的和
'''
return np.sum(x[0, :])
def sum_col(x):
'''
计算第零列的和
'''
return np.sum(x[:, 0])
'''
我们可以看到,C 风格数组按行访问比较快
F 风格数组按列访问比较快
%timeit sum_row(c_array)
10000 loops, best of 3: 21.2 µs per loop
%timeit sum_row(f_array)
10000 loops, best of 3: 157 µs per loop
%timeit sum_col(c_array)
10000 loops, best of 3: 162 µs per loop
%timeit sum_col(f_array)
10000 loops, best of 3: 21.4 µs per loop
'''
副本和视图
x = np.random.rand(100,10)
y = x[:5, :]
np.may_share_memory(x, y)
y[:] = 0
print(x[:5, :])
'''
[[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]
'''
x = np.random.rand(100,10)
y = np.empty([5, 10])
y[:] = x[:5, :]
np.may_share_memory(x, y)
y[:] = 0
print(x[:5, :])
数组创建
x = np.array([1, 2, 3])
y = np.array(['hello', 'world'])
x = range(5)
y = np.array(x)
x = np.arange(5)
x = np.array([[1, 2, 3],[4, 5, 6]])
x.ndim
x.shape
x = np.random.rand(2, 2, 2)
print(x.shape)
shape_tuple = (2, 3, 4)
y = np.random.random(shape_tuple)
print(y.shape)
LOW, HIGH = 1, 11
SIZE = 10
x = np.random.randint(LOW, HIGH, size=SIZE)
print(x)
[ 6 9 10 7 9 5 8 8 9 3]
数据类型
x = np.random.random((10,10))
x.dtype
x = np.array(range(10))
x.dtype
x = np.array(['hello', 'world'])
x.dtype
x = np.ones((10, 10), dtype=np.int)
x.dtype
x = np.zeros((10, 10), dtype='|S1')
x.dtype