Python从入门到精通2–numpy总结
import numpy as np
print(np.__version__)
1.19.2
np.zeros(10)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
np.zeros(10,dtype = 'int')
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
np.arange(0,100,5)
array([ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80,
85, 90, 95])
List = [1,2,3,4,5,6,7,8,9]
result = np.array(List)
result
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
#方法1
np.ones((3,3))
#方法2
np.array([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 1., 1., 1.]])
array([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]])
np.full((2,2), True, dtype=bool)
array([[ True, True],
[ True, True]])
备注:从5开始,50结束,共10个数据
np.linspace(start=5,stop=50,num=10)
array([ 5., 10., 15., 20., 25., 30., 35., 40., 45., 50.])
np.arange(start = 5, stop = 55, step = 5,dtype = 'int32')
array([ 5, 10, 15, 20, 25, 30, 35, 40, 45, 50])
备注:矩阵元素均为0—10之间的随机数
np.random.randint(0,10,(3,3))
array([[5, 3, 3],
[2, 0, 4],
[7, 6, 2]])
备注:矩阵元素均为服从标准正态分布的随机数
np.random.randn(3, 3)
array([[ 0.77191714, 1.99485092, 0.36574655],
[-2.35963788, -0.18879237, 1.00267285],
[-1.20021396, -0.87413623, -0.17664144]])
result = result.reshape(3,3)
result.T
array([[1, 4, 7],
[2, 5, 8],
[3, 6, 9]])
result.dtype
#dtype('int64')
dtype('int32')
#方法一:直接查看
print(result.nbytes)
#方法2手动计算
print(result.itemsize * 9)
36
36
result = result.astype(float)
result[2,2]
9.0
result[2,2] = result[2,2] * 10
result[result % 2 == 0]
array([ 2., 4., 6., 8., 90.])
result[result % 2 == 1] = 666
result = np.diag([5,5,5,5,5])
result
array([[5, 0, 0, 0, 0],
[0, 5, 0, 0, 0],
[0, 0, 5, 0, 0],
[0, 0, 0, 5, 0],
[0, 0, 0, 0, 5]])
a = result[:, [1,0,2,3,4]]
b = result[[0,1,2,4,3], :]
print((a == b).all())
False
len(np.argwhere(a != b))
8
np.argwhere(a != b)
array([[0, 0],
[0, 1],
[1, 0],
[1, 1],
[3, 3],
[3, 4],
[4, 3],
[4, 4]], dtype=int64)
np.dot(a,b)
array([[ 0, 25, 0, 0, 0],
[25, 0, 0, 0, 0],
[ 0, 0, 25, 0, 0],
[ 0, 0, 0, 0, 25],
[ 0, 0, 0, 25, 0]])
print(np.multiply(a,b))
print('========方法2========')
print(a * b) #方法2
[[ 0 0 0 0 0]
[ 0 0 0 0 0]
[ 0 0 25 0 0]
[ 0 0 0 0 0]
[ 0 0 0 0 0]]
========方法2========
[[ 0 0 0 0 0]
[ 0 0 0 0 0]
[ 0 0 25 0 0]
[ 0 0 0 0 0]
[ 0 0 0 0 0]]
np.linalg.det(result)
3124.999999999999
np.linalg.inv(result)
array([[0.2, 0. , 0. , 0. , 0. ],
[0. , 0.2, 0. , 0. , 0. ],
[0. , 0. , 0.2, 0. , 0. ],
[0. , 0. , 0. , 0.2, 0. ],
[0. , 0. , 0. , 0. , 0.2]])
a = np.matrix(a)
b = np.matrix(b)
np.multiply(a,b)
matrix([[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 25, 0, 0],
[ 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0]])
a * b
matrix([[ 0, 25, 0, 0, 0],
[25, 0, 0, 0, 0],
[ 0, 0, 25, 0, 0],
[ 0, 0, 0, 0, 25],
[ 0, 0, 0, 25, 0]])
np.hstack((a,b))
matrix([[0, 5, 0, 0, 0, 5, 0, 0, 0, 0],
[5, 0, 0, 0, 0, 0, 5, 0, 0, 0],
[0, 0, 5, 0, 0, 0, 0, 5, 0, 0],
[0, 0, 0, 5, 0, 0, 0, 0, 0, 5],
[0, 0, 0, 0, 5, 0, 0, 0, 5, 0]])
np.vstack((a,b))
matrix([[0, 5, 0, 0, 0],
[5, 0, 0, 0, 0],
[0, 0, 5, 0, 0],
[0, 0, 0, 5, 0],
[0, 0, 0, 0, 5],
[5, 0, 0, 0, 0],
[0, 5, 0, 0, 0],
[0, 0, 5, 0, 0],
[0, 0, 0, 0, 5],
[0, 0, 0, 5, 0]])
new = np.pad(result,pad_width = 1,constant_values=1)
np.argwhere(new > 1)
array([[1, 1],
[2, 2],
[3, 3],
[4, 4],
[5, 5]], dtype=int64)
new[new > 1] = 8
new
array([[1, 1, 1, 1, 1, 1, 1],
[1, 8, 0, 0, 0, 0, 1],
[1, 0, 8, 0, 0, 0, 1],
[1, 0, 0, 8, 0, 0, 1],
[1, 0, 0, 0, 8, 0, 1],
[1, 0, 0, 0, 0, 8, 1],
[1, 1, 1, 1, 1, 1, 1]])
np.sum(new, 0)
array([ 7, 10, 10, 10, 10, 10, 7])
np.sum(new, 1)
array([ 7, 10, 10, 10, 10, 10, 7])
import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
备注:使用numpy生成6行6列的二维数组,值为1-100随机数
data = np.random.randint(1,100, [6,6])
data
array([[64, 81, 4, 42, 31, 32],
[73, 24, 12, 93, 67, 87],
[95, 1, 61, 60, 63, 45],
[56, 22, 75, 50, 63, 38],
[67, 82, 39, 64, 12, 77],
[77, 41, 52, 53, 7, 7]])
np.amax(data, axis=0)
array([95, 82, 75, 93, 67, 87])
np.amin(data, axis=1)
array([ 4, 12, 1, 22, 12, 7])
np.unique(data,return_counts=True)
(array([ 1, 4, 7, 12, 22, 24, 31, 32, 38, 39, 41, 42, 45, 50, 52, 53, 56,
60, 61, 63, 64, 67, 73, 75, 77, 81, 82, 87, 93, 95]),
array([1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2,
1, 1, 2, 1, 1, 1, 1, 1], dtype=int64))
data.argsort()
array([[2, 4, 5, 3, 0, 1],
[2, 1, 4, 0, 5, 3],
[1, 5, 3, 2, 4, 0],
[1, 5, 3, 0, 4, 2],
[4, 2, 3, 0, 5, 1],
[4, 5, 1, 2, 3, 0]], dtype=int64)
np.repeat(data, 2, axis=0)
array([[64, 81, 4, 42, 31, 32],
[64, 81, 4, 42, 31, 32],
[73, 24, 12, 93, 67, 87],
[73, 24, 12, 93, 67, 87],
[95, 1, 61, 60, 63, 45],
[95, 1, 61, 60, 63, 45],
[56, 22, 75, 50, 63, 38],
[56, 22, 75, 50, 63, 38],
[67, 82, 39, 64, 12, 77],
[67, 82, 39, 64, 12, 77],
[77, 41, 52, 53, 7, 7],
[77, 41, 52, 53, 7, 7]])
np.unique(data,axis = 0)
array([[56, 22, 75, 50, 63, 38],
[64, 81, 4, 42, 31, 32],
[67, 82, 39, 64, 12, 77],
[73, 24, 12, 93, 67, 87],
[77, 41, 52, 53, 7, 7],
[95, 1, 61, 60, 63, 45]])
备注:从data的第一行中不放回抽3个元素
np.random.choice(data[0:1][0], 3, replace=False)
array([31, 42, 81])
a = data[1:2]
b = data[2:3]
index=np.isin(a,b)
array=a[~index]
array
array([73, 24, 12, 93, 67, 87])
(~data.any(axis=1)).any()
False
data.sort(axis = 1)
data
array([[ 4, 31, 32, 42, 64, 81],
[12, 24, 67, 73, 87, 93],
[ 1, 45, 60, 61, 63, 95],
[22, 38, 50, 56, 63, 75],
[12, 39, 64, 67, 77, 82],
[ 7, 7, 41, 52, 53, 77]])
data1 = data.astype(float)
data1[data1 < 5] = np.nan
data1
array([[nan, 31., 32., 42., 64., 81.],
[12., 24., 67., 73., 87., 93.],
[nan, 45., 60., 61., 63., 95.],
[22., 38., 50., 56., 63., 75.],
[12., 39., 64., 67., 77., 82.],
[ 7., 7., 41., 52., 53., 77.]])
data1 = data1[~np.isnan(data1).any(axis=1), :]
data1
array([[12., 24., 67., 73., 87., 93.],
[22., 38., 50., 56., 63., 75.],
[12., 39., 64., 67., 77., 82.],
[ 7., 7., 41., 52., 53., 77.]])
vals, counts = np.unique(data1[0,:], return_counts=True)
print(vals[np.argmax(counts)])
12.0
a = 100
data1.flat[np.abs(data1 - a).argmin()]
93.0
data1 - data1.mean(axis=1, keepdims=True)
array([[-47.33333333, -35.33333333, 7.66666667, 13.66666667,
27.66666667, 33.66666667],
[-28.66666667, -12.66666667, -0.66666667, 5.33333333,
12.33333333, 24.33333333],
[-44.83333333, -17.83333333, 7.16666667, 10.16666667,
20.16666667, 25.16666667],
[-32.5 , -32.5 , 1.5 , 12.5 ,
13.5 , 37.5 ]])
a = np.max(data1) - np.min(data1)
(data1 - np.min(data1)) / a
array([[0.05813953, 0.19767442, 0.69767442, 0.76744186, 0.93023256,
1. ],
[0.1744186 , 0.36046512, 0.5 , 0.56976744, 0.65116279,
0.79069767],
[0.05813953, 0.37209302, 0.6627907 , 0.69767442, 0.81395349,
0.87209302],
[0. , 0. , 0.39534884, 0.52325581, 0.53488372,
0.81395349]])
mu = np.mean(data1, axis=0)
sigma = np.std(data1, axis=0)
(data1 - mu) / sigma
array([[-0.22941573, -0.23111136, 1.09030457, 1.31008094, 1.30769231,
1.61229232],
[ 1.60591014, 0.84740834, -0.52145001, -0.7145896 , -0.53846154,
-0.96737539],
[-0.22941573, 0.92444546, 0.80587729, 0.59549133, 0.53846154,
0.03582872],
[-1.14707867, -1.54074243, -1.37473184, -1.19098267, -1.30769231,
-0.68074565]])
np.savetxt('test.txt',data1)
import numpy as np
import pandas as pd
import warnings
warnings.filterwarnings("ignore")
输入:
arr1 = np.random.randint(10,6,6)
arr2 = np.random.randint(10,6,6)
arr1 = np.random.randint(1,10,10)
arr2 = np.random.randint(1,10,10)
print("arr1: %s"%arr1)
print("arr2: %s"%arr2)
np.intersect1d(arr1,arr2)
arr1: [1 7 4 9 2 3 8 8 2 9]
arr2: [5 7 7 2 3 7 4 8 4 1]
array([1, 2, 3, 4, 7, 8])
输入:
arr1 = np.random.randint(1,10,10)
arr2 = np.random.randint(1,10,10)
arr1 = np.random.randint(1,10,10)
arr2 = np.random.randint(1,10,10)
print("arr1: %s"%arr1)
print("arr2: %s"%arr2)
np.setdiff1d(arr1,arr2)
arr1: [3 7 6 4 4 8 6 2 4 6]
arr2: [7 2 2 4 5 5 3 7 2 1]
array([6, 8])
输入:
arr1 = np.random.randint(1,10,10)
arr1 = np.random.randint(1,10,10)
arr1.flags.writeable = False
#尝试修改会报错!
arr1[0] = 6
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-69-ddcf305e5efb> in <module>
1 #尝试修改会报错!
----> 2 arr1[0] = 6
ValueError: assignment destination is read-only
输入:
a = [1,2,3,4,5]
a = [1,2,3,4,5]
np.array(a)
输入:
df = pd.DataFrame({‘A’:[1,2,3],‘B’:[4,5,6],‘C’:[7,8,9]})
df = pd.DataFrame({'A':[1,2,3],'B':[4,5,6],'C':[7,8,9]})
print(df)
print(df.values)
输入:
arr1 = np.random.randint(1,10,10)
arr2 = np.random.randint(1,10,10)
arr1 = np.random.randint(1,10,10)
arr2 = np.random.randint(1,10,10)
print("arr1的平均数为:%s" %np.mean(arr1))
print("arr1的中位数为:%s" %np.median(arr1))
print("arr1的方差为:%s" %np.var(arr1))
print("arr1的标准差为:%s" %np.std(arr1))
print("arr1,arr的相关性矩阵为:%s" %np.cov(arr1,arr2))
print("arr1,arr的协方差矩阵为:%s" %np.corrcoef(arr1,arr2))
输入:
arr = np.array([1,2,3,4,5])
arr = np.array([1,2,3,4,5])
np.random.choice(arr,10,p = [0.1,0.1,0.1,0.1,0.6])
输入:
arr = np.array([1,2,3,4,5])
#对副本数据进行修改,不会影响到原始数据
arr = np.array([1,2,3,4,5])
arr1 = arr.copy()
输入: arr = np.arange(10)
备注:从索引2开始到索引8停止,间隔为2
arr = np.arange(10)
a = slice(2,8,2)
arr[a] #等价于arr[2:8:2]
array([2, 4, 6])
输入:
str1 = [‘I love’]
str2 = [’ Python’]
#拼接字符串
str1 = ['I love']
str2 = [' Python']
print(np.char.add(str1,str2))
#大写首字母
str3 = np.char.add(str1,str2)
print(np.char.title(str3))
['I love Python']
['I Love Python']
输入:
arr = np.random.uniform(0,10,10)
arr = np.random.uniform(0,10,10)
print(arr)
###向上取整
print(np.ceil(arr))
###向下取整
print(np.floor(arr) )
[7.93163895 0.25298605 8.03086361 6.60320156 8.03587266 2.22664371
9.18459953 9.2502519 7.75398358 4.22163385]
[ 8. 1. 9. 7. 9. 3. 10. 10. 8. 5.]
[7. 0. 8. 6. 8. 2. 9. 9. 7. 4.]
np.set_printoptions(suppress=True)
输入:
arr = np.random.randint(1,10,[3,3])
arr = np.random.randint(1,10,[3,3])
print(arr)
print('列逆序')
print(arr[:, -1::-1])
print('行逆序')
print(arr[-1::-1, :])
[[3 1 1]
[8 7 2]
[4 4 8]]
列逆序
[[1 1 3]
[2 7 8]
[8 4 4]]
行逆序
[[4 4 8]
[8 7 2]
[3 1 1]]
输入:
arr1 = np.random.randint(1,10,5)
arr2 = np.random.randint(1,20,10)
备注:在arr2中根据arr1中元素以位置查找
arr1 = np.random.randint(1,10,5)
arr2 = np.random.randint(1,20,10)
print(arr1)
print(arr2)
print(np.take(arr2,arr1))
[9 6 5 4 9]
[13 14 4 16 4 10 15 6 5 3]
[ 3 15 10 4 3]
输入:
a = 10
b = 3
np.mod(a,b)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-76-2a4695c40758> in <module>
----> 1 np.mod(a,b)
TypeError: unsupported operand type(s) for %: 'slice' and 'int'
输入:
A = np.random.randint(1,10,[3,3])
np.linalg.svd(A)
输入:
arr = np.random.randint(1,20,10)
arr = np.random.randint(1,20,10)
print(arr[(arr>1)&(arr<7)&(arr%2==0)])
[4]
输入:
arr = np.random.randint(1,20,10)
备注:将大于等于7,或小于3的元素标记为1,其余为0
arr = np.random.randint(1,20,10)
print(arr)
print(np.piecewise(arr, [arr < 3, arr >= 7], [-1, 1]))
[17 16 1 1 7 2 5 3 13 16]
[ 1 1 -1 -1 1 -1 0 0 1 1]
输入:
arr = np.random.randint(1,10,[3,1])
备注:从数组的形状中删除单维度条目,即把shape中为1的维度去掉
arr = np.random.randint(1,10,[3,1])
print(arr)
print(np.squeeze(arr))
[[7]
[9]
[1]]
[7 9 1]
输入:
A = np.array([[1, 2, 3], [2, -1, 1], [3, 0, -1]])
b = np.array([9, 8, 3])
求解Ax = b
A = np.array([[1, 2, 3], [2, -1, 1], [3, 0, -1]])
b = np.array([9, 8, 3])
x = np.linalg.solve(A, b)
print(x)
[ 2. -1. 3.]