Python numpy
import numpy a=numpy.array([1,2,3,4]) b=numpy.array([[1,2,3],[4,5,6],[7,8,9]]) print(a.shape) print(b.shape)
创建了一个一维向量和三行散列的矩阵
注意:这里要求数据是同一结构,shape函数作用:几行几列
取值:
import numpy b=numpy.array([[1,2,3],[4,5,6],[7,8,9]]) print(b[:,1])#这里打印矩阵的第二列 print(b[:,0:2])#这里取到第一列和第二列
修改矩阵中的值:
这里把5和7的值改成了10
import numpy b=numpy.array([[1,2,3],[4,5,6],[7,8,9]]) b[(b==5)|(b==7)] = 10 print(b)
强转类型:
把int型转为str型
import numpy b=numpy.array([[1,2,3],[4,5,6],[7,8,9]]) c = b.astype(str) print(c)
其他操作:
import numpy b=numpy.array([[1,2,3],[4,5,6],[7,8,9]]) print(b.min())#求最小值 print(b.max(axis=1))#按行求最大 print(b.sum(axis=0))#按列求和
import numpy as np a=np.arange(10).reshape(2,5) print(a) '''创建矩阵: [[0 1 2 3 4] [5 6 7 8 9]] ''' print(a.ndim)#求维度 print(a.shape)#几行几列 print(a.dtype.name)#矩阵数据类型名字 print(a.size)#元素个数
矩阵初始化:
import numpy as np #矩阵初始化方法: np.zeros((3,4))#3行4列矩阵初始化为0(默认为float类型) np.ones((3,4),dtype=np.int32)#3行4列初始化值为1的int类型
创建矩阵:
import numpy as np np.arange(10,30,5) #从10到30,每隔5 #array([10, 15, 20, 25]) np.random.random((2,3)) ''' 随机创建:2行3列,-1到1之间 注意:必须是两个random array([[0.20925672, 0.09790786, 0.00158854], [0.73711854, 0.83033327, 0.22525092]]) ''' np.linspace(1,3,100) #从1到3平均地取100个数(float类型)
运算:
import numpy as np a=np.array([[1,2,3],[4,5,6],[7,8,9]]) print(np.hstack((a,a))) print(np.vstack((a,a))) print(a.T) print(a+a) print(a*a) print(a.dot(a)) print(np.dot(a,a)) print(np.exp(a)) print(np.sqrt(a)) print(a.shape) print(a.ravel()) ''' 不做解释,一目了然 [[1 2 3 1 2 3] [4 5 6 4 5 6] [7 8 9 7 8 9]] [[1 2 3] [4 5 6] [7 8 9] [1 2 3] [4 5 6] [7 8 9]] [[1 4 7] [2 5 8] [3 6 9]] [[ 2 4 6] [ 8 10 12] [14 16 18]] [[ 1 4 9] [16 25 36] [49 64 81]] [[ 30 36 42] [ 66 81 96] [102 126 150]] [[ 30 36 42] [ 66 81 96] [102 126 150]] [[2.71828183e+00 7.38905610e+00 2.00855369e+01] [5.45981500e+01 1.48413159e+02 4.03428793e+02] [1.09663316e+03 2.98095799e+03 8.10308393e+03]] [[1. 1.41421356 1.73205081] [2. 2.23606798 2.44948974] [2.64575131 2.82842712 3. ]] (3, 3) [1 2 3 4 5 6 7 8 9] '''
import numpy as np a=np.array([[1,2,3],[4,5,6],[7,8,9]]) print(a.argmax(axis=0)) #[2 2 2]列最大索引值 print(a.argmin(axis=1)) #[0 0 0]行最小索引值
import numpy as np a=np.arange(0,40,10) print(a) b=np.tile(a,(3,2)) c=np.tile(a,(2,3)) print(b) print(c) ''' [ 0 10 20 30] [[ 0 10 20 30 0 10 20 30] [ 0 10 20 30 0 10 20 30] [ 0 10 20 30 0 10 20 30]] [[ 0 10 20 30 0 10 20 30 0 10 20 30] [ 0 10 20 30 0 10 20 30 0 10 20 30]] '''
排序:
import numpy as np a=np.array([[1,4,6],[2,9,7],[5,3,8]]) print(a) ''' [[1 4 6] [2 9 7] [5 3 8]] ''' b=np.sort(a,axis=1)#按行排列 print(b) ''' [[1 4 6] [2 7 9] [3 5 8]] ''' c=np.sort(a,axis=0)#按列排列 print(c) ''' [[1 3 6] [2 4 7] [5 9 8]] ''' d=np.argsort(a)#索引值排序 print(d) ''' [[0 1 2] [0 2 1] [1 0 2]] '''
特别注意:
import numpy as np a=np.array([[1,2,3],[4,5,6],[7,8,9]]) c=a.view() print(c is a)#false(c和a指向内存地址不同) #复制了a,赋值给c #如果是c=a,那么c和a是同一个(指向同一个地址) #print(c is a)地话,就会打印true c[1,2] = 100 print(a) ''' [[ 1 2 3] [ 4 5 100] [ 7 8 9]] ''' #这里发现修改了c,那么a也被修改了 #c和a虽然地址不同,但是共用一组数据 d=a.copy() print(d is a)#false d[1,3] = 100 #这里没有改变a print(a)
读取txt文件:
import numpy #第一个参数为路径,第二个参数为分隔符,第三个参数是读取的类型 #最后一个参数意思:是否去掉第一行 a=numpy.genfromtxt("d:/a.txt",delimiter=",",dtype="str",skip_header=1) print(a)
相关推荐
世事一场大梦 2020-11-17
夜斗不是神 2020-11-17
xiaoseyihe 2020-11-16
huavhuahua 2020-11-20
Yasin 2020-11-16
xiaoseyihe 2020-11-16
千锋 2020-11-15
diyanpython 2020-11-12
chunjiekid 2020-11-10
wordmhg 2020-11-06
Morelia 2020-11-03
CloudXli 2020-11-03
文山羊 2020-10-31
comtop0 2020-10-31
pythonxuexi 2020-10-30
三石 2020-10-29
chaochao 2020-10-27
PythonMaker 2020-10-27