python之numpy基础总结

References

1.1. 安装numpy和pandas

1.2. numpy的属性

1.3. numpy创建ndarray

1.4. numpy的基本运算

1.5. numpy的索引/切片

# 一维array
s = np.arange(13) ** 2
print('s: ', s)
print('s[0]: ', s[0])
print('s[4]: ', s[4])
print('s[0:3]: ', s[0:3])
print('s[[0, 2, 4]]: ', s[[0, 2, 4]])

# 二维array
r = np.arange(36).reshape((6, 6))
print('r: \n', r)
print('r[2, 2]: \n', r[2, 2])
print('r[2, 2]: \n', r[2][2])
print('r[2, :]: \n', r[2][:])
print('r[:, 1]: \n', r[:][1])
print('r[3, 3:6]: \n', r[3, 3:6])

# 过滤
print(r > 30)
print(r[r > 30])
# 将大于30的数赋值为30
r[r > 30] = 30
print(r)

1.6. numpy的array合并

1.7. numpy的array分割

1.8. numpy的copy和deep copy

# assignment and copy
import numpy as np
# # # # # #
# example 1
r = np.arange(36).reshape((6, 6))
print('r: \n', r)

r2 = r[:3, :3]
print(r2)

# 将r2内容设置为0
r2[:] = 0
# 查看r的内容
print(r)

r3 = r.copy()
r3[:] = 0
print(r)

# # # # # #
# example 2
a = np.arange(4)
b = np.arange(4)
c = a
d = a.copy()

np.array_equiv(a, b)
np.array_equal(a, b)
print(a is b)
print(c is a)

c[1]= 100
print(c, id(c))
print(a, id(a))

print(d is a, id(a), id(d)))
print(np.array_equal(a,d))

1.9 vectorization 向量化操作

Table of Contents