🍉优快云小墨&晓末:https://blog.youkuaiyun.com/jd1813346972
个人介绍: 研一|统计学|干货分享
擅长Python、Matlab、R等主流编程软件
累计十余项国家级比赛奖项,参与研究经费10w、40w级横向
文章目录
【Python进阶(六)】——随机数与数组,建议收藏!
该篇文章利用Python展示了随机数,数组的生成,以及其对应的Python处理方法和注意事项。
1 随机数
1.1 一次生成一个随机数
运行程序:
from IPython.core.interactiveshell import InteractiveShell
InteractiveShell.ast_node_interactivity = "all" ##执行多输出
import random
random.seed(3)
random.randint(1, 100) #生成一个1-100之间的随机整数
random.uniform(-10, 10)#生成一个-10-10之间的浮点数
round(random.uniform(-10, 10),2) #生成一个-10-10之间的浮点数并保留两位有效小数
运行结果:
31
1.852818212543312
-7.39
1.2 一次生成一个随机数组
运行程序:
import numpy as np
rand=np.random.RandomState(32)#参数32为种子数
x=rand.randint(0,10,(3,6))#0-10的整数随机数组,(3,6)为目标数组形状
x
import numpy as np
rand=np.random.RandomState(1)
x=rand.rand(5) *10 #生成服从均匀分布的浮点数,5个数,rand.rand返回值范围为0-1,*10调整范围
x
import numpy as np
rand=np.random.RandomState(1)
y= rand.randn(5) +5#生成数组服从正态分布,生成所有随机数+5
y
x=np.linspace(0,10,20) #生成等距数组,范围为0-10,数量为20
x
运行结果:
array([[7, 5, 6, 8, 3, 7],
[9, 3, 5, 9, 4, 1],
[3, 1, 2, 3, 8, 2]])
array([4.17022005e+00, 7.20324493e+00, 1.14374817e-03, 3.02332573e+00,
1.46755891e+00])
array([6.62434536, 4.38824359, 4.47182825, 3.92703138, 5.86540763])
array([ 0. , 0.52631579, 1.05263158, 1.57894737, 2.10526316,
2.63157895, 3.15789474, 3.68421053, 4.21052632, 4.73684211,
5.26315789, 5.78947368, 6.31578947, 6.84210526, 7.36842105,
7.89473684, 8.42105263, 8.94736842, 9.47368421, 10. ])
2 数组
2.1 创建方法
运行程序:
import numpy as np #调用ndarray之前需要导入Numpy模块
MyArray1 = np.arange(1,20) #返回一个大于等于1,小于20的有序自然数数组
MyArray1
range(1,10,2) #返回值为一个迭代器
list(range(1,10,2))#将迭代器转化为列表
np.arange(1,10,2) #返回范围值,步长为2
MyArray2=np.array([1,2,3,4,3,5]) #返回值为array
MyArray2
np.array(range(1,10,2))#返回值步长为2
MyArray3=np.zeros((5,5)) #返回5×5 0数组
MyArray3
MyArray4=np.ones((5,5))#返回5×5 1数组
MyArray4
np.full((3,5),2) #返回3×5 2数组
rand=np.random.RandomState(30)
MyArray5=rand.randint(0,100,[3,5]) #3×5 0-100随机整数数组
MyArray5
运行结果:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19])
range(1, 10, 2)
[1, 3, 5, 7, 9]
array([1, 3, 5, 7, 9])
array([1, 2, 3, 4, 3, 5])
array([1, 3, 5, 7, 9])
array([[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.],
[0., 0., 0., 0., 0.]])
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
array([[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2],
[2, 2, 2, 2, 2]])
array([[37, 37, 45, 45, 12],
[23, 2, 53, 17, 46],
[ 3, 41, 7, 65, 49]])
2.2 主要特征
运行程序:
import numpy as np
MyArray4=np.zeros(shape=(2,15),dtype=np.int) #2×15列0数组,类型为整型
MyArray4
np.ones((3,5),dtype=float) #类型为浮点型
np.ones([3,5],dtype=float) #shape参数也可也取列表[3,5]
运行结果:
array([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
array([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
2.3 切片/读取
运行程序:
import numpy as np
myArray=np.array(range(1,10))
myArray
myArray=np.arange(1,10)
myArray
myArray[0] #第1个数
myArray[-1]#倒数第一个数
import numpy as np
myArray=np.array(range(0,10))
print("myArray=",myArray)
print("myArray[1:9:2]=",myArray[1:9:2])
print("myArray[:9:2]=",myArray[:9:2])
print("myArray[::2]=",myArray[::2])
print("myArray[::]=",myArray[::])
print("myArray[:8:]=",myArray[:8:])
print("myArray[:8]=",myArray[0:8])
print("myArray[4::]=",myArray[4::])
print("myArray[9:1:-2]=",myArray[9:1:-2])
print("myArray[::-2]=",myArray[::-2])
print("myArray[[2,5,6]]=",myArray[[2,5,6]])
print("myArray[myArray>5]=",myArray[myArray>5])
myArray[0:2]
myArray[1:5:2]
myArray[::2]
myArray[::-2]
myArray
myArray=np.array(range(1,11))
myArray
myArray[[1,3,6]]#myArray[1,3,6]纠错
myArray
myArray[:,np.newaxis] #定义一个新维度
myArray[:,np.newaxis].shape #查看形状
myArray2=np.arange(1,21).reshape([5,4])#5×4数组,1-20
myArray2
myArray2[[2,4],3] #多维数组的切片,第3、4行第4列
x=[2,4]
myArray2[x,2]
运行结果:
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
array([1, 2, 3, 4, 5, 6, 7, 8, 9])
1
9
myArray= [0 1 2 3 4 5 6 7 8 9]
myArray[1:9:2]= [1 3 5 7]
myArray[:9:2]= [0 2 4 6 8]
myArray[::2]= [0 2 4 6 8]
myArray[::]= [0 1 2 3 4 5 6 7 8 9]
myArray[:8:]= [0 1 2 3 4 5 6 7]
myArray[:8]= [0 1 2 3 4 5 6 7]
myArray[4::]= [4 5 6 7 8 9]
myArray[9:1:-2]= [9 7 5 3]
myArray[::-2]= [9 7 5 3 1]
myArray[[2,5,6]]= [2 5 6]
myArray[myArray>5]= [6 7 8 9]
array([0, 1])
array([1, 3])
array([0, 2, 4, 6, 8])
array([9, 7, 5, 3, 1])
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
array([2, 4, 7])
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
array([[ 1],
[ 2],
[ 3],
[ 4],
[ 5],
[ 6],
[ 7],
[ 8],
[ 9],
[10]])
(10, 1)
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20]])
array([12, 20])
array([11, 19])
2.4 浅拷贝与深拷贝
运行程序:
import numpy as np
myArray1=np.array(range(0,10))
myArray2=myArray1
myArray2[1]=100 #修改第2个数
myArray1 #浅拷贝:复制过来的是引用,“复制对象和被复制对象共用一个存储空间”
import numpy as np
myArray1=np.array(range(0,10))
myArray2=myArray1.copy()
myArray2[1]=200
myArray1 #深拷贝:复制过来的是值,复制对象和被复制对象占用两个不同空间
myArray2
运行结果:
array([ 0, 100, 2, 3, 4, 5, 6, 7, 8, 9])
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
array([ 0, 200, 2, 3, 4, 5, 6, 7, 8, 9])
2.5 形状与重构
运行程序:
import numpy as np
MyArray5=np.arange(1,21)
MyArray5
MyArray5.shape
MyArray6=MyArray5.reshape(4,5) #重构形状
MyArray6
MyArray5.reshape(5,4)
MyArray5
MyArray5.resize(4,5) #resize不修改数组本身,即返回另一个数组,reshape就地修改原数组
MyArray5
MyArray5.swapaxes(0,1)#swapaxes方法进行轴调换,实现矩阵转置
MyArray5
MyArray5=MyArray5.swapaxes(0,1)
MyArray5
MyArray5.flatten()#将多维数组转换为一维数组
MyArray5.tolist()#将多维数组转化为嵌套列表
MyArray5.astype(np.float) #重设数组元素数据类型
MyArray5
MyArray5.reshape(5,5) #reshape前提为可以reshape
运行结果:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20])
(20,)
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]])
array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16],
[17, 18, 19, 20]])
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
18, 19, 20])
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]])
array([[ 1, 6, 11, 16],
[ 2, 7, 12, 17],
[ 3, 8, 13, 18],
[ 4, 9, 14, 19],
[ 5, 10, 15, 20]])
array([[ 1, 2, 3, 4, 5],
[ 6, 7, 8, 9, 10],
[11, 12, 13, 14, 15],
[16, 17, 18, 19, 20]])
array([[ 1, 6, 11, 16],
[ 2, 7, 12, 17],
[ 3, 8, 13, 18],
[ 4, 9, 14, 19],
[ 5, 10, 15, 20]])
array([ 1, 6, 11, 16, 2, 7, 12, 17, 3, 8, 13, 18, 4, 9, 14, 19, 5,
10, 15, 20])
[[1, 6, 11, 16],
[2, 7, 12, 17],
[3, 8, 13, 18],
[4, 9, 14, 19],
[5, 10, 15, 20]]
array([[ 1., 6., 11., 16.],
[ 2., 7., 12., 17.],
[ 3., 8., 13., 18.],
[ 4., 9., 14., 19.],
[ 5., 10., 15., 20.]])
array([[ 1, 6, 11, 16],
[ 2, 7, 12, 17],
[ 3, 8, 13, 18],
[ 4, 9, 14, 19],
[ 5, 10, 15, 20]])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
Cell In[15], line 12
8 MyArray6
10 MyArray5.reshape(5,4)
---> 12 MyArray5.reshape(5,5)
ValueError: cannot reshape array of size 20 into shape (5,5)
2.6 属性计算
运行程序:
np.rank(MyArray5) #计算数组的秩
np.ndim(MyArray5) #计算数组的秩
MyArray5.ndim #计算数组的秩
np.shape(MyArray5)#数组形状
MyArray5.shape #数组形状
MyArray5.size #数组元素个数
type(MyArray5) #查看数组类型
运行结果:
2
2
2
(5, 4)
(5, 4)
20
numpy.ndarray
2.7 ndarray的计算
运行程序:
MyArray5
MyArray5*10 #数组的乘法
x=np.array([11,12,13,14,15,16,17,18])
x1,x2,x3=np.split(x,[3,5]) #横向拆分,[3,5]为拆分位置索引
print(x1,x2,x3)
upper,lower=np.vsplit(MyArray5.reshape(4,5),[2])#纵向拆分
print("上半部分为\n",upper)
print("\n\n下半部分为\n",lower)
np.concatenate((lower,upper),axis=0) #数组的合并,逐列计算
np.vstack([upper,lower]) #行合并
np.hstack([upper,lower]) #列合并
np.add(MyArray5,1) #所有数加1
运行结果:
array([ 0, 100, 2, 3, 4, 5, 6, 7, 8, 9])
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
array([ 0, 200, 2, 3, 4, 5, 6, 7, 8, 9])array([[ 1, 6, 11, 16],
[ 2, 7, 12, 17],
[ 3, 8, 13, 18],
[ 4, 9, 14, 19],
[ 5, 10, 15, 20]])
array([[ 10, 60, 110, 160],
[ 20, 70, 120, 170],
[ 30, 80, 130, 180],
[ 40, 90, 140, 190],
[ 50, 100, 150, 200]])
[11 12 13] [14 15] [16 17 18]
上半部分为
[[ 1 6 11 16 2]
[ 7 12 17 3 8]]
下半部分为
[[13 18 4 9 14]
[19 5 10 15 20]]
array([[13, 18, 4, 9, 14],
[19, 5, 10, 15, 20],
[ 1, 6, 11, 16, 2],
[ 7, 12, 17, 3, 8]])
array([[ 1, 6, 11, 16, 2],
[ 7, 12, 17, 3, 8],
[13, 18, 4, 9, 14],
[19, 5, 10, 15, 20]])
array([[ 1, 6, 11, 16, 2, 13, 18, 4, 9, 14],
[ 7, 12, 17, 3, 8, 19, 5, 10, 15, 20]])
array([[ 2, 7, 12, 17],
[ 3, 8, 13, 18],
[ 4, 9, 14, 19],
[ 5, 10, 15, 20],
[ 6, 11, 16, 21]])
2.8 ndarray的元素类型
运行程序:
#同一数组,所有元素类型必须一致,若不一致或无显式定义,则按dtype=object处理
np.zeros(10,dtype="int16")
np.zeros(10,dtype="float")
a1=np.array([1,2,3,None])
a1
a1=np.array([1,2,3,None,np.nan])
a1
运行结果:
array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0], dtype=int16)
array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0.])
array([1, 2, 3, None], dtype=object)
array([1, 2, 3, None, nan], dtype=object)
2.9 插入与删除
运行程序:
import numpy as np
myArray1=np.array([11,12,13,14,15,16,17,18])
np.delete(myArray1,2)#删除第3个数
np.insert(myArray1,1,88) #在第2个数位置增加元素88
运行结果:
array([11, 12, 14, 15, 16, 17, 18])
array([11, 88, 12, 13, 14, 15, 16, 17, 18])array([ 0, 100, 2, 3, 4, 5, 6, 7, 8, 9])
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
array([ 0, 200, 2, 3, 4, 5, 6, 7, 8, 9])
2.10 缺失值处理
运行程序:
np.isnan(myArray)#判断数组每个元素是否为缺失值
np.any(np.isnan(myArray))#判断所有元素至少有一个缺失值
np.all(np.isnan(myArray))#判断数组中是否所有元素为缺失值
MyArray=np.array([1,2,3,np.nan])
np.nansum(MyArray) #np.nan可以参与算术运算
np.sum(MyArray)#在Numppy中,np.nan是浮点型,可以参加算术运算
运行结果:
array([False, False, False, False, False, False, False, False, False,
False])
False
False
6.0
nan
2.11 ndarray的广播规则
运行程序:
import numpy as np
A1=np.array(range(1,10)).reshape([3,3])
A1
A2=np.array([10,10,10])
A2
A1+A2#若行数不一样,列数一样,以行为单位的广播操作,进行循环补齐
A3=np.arange(10).reshape(2,5)
A3
A4=np.arange(16).reshape(4,4)
A4
A3+A4#若列数不一致(除了列数为1),解释器报错
运行结果:
array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
array([10, 10, 10])
array([[11, 12, 13],
[14, 15, 16],
[17, 18, 19]])
array([[0, 1, 2, 3, 4],
[5, 6, 7, 8, 9]])
array([[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11],
[12, 13, 14, 15]])
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-139-27dab191cac1> in <module>
4 A4=np.arange(16).reshape(4,4)
5 A4
----> 6 A3+A4#若列数不一致(除了列数为1),解释器报错
ValueError: operands could not be broadcast together with shapes (2,5) (4,4)
2.12 ndarray的排序
运行程序:
import numpy as np
myArray=np.array([11,18,13,12,19,15,14,17,16])
myArray
np.sort(myArray)#正序排序
np.argsort(myArray) #返回排序后的索引
MyArray=np.array([[21, 22, 23, 24,25],
[35, 34,33, 32, 31],
[ 1, 2, 3, 100, 4]])
np.sort(MyArray,axis=1) #行排序
np.sort(MyArray,axis=0)#列排序
运行结果:
array([11, 18, 13, 12, 19, 15, 14, 17, 16])
array([11, 12, 13, 14, 15, 16, 17, 18, 19])
array([0, 3, 2, 6, 5, 8, 7, 1, 4], dtype=int64)
array([[ 21, 22, 23, 24, 25],
[ 31, 32, 33, 34, 35],
[ 1, 2, 3, 4, 100]])
array([[ 1, 2, 3, 24, 4],
[ 21, 22, 23, 32, 25],
[ 35, 34, 33, 100, 31]])