Numpy
开源的python科学计算库,主要功能是用来操作数组和矩阵
- Numpy
- ndim
- 维度
- shape
- 行数,列数
- size
- 元素个数
- ndim
更像是一个网格,使其内所有元素保持相同的类型
正是因为所有元素都保持了相同的类型,所有numpy可以进行向量运算,会提高很多
速度
-
优势
- 速度更快,并且快很多
- 集成了许多数组的计算函数
-
创建numpy数组
- 用numpy数组函数,借助Python的list
- Numpy的内置函数,快速生成数组
-
有用的属性
- dtype
- 元素的数据类型
- shape
- n个正整数组成的元组,说明每个维度的大小
- dtype
-
当元素数组类型不同时:
- 其他与string
- 全部转换为string
- int float
- 全部转换为float64
- 指定dtype
- 会全部转换为你指定的类型
- 其他与string
-
Numpy将x保存于文档中
- Numpy如何高效处理数据
- 可变:数组创建完毕后还可以改变其中数据
- Numpy
- 访问
- 同样支持正数和负数
- 访问
x = np.array(np.arange(1,6,1),dtype=int32)
print(x)
print("1st elem: ",x[0])
-
删除和添加
元素-
np.delete(arr_name,[删除的位置(一或多个)],axis = 0,1)
- 0是行,1是列
-
np.append(arr_name,[‘要插入的内容,二维以上应该是一个数组’],axis = 0,1)
- 同上
-
np.insert(arr_name,位置,[‘要插入的内容’],axis)
扫描二维码关注公众号,回复: 6123917 查看本文章
-
# We create a rank 1 ndarray
x = np.array([1, 2, 3, 4, 5])
# We create a rank 2 ndarray
Y = np.array([[1,2,3],[4,5,6],[7,8,9]])
# We print x
print()
print('Original x = ', x)
# We delete the first and last element of x
x = np.delete(x, [0,4])
# We print x with the first and last element deleted
print()
print('Modified x = ', x)
# We print Y
print()
print('Original Y = \n', Y)
# We delete the first row of y
w = np.delete(Y, 0, axis=0)
# We delete the first and last column of y
v = np.delete(Y, [0,2], axis=1)
# We print w
print()
print('w = \n', w)
# We print v
print()
print('v = \n', v)
# We create a rank 1 ndarray
x = np.array([1, 2, 3, 4, 5])
# We create a rank 2 ndarray
Y = np.array([[1,2,3],[4,5,6]])
# We print x
print()
print('Original x = ', x)
# We append the integer 6 to x
x = np.append(x, 6)
# We print x
print()
print('x = ', x)
# We append the integer 7 and 8 to x
x = np.append(x, [7,8])
# We print x
print()
print('x = ', x)
# We print Y
print()
print('Original Y = \n', Y)
# We append a new row containing 7,8,9 to y
v = np.append(Y, [[7,8,9]], axis=0)
# We append a new column containing 9 and 10 to y
q = np.append(Y,[[9],[10]], axis=1)
# We print v
print()
print('v = \n', v)
# We print q
print()
print('q = \n', q)
注意append [[]] 是两个中括号
- numpy
堆叠
- np.vstack
- 垂直方向堆叠
- np.hstack
- 水平方向堆叠
- np.vstack
# 水平方向堆叠
x = np.array([1,2])
y = np.array([[3,4],[5,6]])
x = np.hstack((x.reshape(2,1),y))
print(x)
[[1 3 4]
[2 5 6]]
x = np.array([1,2])
y = np.array([[3,4],[5,6]])
x = np.vstack((x,y))
print(x)
[[1 2]
[3 4]
[5 6]]
- Numpy 切片
- 访问数组的子数组
- np.ndarray[start:end]
- np.ndarray[start:]
- np.ndarray[:end]
- !!左闭右开区间
- start可以是序列,表示选取哪些行
- end可以是序列,表示选取哪些列
- 访问数组的子数组
# We create a 4 x 5 ndarray that contains integers from 0 to 19
X = np.arange(20).reshape(4, 5)
# We print X
print()
print('X = \n', X)
print()
# 选择x数组当中1,2,3行中2-4列的所有元素组成一个新的数组
Z = X[1:4,2:5]
# We print Z
print('Z = \n', Z)
# We can select the same elements as above using method 2
W = X[1:,2:5]
# We print W
print()
print('W = \n', W)
# We select all the elements that are in the 1st through 3rd rows and in the 3rd to 4th columns
Y = X[:3,2:5]
# We print Y
print()
print('Y = \n', Y)
# We select all the elements in the 3rd row
v = X[2,:]
# We print v
print()
print('v = ', v)
# We select all the elements in the 3rd column
q = X[:,2]
# We print q
print()
print('q = ', q)
# We select all the elements in the 3rd column but return a rank 2 ndarray
R = X[:,2:3]
# We print R
print()
print('R = \n', R)
- 当对列进行
切片
时- 切片只是原数组的新视角,并没有复制到一个新的var_name中
下面返回的是一个
秩为2
的数组,即列数组
# We create a 4 x 5 ndarray that contains integers from 0 to 19
X = np.arange(20).reshape(4, 5)
print(X)
y = X[:,2:3]
print(y)
下面返回的是一个
秩为1
的数组,既行数组
# We create a 4 x 5 ndarray that contains integers from 0 to 19
X = np.arange(20).reshape(4, 5)
print(X)
y = X[:,2]
print(y)
- 如何生成一个
新的数组
?- arr_name[1:,2:].copy()
x = X[1:,2:].copy()
- 一些内置的
特殊的选择
函数- np.diag(X)
x = np.diag(X)# 秩为1
- 还可以输出在主对角线以外的元素
上方
x = np.diag(X,k=1)
下方
x = np.diag(X,k=-1)
- 非重复数组
X = np.array([[1,2,3],[5,2,7],[1,2,3]])
x = np.unique(X)
print(x)
[1 2 3 5 8]
- 并不知道具体的索引怎么办呢?
- 利用布尔表达式进行索引
X = np.arange(25).reshape(5,5)
print(X[X > 10])
print(X>7 and X < 17)
[11 12 13 14 15 16 17 18 19 20 21 22 23 24]
- 交,差,并
- 交:np.intersect1d(X,Y)
- 差:np.setdiff1d(X,Y)
- 并:np.union1d(X,Y)
X = np.random.randint(0,10,8)
Y = np.random.randint(0,10,8)
print("X = {}".format(X))
print("Y = {}".format(Y))
print(np.intersect1d(X,Y))
print(np.setdiff1d(X,Y))
print(np.union1d(X,Y))
X = [0 2 6 6 8 3 9 8]
Y = [9 8 6 9 5 0 3 1]
[0 3 6 8 9]
[2]
[0 1 2 3 5 6 8 9]
- sort
- 如果把sort()当函数使用
- 不会改变原数组
- 如果把sort()当方法用
- 会改变原数组
- 如果把sort()当函数使用
X = np.random.randint(0,10,8)
Y = np.random.randint(0,10,8)
print("X = {}".format(X))
print("Y = {}".format(Y))
print("sorted as function : {} origin : {}".format(np.sort(X),X))
print("sorted as method : {} origin : {}".format(Y.sort(),Y))
X = [4 5 3 6 6 6 7 9]
Y = [4 7 9 9 4 1 3 0]
sorted as function : [3 4 5 6 6 6 7 9] origin : [4 5 3 6 6 6 7 9]
sorted as method : None origin : [0 1 3 4 4 7 9 9]
- 秩为2的数组排序
- 需要制定axis
# We create an unsorted rank 2 ndarray
X = np.random.randint(1,11,size=(5,5))
# We print X
print()
print('Original X = \n', X)
print()
# 按列排序
print()
print('X with sorted columns :\n', np.sort(X, axis = 0))
# 按行排序
print()
print('X with sorted rows :\n', np.sort(X, axis = 1))
# We create an unsorted rank 2 ndarray
X = np.random.randint(1,11,size=(5,5))
# We print X
print()
print('Original X = \n', X)
print()
# We sort the columns of X and print the sorted array
print()
print('X with sorted columns :\n', np.sort(X, axis = 0))
# We sort the rows of X and print the sorted array
print()
print('X with sorted rows :\n', np.sort(X, axis = 1))
numpy 算数运算
- +,-,*,/
- 通过广播机制
- 小数组赋值扩大成大数组以实现兼容
- *乘法是
对应元素乘法
线性代数
乘法需要用点乘- arr_name.dot(arr_name)
# We create two rank 1 ndarrays
x = np.array([1,2,3,4])
y = np.array([5.5,6.5,7.5,8.5])
print("x = {} \ny = {}".format(x,y))
print('x + y = ', x + y)
print('add(x,y) = ', np.add(x,y))
print()
print('x - y = ', x - y)
print('subtract(x,y) = ', np.subtract(x,y))
print()
print('x * y = ', x * y)
print('multiply(x,y) = ', np.multiply(x,y))
print()
print('x / y = ', x / y)
print('divide(x,y) = ', np.divide(x,y))
x = [1 2 3 4]
y = [5.5 6.5 7.5 8.5]
x + y = [ 6.5 8.5 10.5 12.5]
add(x,y) = [ 6.5 8.5 10.5 12.5]
x - y = [-4.5 -4.5 -4.5 -4.5]
subtract(x,y) = [-4.5 -4.5 -4.5 -4.5]
x * y = [ 5.5 13. 22.5 34. ]
multiply(x,y) = [ 5.5 13. 22.5 34. ]
x / y = [0.18181818 0.30769231 0.4 0.47058824]
divide(x,y) = [0.18181818 0.30769231 0.4 0.47058824]
- 多维数组
# We create two rank 2 ndarrays
X = np.array([1,2,3,4]).reshape(2,2)
Y = np.array([5.5,6.5,7.5,8.5]).reshape(2,2)
# We print X
print()
print('X = \n', X)
# We print Y
print()
print('Y = \n', Y)
print()
# We perform basic element-wise operations using arithmetic symbols and functions
print('X + Y = \n', X + Y)
print()
print('add(X,Y) = \n', np.add(X,Y))
print()
print('X - Y = \n', X - Y)
print()
print('subtract(X,Y) = \n', np.subtract(X,Y))
print()
print('X * Y = \n', X * Y)
print()
print('multiply(X,Y) = \n', np.multiply(X,Y))
print()
print('X / Y = \n', X / Y)
print()
print('divide(X,Y) = \n', np.divide(X,Y))
X =
[[1 2]
[3 4]]
Y =
[[ 5.5 6.5]
[ 7.5 8.5]]
X + Y =
[[ 6.5 8.5]
[ 10.5 12.5]]
add(X,Y) =
[[ 6.5 8.5]
[ 10.5 12.5]]
X - Y =
[[-4.5 -4.5]
[-4.5 -4.5]]
subtract(X,Y) =
[[-4.5 -4.5]
[-4.5 -4.5]]
X * Y =
[[ 5.5 13. ]
[ 22.5 34. ]]
multiply(X,Y) =
[[ 5.5 13. ]
[ 22.5 34. ]]
X / Y =
[[ 0.18181818 0.30769231]
[ 0.4 0.47058824]]
divide(X,Y) =
[[ 0.18181818 0.30769231]
[ 0.4 0.47058824]]
- 其他的一些
数学
运算
# We create a rank 1 ndarray
x = np.array([1,2,3,4])
# We print x
print()
print('x = ', x)
# We apply different mathematical functions to all elements of x
print()
# 求指数函数值
print('EXP(x) =', np.exp(x))
print()
# 求每个元素的平方根
print('SQRT(x) =',np.sqrt(x))
print()
# 求每个元素的平方
print('POW(x,2) =',np.power(x,2)) # We raise all elements to the power of 2
x = [1 2 3 4]
EXP(x) = [ 2.71828183 7.3890561 20.08553692 54.59815003]
SQRT(x) = [ 1. 1.41421356 1.73205081 2. ]
POW(x,2) = [ 1 4 9 16]
- np的一些
统计
函数
# We create a 2 x 2 ndarray
X = np.array([[1,2], [3,4]])
# We print x
print()
print('X = \n', X)
print()
# 平均值
print('Average of all elements in X:', X.mean())
# 按列求平均值
print('Average of all elements in the columns of X:', X.mean(axis=0))
# 按行求平均值
print('Average of all elements in the rows of X:', X.mean(axis=1))
print()
# 求和
print('Sum of all elements in X:', X.sum())
# 按列求和
print('Sum of all elements in the columns of X:', X.sum(axis=0))
# 按行求和
print('Sum of all elements in the rows of X:', X.sum(axis=1))
print()
# 求方差
print('Standard Deviation of all elements in X:', X.std())
# 按列求方差
print('Standard Deviation of all elements in the columns of X:', X.std(axis=0))
# 按行求方差
print('Standard Deviation of all elements in the rows of X:', X.std(axis=1))
print()
# 中位数
print('Median of all elements in X:', np.median(X))
# 按列求中位数
print('Median of all elements in the columns of X:', np.median(X,axis=0))
# 按列求中位数
print('Median of all elements in the rows of X:', np.median(X,axis=1))
print()
# 最大值
print('Maximum value of all elements in X:', X.max())
print('Maximum value of all elements in the columns of X:', X.max(axis=0))
print('Maximum value of all elements in the rows of X:', X.max(axis=1))
print()
# 最小值
print('Minimum value of all elements in X:', X.min())
print('Minimum value of all elements in the columns of X:', X.min(axis=0))
print('Minimum value of all elements in the rows of X:', X.min(axis=1))
- numpy可以在
不用复杂循环
的情况下对数组内元素进行算数运算
x = np.arange(1,5).reshape(2,2)
print("x = \n{}\n".format(x))
print("x + 3 = \n{}\n".format(x+3))
print("x - 3 = \n{}\n".format(x-3))
print("x * 3 = \n{}\n".format(x*3))
print("x / 3 = \n{}\n".format(x/3))
x =
[[1 2]
[3 4]]
x + 3 =
[[4 5]
[6 7]]
x - 3 =
[[-2 -1]
[ 0 1]]
x * 3 =
[[ 3 6]
[ 9 12]]
x / 3 =
[[0.33333333 0.66666667]
[1. 1.33333333]]