您的位置:首页 > 编程语言 > Python开发

numpy基础篇-简单入门教程1

2019-02-23 22:58 417 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_42419002/article/details/88859695

numpy基础篇-简单入门教程1

np.split(A, 4, axis=1),np.hsplit(A, 4) 分割

[code]A = np.arange(12).reshape((3, 4))    # 水平方向的长度是4

print(np.split(A, 4, axis=1))        # 参数必须是可均分的,vertically均分成四块,分垂直风向,相当于竖着切举证
print(np.array_split(A, 3, axis=1))  # 可以不必是均分的 2+1+1
print(np.vsplit(A, 3))               # vertical 分垂直方向
print(np.hsplit(A, 4))               # 分水平方向

a.copy() 深度复制

[code]a = np.arange(4)
b = a          # a, b, c指向同样的地址内容
c = b
print(c is a)  # True

b = a.copy()   # b是一个新的存储区域
print(b is a)  # False

1D one-dimensional

[code]a = np.arange(0, 8, 2)
print(a)        # [0 2 4 6]
print(a.shape)  # (4,)
print(a[3])     # 6

2D two-dimensional

  • 方括号的深度为2
  • 访问具体数据的两种方法效果相同
[code]b = np.arange(6).reshape(2, 3)
print(b)        # [[0 1 2]  [3 4 5]]
print(b.shape)  # (2, 3)

print(b[1][0])  # 3
print(b[1, 0])  # 3

3D three-dimensional

  • 方括号的深度为3
  • 访问具体数据的两种方法效果相同
[code]c = np.arange(24).reshape(2, 3, 4)
print(c)
> [[[ 0  1  2  3]
>   [ 4  5  6  7]
>   [ 8  9 10 11]]
>  [[12 13 14 15]
>   [16 17 18 19]
>   [20 21 22 23]]]

print(c.shape)     # (2, 3, 4)
print(c[0, 1, 2])  # 6
print(c[0][1][2])  # 6

构造数组

  • np.ones((2, 3)), np.zeros((3, 4))
[code]
print(np.ones(2))
print(np.ones((2)))
print(np.ones((2,)))         # 一维时括号和逗号都可以省略

print(np.ones((2, 3)))
print(np.ones((2, 3, 4)))    # 全1
  • np.full((2, 2), 3)
[code]print(np.full((2, 2), 3))    # 全3矩阵,[[3 3]  [3 3]]
  • np.eye(2)
[code]print(np.eye(2))
print(np.eye((2)))     # 单位矩阵,只有对角非零且全为1的方正矩阵,括号可以省略
  • np.empty((2,3))
[code]print(np.empty((2,3)))
print(np.empty_like(c))  # 空矩阵的内容由缓存状态决定
  • np.linespace(0, 10, num=5)
[code]print(np.linspace(0, 10, num=5))  # [ 0.   2.5  5.   7.5 10. ]
  • np.random.random((2, 4, 5))
[code]my_random_array = np.random.random(5)
my_random_array = np.random.random((5))
my_random_array = np.random.random((5,))  # 一维时括号和逗号都可以省略

print(my_random_array)  # [0.26326548 0.92779013 0.81319706 0.62325733 0.61494552]

my_random_array = np.random.random((1, 2))
print(my_random_array)  # [[0.73278507 0.12801832]]

维度对比 type() 和 np.shape()

  • print() python数组与numpy数组的输出形式不同,前者有逗号,后者没有逗号
[code]print([[0, 1], [2, 3]][0][0])  # 0
print([[0, 1], [2, 3]][0])     # [0, 1]
print([[0, 1], [2, 3]])        # [[0, 1], [2, 3]]

print(np.array([[0, 1], [2, 3]][0][0]))  # 0
print(np.array([[0, 1], [2, 3]][0]))     # [0 1]
print(np.array([[0, 1], [2, 3]]))        # [[0 1] [2 3]]
  • type() python数组与numpy数组的类型不同
[code]print(type([[0, 1, 2], [3, 4, 5]][0][0]))  # <class 'int'>
print(type([[0, 1, 2], [3, 4, 5]][0]))     # <class 'list'>
print(type([[0, 1, 2], [3, 4, 5]]))        # <class 'list'>

print(type(np.array([[0, 1, 2], [3, 4, 5]])[0][0]))  # <class 'numpy.int64'>
print(type(np.array([[0, 1, 2], [3, 4, 5]])[0]))     # <class 'numpy.ndarray'>
print(type(np.array([[0, 1, 2], [3, 4, 5]])))        # <class 'numpy.ndarray'>
  • np.shape()
[code]print(np.shape(np.array([[0, 1, 2], [3, 4, 5]])[0][0]))   # ()
print(np.shape(np.array([[0, 1, 2], [3, 4, 5]])[0]))      # (3,)
print(np.shape(np.array([[0, 1, 2], [3, 4, 5]])))         # (2, 3)

print(np.random.random((2, 2)))  # [[0.09242512 0.31837721] [0.13707168 0.31265585]]

slice 切片

[code]my_array = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0]])

print(my_array)        # [[1 2 3 4 5] [6 7 8 9 0]]
print(my_array.shape)  # (2, 5)

print(my_array[0])     # [1 2 3 4 5]
print(my_array[1])     # [6 7 8 9 0]
print(my_array[0][3])  # 4

print(my_array[0, :])  # [1 2 3 4 5]   上面的方法和这种表示方法效果相同
print(my_array[1, :])  # [6 7 8 9 0]
print(my_array[0, 3])  # 4
  • 注意:
      冒号和逗号在一起时起作用,单独的 [ : ] 需要忽略,不影响输出结果的判断
    1. 以后使用上面这种只用一对方括号的表示方法 [ , ]
    2. :可以用 ... 替代,一般使用 :
[code]print(my_array[:][0])  # [1 2 3 4 5]   [:]不起作用,需要忽略
print(my_array[:][1])  # [6 7 8 9 0]
print(my_array[0][:])  # [1 2 3 4 5]   [:]不起作用,需要忽略
print(my_array[1][:])  # [6 7 8 9 0]
  • 下面三种表示方法的的输出结果相同
[code]my_array = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [11, 12, 14, 14, 15]])

print(my_array[0])
print(my_array[1])

print(my_array[0, ])
print(my_array[1, ])

print(my_array[0, ...])    # [1 2 3 4 5]
print(my_array[1, ...])    # [6 7 8 9 0]
  • 下面三种表示方法的的输出结果相同
[code]my_array = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 0], [11, 12, 14, 14, 15]])
print(my_array[[0], ])
print(my_array[[1], ])

print(my_array[[0], :])
print(my_array[[1], :])

print(my_array[[0], ...])    # [[1 2 3 4 5]]
print(my_array[[1], ...])    # [[6 7 8 9 0]]
[code]print(my_array[0, 0])            # 1
print(my_array[[0], [0]])        # [1]
print(my_array[[0], [0, 2]])     # [1 3]
print(my_array[[0, 1], [2, 0]])  # [3 6]

+ - * / .dot()

[code]a = np.array([[1.0, 2.0], [3.0, 4.0]])
b = np.array([[5.0, 6.0], [7.0, 8.0]])

sum = a + b          # [[ 6.  8.] [10. 12.]]  对应相加
difference = a - b   # [[-4. -4.] [-4. -4.]]
product = a * b      # [[ 5. 12.] [21. 32.]]  对应相乘
quotient = a / b     # [[0.2        0.33333333] [0.42857143 0.5       ]]
matrix_product = a.dot(b)  # [[19. 22.] [43. 50.]]  矩阵的乘法

1D Array 一位数组

  • 数组表示时,圆括号和方括号的效果相同,一般是用方括号,数据方括号之间都用逗号间隔开
[code]a = np.array([0, 1, 2, 3, 4])     # [0 1 2 3 4]
b = np.array((0, 1, 2, 3, 4))     #  [0 1 2 3 4]

c = np.arange(5)                  # [0 1 2 3 4]
d = np.linspace(0, 2*np.pi, 5)    #  [0. 1.57079633 3.14159265 4.71238898 6.28318531]

MD Array M维数组

[code]print(np.arange(11, 36).reshape(5, 5))
a = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])

print(a)  # [[11 12 13 14 15]
[16 17 18 19 20]
[21 22 23 24 25]
[26 27 28 29 30]
[31 32 33 34 35]]

print(a[2, 4])       # 25
print(a[0, 1:4])     # [12 13 14]
print(a[1:4, 0])     # [16 21 26]
print(a[::2, ::2])   # [[11 13 15]
[21 23 25]
[31 33 35]]

print(a[:, 1])       # [12 17 22 27 32]

Array properties 数组属性

  • .dtype .size .shape .itemsize .ndim .nbytes
[code]a = np.array([[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20],
[21, 22, 23, 24, 25],
[26, 27, 28, 29, 30],
[31, 32, 33, 34, 35]])
print(type(a))     # <class 'numpy.ndarray'>  类型
print(a.dtype)     # int64  一个元素64位
print(a.size)      # 25  一共25个元素
print(a.shape)     # (5, 5)
print(a.itemsize)  # 8  一个元素大小为8个字节
print(a.ndim)      # 2  2维数组
print(a.nbytes)    # 200  数组大小为200字节=25个*8字节

Basic Operators 基本操作符

  • np.arange(4) .reshape(2, 2)
[code]a = np.arange(4)             # [0 1 2 3]
a = a.reshape(2, 2)          # [[0 1] [2 3]]
b = np.array([3, 2, 1, 0])   # [3 2 1 0]
b = b.reshape((2, 2))        # [[3 2] [1 0]]
print(b+1)                   # [[4 3] [2 1]]
print(a + b)                 # [[3 3] [3 3]]
print(a - b)                 # [[-3 -1][ 1  3]]
print(a * b)                 # [[0 2] [2 0]]
print(a / (b+1))             # [[0. 0.33333333] [1. 3.]]
print(a ** 2)                # [[0 1] [4 9]]
print(a <= b)                # [[ True  True] [False False]]
print(a.dot(b))              # [[1 0] [9 4]]

Special operator 特殊运算符

  • .sum() .min() .max() .cumsum()
[code]a = np.arange(10)
print(a)            # [0 1 2 3 4 5 6 7 8 9]
print(a.sum())      # 45
print(a.min())      # 0
print(a.max())      # 9
print(a.cumsum())   # [ 0  1  3  6 10 15 21 28 36 45]

Fancy indexing 花式索引

[code]a = np.arange(0, 100, 10)
indices = [1, 2, 5, -1]
b = a[indices]
print(a)       # [ 0 10 20 30 40 50 60 70 80 90]
print(b)       # [10 20 50 90]

Boolean masking 布尔屏蔽

  • 代码:
[code]import matplotlib.pyplot as plt
import numpy as np

a = np.linspace(0, 2 * np.pi, 11)
b = np.sin(a)
plt.plot(a, b)
mask = b >= 0

print(mask)       # [ True  True .....  False False ]
print(a[mask])    # [0.  0.12822827 ...... 2.94925025 3.07747852]
print(b[mask])    # [0.  0.12787716 ...... 0.19115863 0.06407022]

plt.plot(a[mask], b[mask], 'bo')
mask = (b >= 0) & (a <= np.pi / 2)
plt.plot(a[mask], b[mask], 'go')

plt.show()
  • 显示输出:

Incomplete Indexing 不完整的索引

[code]a = np.arange(0, 100, 10)  # [ 0 10 20 30 40 50 60 70 80 90]
b = a[:5]                  # [ 0 10 20 30 40]
c = a[a >= 50]             # [50 60 70 80 90]

np.where(arr < 50)函数

[code]a = np.arange(0, 100, 10)
b = np.where(a < 50)
c = np.where(a < 50)[0]

print(a)        # [ 0 10 20 30 40 50 60 70 80 90]
print(b)        # (array([0, 1, 2, 3, 4]),)
print(b[0])     # [0 1 2 3 4]
print(c)        # [0 1 2 3 4]

END

posted @ 2019-02-23 22:58 YangZhaonan 阅读(...) 评论(...) 编辑 收藏

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: