参考资料

创建

#!/usr/bin/env python3
# -*- coding:utf-8 -*-


import numpy as np
import pandas as pd

# Numpy属性

# 输出矩阵

# 0维
a = np.array(3)

# 1维必须加[]
a = np.array([3, 15])

# 2维
array = np.array([[1, 2, 3], [1, 2, 3]])
print("输出数组")
print(array)

# 输出维数ndim
print("输出维数", array.ndim)

# 输出大小shap,行列值
print("矩阵的行列值:", array.shape)

# 输出矩阵的元素个数size
print("输出矩阵的元素个数:", array.size)

# Numpy关键字

# array 创建数组
array = np.array([1, 2, 3])
print("创建数组:", array)

# dtype 指定数据类型

array = np.array([1, 2, 3], dtype=np.int64)
print(array, "数组元素数据类型为", array.dtype)

array = np.array([1, 2, 3], dtype=np.int32)
print(array, "数组元素数据类型为", array.dtype)

array = np.array([1, 2, 3], dtype=np.float64)
print(array, "数组元素数据类型为", array.dtype)

array = np.array([1, 2, 3], dtype=np.float32)
print(array, "数组元素数据类型为", array.dtype)

# 创建特定数据

# 一般数据创建
a = np.array([[2, 3, 4], [5, 6, 7]])
print(a)

# 3行4列0
a = np.zeros((3, 4))
print(a)

# 3行4列1
a = np.ones((3, 4))
print(a)

# 全空数组
b = np.empty((3, 4)) # 数据为empty,3行4列
print(b)

# 查看注释
# print((help(np.empty)))

# 创建连续数组,10到20步长为2的连续数组
a = np.arange(10, 20, 2)
print(a)

# 使用reshape改变数据的形状
a = np.arange(12).reshape((3, 4))
print(a)

# 使用linspace创建线段型数据
a = np.linspace(1, 10, 20) # 开始端1,结束端10,且分割成20个数据,生成线段
print(a)

a = np.linspace(1, 10, 20).reshape(3, 4)
print(a)

基础运算1

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import numpy as np
import pandas as pd

# Numpy运算
a = np.array([10, 20, 30, 40])
b = np.arange(1, 5)
print(b) # [1 2 3 4]
print(a - b) # [ 9 18 27 36]
print(a + b) # [11 22 33 44]
print(a * b) # [ 10 40 90 160]
print(a / b) # [10. 10. 10. 10.]
print(b ** 2) # [ 1 4 9 16]
print(10 * np.sin(b)) # [ 8.41470985 9.09297427 1.41120008 -7.56802495]
print(10 * np.cos(b)) # [ 5.40302306 -4.16146837 -9.89992497 -6.53643621]
print(b < 4) # [ True True True False]

# 矩阵运算
a = np.array([[1, 1], [0, 1]])
b = np.arange(4).reshape((2, 2)) # 将数组定义为二行二列的矩阵
print(a)
# [[1 1]
# [0 1]]
print(b)
# [[0 1]
# [2 3]]
print(a * b) # 矩阵普通乘法
# [[0 1]
# [2 3]]
print(np.dot(a, b)) # 矩阵算数乘法
# [[2 4]
# [2 3]]
print(a.dot(b)) # 与上式相同
# [[2 4]
# [2 3]]

# 数组随机生成

a = np.random.random((2, 4)) # 2x4的 0-1之间的随机数
print(a)
print(np.sum(a), np.min(a), np.max(a))
# [[0.70495658 0.32237221 0.83518711 0.48553422]
# [0.2691227 0.59185169 0.59137848 0.30830106]]
# 4.108704042882392 0.26912269581385617 0.8351871053571626

# 行列操作

# 对行取最大值、对行取最小、取行最大值
# axis = 1 表示对行求和 0表示对列求和
print(a)
# [[0.22579687 0.89965822 0.14778894 0.86993282]
# [0.57134816 0.1807615 0.4241878 0.33533019]]
print("求和", np.sum(a, axis=0))
# [0.79714504 1.08041971 0.57197674 1.20526301]
print(np.min(a, axis=0)) # 对列求最小
# [0.22579687 0.1807615 0.14778894 0.33533019]
print(np.max(a, axis=0)) # 对列求最大
# [0.57134816 0.89965822 0.4241878 0.86993282]

基本运算2

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import numpy as np
import pandas as pd

# Numpy运算
a = np.arange(2, 14).reshape(3, 4)
print(a)
# [[ 2 3 4 5]
# [ 6 7 8 9]
# [10 11 12 13]]

# 输出最小值索引
print(np.argmin(a))
# 0
# 输出最大值索引
print(np.argmax(a))
# 11

# 输出平均值
print(np.average(a))
# 7.5
# 输出中位数
print(np.median(a))
# 7.5

# 当前位的前项和矩阵
print(np.cumsum(a).reshape(3, 4))
# [ 2 5 9 14 20 27 35 44 54 65 77 90]
# reshape后
# [[ 2 5 9 14]
# [20 27 35 44]
# [54 65 77 90]]

# 返回非零数值的索引,下方是矩阵的形式
print(np.nonzero(a))
# (array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))
# print(help(np.nonzero))

# 生成按行递减矩阵
a = np.arange(14, 2, -1).reshape(3, 4)
print(a)
# [[14 13 12 11]
# [10 9 8 7]
# [ 6 5 4 3]]
# 按行排序
print(np.sort(a)) # 此处没有改变a
# [[11 12 13 14]
# [ 7 8 9 10]
# [ 3 4 5 6]]

print(a)
# [[14 13 12 11]
# [10 9 8 7]
# [ 6 5 4 3]]
# 矩阵的转置
# 将矩阵的行列互换得到的新矩阵称为转置矩阵
print(np.transpose(a))
# [[14 10 6]
# [13 9 5]
# [12 8 4]
# [11 7 3]]
print(a.T) # 上式的简写
# [[14 10 6]
# [13 9 5]
# [12 8 4]
# [11 7 3]]

print((a.T).dot(a))
# [[332 302 272 242]
# [302 275 248 221]
# [272 248 224 200]
# [242 221 200 179]]

# 将小于5的数赋值为5,大于9的数赋值为9
print(np.clip(a, 5, 9))
# 对列求平均值
print(np.mean(a, axis=0))

索引

a = np.arange(0, 25).reshape(5, 5)
print(a)
# [[ 0 1 2 3 4]
# [ 5 6 7 8 9]
# [10 11 12 13 14]
# [15 16 17 18 19]
# [20 21 22 23 24]]

# 通过索引获取部分数据
print(a[2])
# [10 11 12 13 14]
print(a[2][0])
# 或者
print(a[2, 0])
# 10

# 输出所有数
print(a[2, :])
# 第三行 [10 11 12 13 14]
print(a[:, 0])
# 第一列 [ 0 5 10 15 20]

# 使用for循环输出行
for row in a:
print(row)
# [0 1 2 3 4]
# [5 6 7 8 9]
# [10 11 12 13 14]
# [15 16 17 18 19]
# [20 21 22 23 24]


# 输出列,通过对转置矩阵求
for column in a.T:
print(column)
# [ 0 5 10 15 20]
# [ 1 6 11 16 21]
# [ 2 7 12 17 22]
# [ 3 8 13 18 23]
# [ 4 9 14 19 24]


# 迭代输出每一个项目
print(a.flatten())
# [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24]
for item in a.flat:
print(item)
# 0
# 1
# 2
# 3
# 4
# 5
# ...

拼接

import numpy as np
import pandas as pd

# 拼接
A = np.array([1, 1, 1])
B = np.array([2, 2, 2])
C = np.vstack((A, B)) # vertical stack
D = np.hstack((A, B)) # horizontal stack

# vertical stack 垂直拼接
print(C)
# [[1 1 1]
# [2 2 2]]
print(C.shape)
# (2, 3)

# horizontal stack 水平拼接
print(D)
# [1 1 1 2 2 2]
print(D.shape)
# (6,)

# 数列变矩阵
print(A.T)
# [1 1 1]

# 加维度
print(A[np.newaxis, :])
# [[1 1 1]]
print(A[:, np.newaxis])
# [[1]
# [1]
# [1]]

# 数列拼接
print(np.column_stack([A, B]))
# [[1 2]
# [1 2]
# [1 2]]

# 水平拼接
print(np.row_stack([A, B]))
# [[1 1 1]
# [2 2 2]]



# 则可以有
M = A[:, np.newaxis]
N = B[:, np.newaxis]
print(np.hstack((M, N))) # 注意是两个括号,表示2维
# [[1 2]
# [1 2]
# [1 2]]

# 沿现有轴联接一系列数组
Q = np.concatenate((A, A, A, A), axis=0)
print(Q)

a = np.array([
[1,2],
[3,4]
])
b = np.array([
[5,6],
[7,8]
])
# 对于任意拼接,0竖拼接,1横拼接
print(np.concatenate([a, b], axis=0))
# [[1 2]
# [3 4]
# [5 6]
# [7 8]]
print(np.concatenate([a, b], axis=1))
# [[1 2 5 6]
# [3 4 7 8]]

拆解

#!/usr/bin/env python3
# -*- coding:utf-8 -*-
import numpy as np
import pandas as pd

# 拆解
A = np.arange(12).reshape((3, 4))
print(A)
# [[ 0 1 2 3]
# [ 4 5 6 7]
# [ 8 9 10 11]]

print(np.split(A, 3, axis=0))
# [array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]

print(np.split(A, 4, axis=1))
# [array([[0],
# [4],
# [8]]),
# array([[1],
# [5],
# [9]]),
# array([[ 2],
# [ 6],
# [10]]),
# array([[ 3],
# [ 7],
# [11]])]
# 但是上述的分割必须是等分的分割

# 不等分分割
print(np.array_split(A,3,axis=1))
# [array([[0, 1],
# [4, 5],
# [8, 9]]), array([[ 2],
# [ 6],
# [10]]), array([[ 3],
# [ 7],
# [11]])]
print(np.vsplit(A,3))
# [array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]

print(np.hsplit(A,2))
# [array([[0, 1],
# [4, 5],
# [8, 9]]), array([[ 2, 3],
# [ 6, 7],
# [10, 11]])]

a = np.array([[1, 11, 2, 22],
[3, 33, 4, 44],
[5, 55, 6, 66],
[7, 77, 8, 88]])

# 分成两段
print(np.vsplit(a, indices_or_sections=2)) # 分成两段
# [array([[ 1, 11, 2, 22],
# [ 3, 33, 4, 44]]), array([[ 5, 55, 6, 66],
# [ 7, 77, 8, 88]])]

print(np.vsplit(a, indices_or_sections=[2, 3])) # 分片成 [:2],[2:3], [3:]
# [array([[ 1, 11, 2, 22],
# [ 3, 33, 4, 44]]), array([[ 5, 55, 6, 66]]), array([[ 7, 77, 8, 88]])]


# 既横着切也竖着切, axis 表示是维数,可以理解为数组的元素层数,
# 0维表示数组最表层的内容既 [1,2]
# 1维表示数组表层元素重的维数[[1,2],[3,4]] 此时1,2元素的axis= 1,数组[1,2] axis = 0
print(np.split(a, indices_or_sections=2, axis=1))
print(np.split(a, indices_or_sections=2, axis=0))

赋值

# 赋值

a = np.arange(4)
print(a)
# [0 1 2 3]
b = a
print(b)
# [0 1 2 3]
a[0] = 21
print(a)
# [21 1 2 3]
print(b)
# [21 1 2 3]
print(b is a)
# True
b[1:4] = [0,0,0]
print(b)
# [21 0 0 0]
print(a)
# [21 0 0 0]

# 深度拷贝 ,通常情况的下的赋值是多指针指向相同对象
c = a.copy()
a[0] = 0
print(a)
# [0 0 0 0]
print(c)
# [21 0 0 0]

linalg多项式应用

ployder求导

ployint求积分

Numpy文件读写

loadtxt