赞
踩
CSV(逗号分隔值)是一种常见的文本文件格式,用于存储表格数据。CSV 文件使用逗号作为字段之间的分隔符,每行表示一个数据记录,每个字段表示一个数据项。
- a = np.arange(20).reshape((4,5))
- a
- Out[177]:
- array([[ 0, 1, 2, 3, 4],
- [ 5, 6, 7, 8, 9],
- [10, 11, 12, 13, 14],
- [15, 16, 17, 18, 19]])
- np.savetxt('a.csv',a,fmt='%.1f',delimiter=',')
- #显示当前目录即可在此目录下查看a.csv文件
- import os
- current_directory = os.getcwd()
- print(current_directory)
- b = np.loadtxt('a.csv',delimiter=',')
- b
- Out[184]:
- array([[ 0., 1., 2., 3., 4.],
- [ 5., 6., 7., 8., 9.],
- [10., 11., 12., 13., 14.],
- [15., 16., 17., 18., 19.]])
- #先获得float类型数据再转为int类型
- b = np.loadtxt('a.csv',delimiter=',').astype(np.int32)
- b
- Out[187]:
- array([[ 0, 1, 2, 3, 4],
- [ 5, 6, 7, 8, 9],
- [10, 11, 12, 13, 14],
- [15, 16, 17, 18, 19]])
- #通过converters参数将获取数据解析为float类型,再将数据转为int类型
- b = np.loadtxt('a.csv',dtype=np.int32,delimiter=',',converters=float)
- b
- Out[190]:
- array([[ 0, 1, 2, 3, 4],
- [ 5, 6, 7, 8, 9],
- [10, 11, 12, 13, 14],
- [15, 16, 17, 18, 19]])
- #unpack参数是将一列作为一个数组进行返回
- b = np.loadtxt('a.csv',dtype=np.int32,delimiter=',',converters=float,unpack='true')
- b
- Out[193]:
- array([[ 0, 5, 10, 15],
- [ 1, 6, 11, 16],
- [ 2, 7, 12, 17],
- [ 3, 8, 13, 18],
- [ 4, 9, 14, 19]])
- a
- Out[194]:
- array([[ 0, 1, 2, 3, 4],
- [ 5, 6, 7, 8, 9],
- [10, 11, 12, 13, 14],
- [15, 16, 17, 18, 19]])
- #设置了sep参数,存储类型为字符型
- a.tofile("a.txt",sep=',',format='%s')
- b
- Out[197]:
- array([[ 0, 5, 10, 15],
- [ 1, 6, 11, 16],
- [ 2, 7, 12, 17],
- [ 3, 8, 13, 18],
- [ 4, 9, 14, 19]])
- #未设置了sep参数,存储类型为二进制类型
- b.tofile("b.txt",format='%s')
-
- #读取到的数据不具有维度信息,均为一维
- read_a = np.fromfile("a.txt",dtype=np.float32,count=-1,sep=",")
- read_a
- Out[204]:
- array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.,
- 13., 14., 15., 16., 17., 18., 19.], dtype=float32)
- #reshape一下
- read_a = np.fromfile("a.txt",dtype=np.float32,count=-1,sep=",").reshape((4,5))
- read_a
- Out[206]:
- array([[ 0., 1., 2., 3., 4.],
- [ 5., 6., 7., 8., 9.],
- [10., 11., 12., 13., 14.],
- [15., 16., 17., 18., 19.]], dtype=float32)
- #由于tofile未设置stp参数(设置分隔符)读取也不设置
- read_b = np.fromfile("b.txt",dtype=np.int32,count=-1).reshape((4,5))
- read_b
- Out[211]:
- array([[ 0, 5, 10, 15, 1],
- [ 6, 11, 16, 2, 7],
- [12, 17, 3, 8, 13],
- a
- Out[212]:
- array([[ 0, 1, 2, 3, 4],
- [ 5, 6, 7, 8, 9],
- [10, 11, 12, 13, 14],
- [15, 16, 17, 18, 19]])
- np.save("a.npy",a)
- a_npy = np.load("a.npy")
- a_npy
- Out[215]:
- array([[ 0, 1, 2, 3, 4],
- [ 5, 6, 7, 8, 9],
- [10, 11, 12, 13, 14],
- [15, 16, 17, 18, 19]])
- #注:该方法下存的文件格式为npy,numpy指定格式
基本随机库函数
- = np.random.rand(2,3)
- a
- Out[219]:
- array([[0.9060742 , 0.52295812, 0.83480179],
- [0.53175022, 0.72104291, 0.43391776]])
- a = np.random.randn(2,3)
- a
- Out[221]:
- array([[ 0.07845213, -0.93702257, -0.80773318],
- [ 0.44340223, 0.20507742, 0.5476877 ]])
- a = np.random.randint(1,10,(2,3))
- a
- Out[224]:
- array([[6, 3, 6],
- [5, 2, 8]])
- np.random.seed(1)
- np.random.rand()
- Out[226]: 0.417022004702574
- np.random.rand()
- Out[227]: 0.7203244934421581
- np.random.seed(1)
- np.random.rand()
- Out[229]: 0.417022004702574
- #在choice函数中,参数a数组必须是一维数组或是一个范围
- a = np.arange(20).reshape((4,5))
- a
- Out[241]:
- array([[ 0, 1, 2, 3, 4],
- [ 5, 6, 7, 8, 9],
- [10, 11, 12, 13, 14],
- [15, 16, 17, 18, 19]])
- np.random.shuffle(a)
- a
- Out[243]:
- array([[ 0, 1, 2, 3, 4],
- [10, 11, 12, 13, 14],
- [ 5, 6, 7, 8, 9],
- [15, 16, 17, 18, 19]])
- np.random.permutation(a)
- Out[244]:
- array([[ 5, 6, 7, 8, 9],
- [10, 11, 12, 13, 14],
- [15, 16, 17, 18, 19],
- [ 0, 1, 2, 3, 4]])
- a
- Out[245]:
- array([[ 0, 1, 2, 3, 4],
- [10, 11, 12, 13, 14],
- [ 5, 6, 7, 8, 9],
- [15, 16, 17, 18, 19]])
- a
- Out[256]:
- array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
- 17, 18, 19])
- np.random.choice(a,(2,10),p = a/np.sum(a))
- Out[266]:
- array([[16, 9, 10, 14, 5, 15, 7, 15, 16, 6],
- [13, 16, 13, 4, 14, 16, 14, 19, 15, 19]])
- #参数解释
- numpy.random.choice(a, size=None, replace=True, p=None)
- a:一维数组或整数范围。如果是一维数组,则表示从该数组中进行选择;如果是整数范围,则表示从该范围内的整数进行选择。
- size(可选):表示要选择的元素个数。如果不提供该参数,则默认选择一个元素。
- replace(可选):表示是否允许重复选择。如果为 True,则允许重复选择;如果为 False,则不允许重复选择。默认为 True。
- p(可选):表示每个元素被选择的概率。如果提供了该参数,则必须与数组 a 的长度相同。默认情况下,每个元素被选择的概率相等。
- a = np.arange(5)
- a
- Out[281]: array([0, 1, 2, 3, 4])
- b = np.random.choice(a,(5,1),replace=False,p=[0.1,0.2,0.1,0.1,0.5])
- b
- Out[285]:
- array([[4],
- [1],
- [2],
- [3],
- [0]])
- a = np.random.uniform(1,10,(2,5))
- a
- Out[287]:
- array([[5.44392743, 6.57960147, 8.4608281 , 2.41112255, 1.16718582],
- [1.63019929, 5.377106 , 6.45696515, 6.11966293, 3.85626168]])
- b = np.random.normal(10,5,(2,5))
- b
- Out[289]:
- array([[10.16016264, 10.98135104, 14.5372558 , -0.67417408, 14.08423588],
- [15.92085655, 8.97759718, 9.4457777 , 17.07241363, 2.91677499]])
- c = np.random.poisson(0.5,(2,5))
- c
- Out[291]:
- array([[0, 1, 0, 1, 1],
- [1, 0, 1, 2, 0]])
- a = np.arange(20).reshape(4,5)
- a
- Out[293]:
- array([[ 0, 1, 2, 3, 4],
- [ 5, 6, 7, 8, 9],
- [10, 11, 12, 13, 14],
- [15, 16, 17, 18, 19]])
- np.sum(a,axis=1)
- Out[294]: array([10, 35, 60, 85])
- np.sum(a,axis=0)
- Out[295]: array([30, 34, 38, 42, 46])
- np.sum(a)
- Out[296]: 190
- np.mean(a,axis=1)
- Out[297]: array([ 2., 7., 12., 17.])
- np.mean(a,axis=0)
- Out[298]: array([ 7.5, 8.5, 9.5, 10.5, 11.5])
- np.mean(a)
- Out[299]: 9.5
- np.average(a,axis=1,weights=[1,2,3,4,5])
- Out[301]: array([ 2.66666667, 7.66666667, 12.66666667, 17.66666667])
- np.average(a,axis=0,weights=[1,2,3,4])
- Out[302]: array([10., 11., 12., 13., 14.])
- np.average(a)
- Out[303]: 9.5
- np.std(a)
- Out[304]: 5.766281297335398
- np.var(a)
- Out[305]: 33.25
- np.var(a,axis=1)
- Out[306]: array([2., 2., 2., 2.])
- np.var(a,axis=0)
- Out[307]: array([31.25, 31.25, 31.25, 31.25, 31.25])
- a
- Out[308]:
- array([[ 0, 1, 2, 3, 4],
- [ 5, 6, 7, 8, 9],
- [10, 11, 12, 13, 14],
- [15, 16, 17, 18, 19]])
- np.min(a)
- Out[309]: 0
- np.max(a)
- Out[310]: 19
- np.argmax(a)
- Out[311]: 19
- np.unravel_index(np.argmax(a),a.shape)
- Out[313]: (3, 4)
- np.median(a)
- Out[315]: 9.5
- a = np.random.randint(0,20,(5))
- a
- Out[320]: array([15, 18, 14, 12, 5])
- np.gradient(a)
- Out[321]: array([ 3. , -0.5, -3. , -4.5, -7. ])
- a = np.random.randint(0,20,(2,5))
- a
- Out[323]:
- array([[12, 12, 12, 7, 7],
- [11, 12, 18, 6, 16]])
- np.gradient(a)
- Out[324]:
- [array([[-1., 0., 6., -1., 9.],
- [-1., 0., 6., -1., 9.]]),
- array([[ 0. , 0. , -2.5, -2.5, 0. ],
- [ 1. , 3.5, -3. , -1. , 10. ]])]
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。