python学习——numpy的基础操作

【1】数列的创建

1.使用numpy生成数列

import numpy as np
#t1,t2的效果相同
t1 = np.array(range(1,10,2))
t2 = np.arange(1,10,2)
print(t1,t2)
#[1 3 5 7 9] [1 3 5 7 9]

2.生成随机数

import numpy as np

t3 = np.random.random() #生成一个小于1的随机小数
print(np.round(t3,2)) #保留小数点后两位

t4 = np.random.random(2) #生成2个小于1的随机小数

【2】数列的计算

对 t5 直接进行 加/减/乘/除 时相当于矩阵中每个元素都进行了操作

t5 = np.arange(12)
t5 = t5.reshape(3,4)
t5 +=2
print(t5)
'''
[[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
 '''
t5 -=2
t6 = np.arange(100,112).reshape(3,4)
print(t5,'\n\n',t6,'\n')
print(t5+t6)
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]] 

 [[100 101 102 103]
 [104 105 106 107]
 [108 109 110 111]] 

[[100 102 104 106]
 [108 110 112 114]
 [116 118 120 122]]
'''

不同维度的相加/相减:
二维数组如3x4,可以通过1x4的数组对每行进行相加/减/乘/除,也可以通过3x1的数组对每列进行相加/减/乘/除
(即二维数组某一维度上相同,可以进行运算)

import numpy as np


t5 = np.arange(12)
t5 = t5.reshape(3,4)

t7 = np.arange(4)
t8 = np.arange(3).reshape(3,1)
print(t5,'\n')
print(t5+t7,'\n')
print(t5-t8)
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]] 

[[ 0  2  4  6]
 [ 4  6  8 10]
 [ 8 10 12 14]] 

[[0 1 2 3]
 [3 4 5 6]
 [6 7 8 9]]
'''

多维度的 ‘数组a’ 和 ‘数组b’ 进行运算的时候遵循广播原则,如:b为2x3x3维度,a为3x33x1维则可以进行运算(即从后往前数维数相同)

(3)赋值与拷贝

b = a ,c= b 时对a进行操作,b、c会同时发生变化。对b进行操作,a也会发生变化,因为三者相关联(指向同一内存)
若不想进行关联,可以使用copy

import numpy as np

a = np.arange(12).reshape(3,4)
b = a.copy()
# 或 b = a[:] 
print(b)
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
 '''

【3】维度变化

reshape改变数组的维度,得到返回值,并不对自身发生改变,注意总元素必须可以分成所要的维度,否则会报错。
如:12个元素的数,可也分为3x4或2x6或2x3x1,但不能分成5x3
(1)多维降一维

import numpy as np
t5 = np.arange(12) 

t6 = t6.reshape(-1) 
t6 = t6.ravel()
t6 = t6.flatten()

(2)升维
单括号变双括号
不会对a本身进行改变,需要赋值

import numpy as np
a = np.array([1,2,3])
b = np.array([1,1,1])

print(np.mat(a))
a = a[np.newaxis,:]
print(a)
print(a.shape) #一行三列
#[[1 2 3]]
#[[1 2 3]]
#(1,3)

(3)转置

import numpy as np
a = np.array([1,2,3])
b = np.array([1,1,1])

print((a[:,np.newaxis]))
a = np.vstack(a)
#或a = a.reshape(3,1)
print(a)
print(a.shape) #三行一列
'''
[[1]
 [2]
 [3]]
[[1]
 [2]
 [3]]
 (1, 3)
 '''

也可以直接在定义的时候进行变化

import numpy as np

a = np.array([1,2,3]).reshape(3,1)
b = np.array([1,1,1])[:,np.newaxis]

print(a)
print(b)
'''
[[1]
 [2]
 [3]]
[[1]
 [1]
 [1]]
'''

【4】数列的合并

import numpy as np

a = np.array([1,2,3])
b = np.array([1,1,1])

c = np.vstack((a,b)) #vertical stack 垂直方向加
d = np.hstack((a,b)) #horizontal stack 水平方向加
print(c)
print(d)
'''
[[1 2 3]
 [1 1 1]]
[1 2 3 1 1 1]
'''

concatenate 是vstack和hstack的合并,axis = 1 时对列坐标进行操作(相当于hstack),axis = 0 时对行坐标进行操作(相当于vstack)

扫描二维码关注公众号,回复: 12329828 查看本文章
import numpy as np

a = np.array([1,2,3]).reshape(3,1)
b = np.array([1,1,1])[:,np.newaxis]

c = np.concatenate((a,a,b),axis=1)
print(c)
d = np.concatenate((a,a,b),axis=0)
print(d)
'''
[[1 1 1]
 [2 2 1]
 [3 3 1]]
[[1]
 [2]
 [3]
 [1]
 [2]
 [3]
 [1]
 [1]
 [1]]
'''

【4】数列的分割

import numpy as np

a = np.arange(12).reshape(3,4)
print(a)
b = np.vsplit(a,3)
print(b)
c = np.hsplit(a,2)
print(c)
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]
'''

和concatenate一样,axis = 1 时(列操作:列分),axis = 0 时(行操作:行分)

import numpy as np

a = np.arange(12).reshape(3,4)
print(a)
b = np.split(a,2,axis=1)
print(b)
c = np.split(a,3,axis=0)
print(c)
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]
'''

猜你喜欢

转载自blog.csdn.net/qq_46126258/article/details/107577072