声明一个张量
import numpy as np
A=np.array([[0,2,4,7],
[2,4,6,9],
[1,3,7,0]])
观察形状、数据结构
print(A.shape)
print(A.dtype)
索引
第0行第三个数
print(A[0,2])
根据索引取值
多个索引
张量计算
广播机制,可以进行并行的计算
print(3*A-1)
'''
[[-1 5 11 20]
[ 5 11 17 26]
[ 2 8 20 -1]]
'''
全0 矩阵
N=np.zeros(A.shape,dtype=int)
print(N)
'''
[[0 0 0 0]
[0 0 0 0]
[0 0 0 0]]
'''
全1 矩阵
N1=np.ones(A.shape,dtype=int)
print(N1)
'''
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
'''
对角矩阵
I=np.eye(4,dtype=int)
print(I)
'''
[[1 0 0 0]
[0 1 0 0]
[0 0 1 0]
[0 0 0 1]]
'''
随机矩阵
R=np.random.randint(0,10,(2,3)) #creating an array of dimension (2,3) with random integer between 0 1nd 10(excluding 10)
print(R)
'''
[[2 9 8]
[0 4 1]]
'''
R2=np.random.randint(0,10,(2,3,4)) #creating an array of dimension (2,3,4) with random integer between 0 1nd 10(excluding 10)
print(R2)
'''
[[[3 6 7 1]
[5 0 1 0]
[0 8 7 9]]
[[8 0 1 0]
[7 5 4 3]
[3 3 6 1]]]
'''
创建顺序矩阵
#Creating sample array
arr = np.arange(0,11)
print(arr)
'''
[ 0 1 2 3 4 5 6 7 8 9 10]
'''
创建均匀矩阵
隔着取点,方便做1,1.1,1.2,1.3…的取值等差数列取值
#creating a linspace
arr2=np.linspace(1,50,6)
print(arr2)
'''
[ 1. 10.8 20.6 30.4 40.2 50. ]
'''
列表转numpy数组
#creating a list
my_list1 = [1,2,3,4]
# Make another list
my_list2 = [11,22,33,44]
#Make a list of lists
my_lists = [my_list1,my_list2]
#Make multi-dimensional array
my_array2 = np.array(my_lists)
#Show array
my_array2
my_array2.shape # 张量形状+
my_array2.size # 数据量
my_array2.dtype # 数据类型
'''
array([[ 1, 2, 3, 4],
[11, 22, 33, 44]])
(2, 4)
8
dtype('int32')
'''
numpy数组转列表
list_my_array2=my_array2.tolist() #converting the array into list
list_my_array2
'''
[[1, 2, 3, 4], [11, 22, 33, 44]]
'''
添加元素
跟python自带的列表没什么区别,只不过是张量运算,np.append
import numpy as np
A=np.array([[0,2,4,7],
[2,4,6,9],
[1,3,7,0]])
B=np.append(A,[4,5,6,8])
B
'''
array([0, 2, 4, 7, 2, 4, 6, 9, 1, 3, 7, 0, 4, 5, 6, 8])
'''
np.insert 插入
#inserting the value into array
C=np.insert(B,3,9)
C
'''
array([0, 2, 4, 9, 7, 2, 4, 6, 9, 1, 3, 7, 0, 4, 5, 6, 8])
'''
删除元素
删除索引为4的元素
#Deleting an element at index 4 from array
D=np.delete(C,4,axis=0)
D
'''
array([0, 2, 4, 9, 2, 4, 6, 9, 1, 3, 7, 0, 4, 5, 6, 8])
'''
合并张量
#concatenate the two array
E=np.concatenate((B,C),axis=0)
E
'''
array([0, 2, 4, 7, 2, 4, 6, 9, 1, 3, 7, 0, 4, 5, 6, 8, 0, 2, 4, 9, 7, 2,4, 6, 9, 1, 3, 7, 0, 4, 5, 6, 8])
'''
# 指定维度合并
#concatenate the two array with axis 1
arr4=np.array([[2,3],
[5,6]])
arr5=np.array([[21,13],
[51,16]])
F=np.concatenate((arr4,arr5),axis=1)
F
'''
array([[ 2, 3, 21, 13],
[ 5, 6, 51, 16]])
'''
split 方法切割
#splitting the array into 3 subarray
S=np.split(E,3)
S
'''
array([0, 2, 4, 7, 2, 4, 6, 9, 1, 3, 7]),
array([0, 4, 5, 6, 8, 0, 2, 4, 9, 7, 2]),
array([4, 6, 9, 1, 3, 7, 0, 4, 5, 6, 8])]
'''
调整形状
import numpy as np
#Create array
arr = np.arange(50).reshape((10,5))
arr
'''
array([[ 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],
[25, 26, 27, 28, 29],
[30, 31, 32, 33, 34],
[35, 36, 37, 38, 39],
[40, 41, 42, 43, 44],
[45, 46, 47, 48, 49]])
'''
转置
# transpose
arr.T
'''
array([[ 0, 5, 10, 15, 20, 25, 30, 35, 40, 45],
[ 1, 6, 11, 16, 21, 26, 31, 36, 41, 46],
[ 2, 7, 12, 17, 22, 27, 32, 37, 42, 47],
[ 3, 8, 13, 18, 23, 28, 33, 38, 43, 48],
[ 4, 9, 14, 19, 24, 29, 34, 39, 44, 49]])
'''
切片操作
arr_2 = np.array([[1,2,3],[4,5,6],[7,8,9]])
arr_2
'''
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
'''
print(arr_2[:2,2])
'''
[3 6]
'''
print(arr_2[0,::2])
'''
[1 3]
'''
张量排序
arr=np.random.randint(0,10,8)
arr
'''
array([2, 6, 5, 1, 5, 5, 2, 8])
'''
arr.sort()
'''
array([1, 2, 2, 5, 5, 5, 6, 8])
'''
均值、求和、方差、标准差
import numpy as np
arr5=np.random.randint(0,50,25)
arr5
'''
array([16, 2, 45, 0, 4, 22, 18, 49, 47, 40, 14, 6, 31, 1, 35, 33, 18,
7, 48, 32, 28, 15, 1, 23, 43])
#Getting mean value
#Getting mean value
'''
# 均值
np.mean(arr5,axis=0)
'''
23.12
'''
# 求和
np.sum(arr5)
'''
578
'''
#Getting varience 方差
np.var(arr5)
# 174.4896
#Getting max value 最大值
np.max(arr5)
#Getting min value
np.min(arr5)
# 1
矩阵运算
#creating an array
a=np.array([[2,3],[4,5]])
b=np.array([[12,13],[14,15]])
np.add(a,1)
# array([[3, 4], [5, 6]])
np.subtract(b,2)
# array([[10, 11],[12, 13]])
np.multiply(a,2)
'''array([[ 4, 6],
[ 8, 10]])'''
np.divide(b,2)
#array([[ 6. , 6.5],
# [ 7. , 7.5]])