- 博客(15)
- 资源 (8)
- 收藏
- 关注
原创 numpy ndarray判断矩阵是否含有一个元素(ndarray.__contains__())
a=np.array([[3, 7, 3],[3, 7, 3],[7, 3, 2]])若判断矩阵a里是否有7和8a.__contains__(7)Out[204]: True说明含有7a.__contains__(8)Out[205]: False说明不含有8
2020-01-18 15:54:30 9551
原创 numpy中ndarray数组中选出指定元素构造新数组(ndarray.take())
官方文档take用法解释先随机生成一个矩阵aimport numpy as npa=np.random.randint(1,9,size=9).reshape((3,3))结果array([[7, 3, 2], [8, 7, 5], [3, 7, 3]])如果希望提取某个axis的若干切片ndarray.take(indices,axis)如,提取...
2020-01-18 15:31:40 4480 3
原创 numpy.clip
ndarray自带的clip method生成一个矩阵a=np.array([[1,2,3],[4,5,6]])aOut[175]: array([[1, 2, 3], [4, 5, 6]])如果我们希望<5的数值为5,则a.clip(5)Out[176]: array([[5, 5, 5], [5, 5, 6]])如果我们希望<5...
2020-01-18 15:11:05 1090 1
原创 numpy中ndarray矩阵如何与虚数相乘
如果我们要把矩阵a=np.array([[1,2,3],[4,5,6]])乘以虚数单位i,可以按照如下方式In[167]: a*(1j)Out[167]: array([[0.+1.j, 0.+2.j, 0.+3.j], [0.+4.j, 0.+5.j, 0.+6.j]])a*(1j)...
2020-01-18 14:58:48 3133
原创 numpy.ndarray类常见的属性(attributes)
numpy中ndarray常见的属性dtype:数据类型T:转置(注意一维数组问题)shape:形状ndim:维度个数size:总的元素个数conj:共轭real:实部imag:虚部flat:1-D的iterator先随机生成3*3的方阵import numpy as npa=np.random.randint(1,9,size=9).reshape((3,3))pri...
2020-01-18 14:50:42 941
原创 numpy中的求和函数:cumsum和sum
numpy中的求和:cumsum和sumimport numpy as npa=np.random.randint(1,9,size=9).reshape((3,3))#随机生成3X3的矩阵print(a)print(a.sum(axis=0))#对每一行求和print(a.cumsum(axis=0))#对每一行求累加和打印结果[[7 3 5] [2 7 2] [7 6 5...
2020-01-18 14:23:45 2666
原创 numpy中ndarray数组转化为list
随机生成矩阵import numpy as npa=np.random.randint(1,9,size=9).reshape((3,3))打印结果array([[7, 6, 6], [1, 1, 8], [2, 4, 8]])如果想把矩阵转换成list类型print(a.tolist())打印结果[[7, 6, 6], [1, 1, 8], [...
2020-01-18 14:12:50 1924
原创 numpy如何将ndarray矩阵用相同元素填充(ndarray.fill)
numpy如何将array矩阵中的元素用相同元素随机生成一个矩阵import numpy as npa=np.random.randint(1,9,size=9).reshape((3,3))打印结果 [[3, 4, 8], [5, 8, 4], [4, 8, 3]]如果想把矩阵所有元素用1替代在命令输入a.fill(1)打印结果...
2020-01-18 14:08:14 2853
原创 如何把ndarray矩阵输出为字符串
标题如何把ndarray矩阵输出为字符串随机生成一个3X3的矩阵,如何将其转换成字符串输出?import numpy as npa=np.random.randint(1,9,size=9).reshape((3,3))print(a.__str__())打印矩阵[[3 4 8] [5 8 4] [4 8 3]]这时,可以调用ndarray的自带method完成在命令行输...
2020-01-18 14:00:00 3240
原创 numpy block创建分块矩阵
import numpy as npa=np.random.randint(1,9,size=9).reshape((3,3))print(a)b=np.random.randint(1,9,size=3)print(b)显示随机生成两个矩阵#a矩阵[[4 1 3] [7 2 3] [5 1 1]]#b矩阵[5 3 2]把b拼接在a右侧b=np.expand_dim...
2020-01-15 13:43:25 9117
原创 numpy数组元素的拼接(concatenate)
把一个13的矩阵拼接在33的矩阵下方随机生成两个矩阵import numpy as npa=np.random.randint(1,9,size=9).reshape((3,3))print(a)b=np.random.randint(1,9,size=3)print(b)结果[[5 8 5] [4 8 6] [8 6 4]][5 3 7]如果要把矩阵b拼接在a下方:...
2020-01-15 13:30:42 1658
原创 numpy ndarray 提取对角元素
方式1使用class methodimport numpy as npa=np.array([[1.123,2.123,3.123],[4.456,5.456,6.456],[7.789,8.789,9.789]])print(a.diagonal(-1))#数字代表偏移量结果:array([4.456, 8.789])方式2使用库函数np.diag(a,-1)#命令行结...
2020-01-15 13:04:21 2176
原创 numpy 如何指定矩阵中输出的小数位数
如果有一个小数位很多的矩阵,我们希望只显示前某几位,该怎么办?```pythonimport numpy as npa=np.array([[1.123,2.123,3.123],[4.456,5.456,6.456],[7.789,8.789,9.789]])print(a.round(0))#显示整数部分print(a.round(1))#显示一位小数输出结果:```pyt...
2020-01-15 12:56:43 13784 1
原创 numpy 一维矩阵转置问题
##Numpy 一位矩阵转置问题numpy中,我们如果直接对一维行矩阵转置,会出现问题这时,可以采用expand_dimsimport numpy as npd=np.zeros((1,9))d.shaped=np.expand_dims(d,axis=0)d.shaped.Td.transpose()expand_dims后的shape是(9,1),a.T就没有问题了...
2020-01-14 20:24:02 2089
Linear Algebra Pure & Applied
2018-12-03
Vector Calculus
2018-11-25
空空如也
TA创建的收藏夹 TA关注的收藏夹
TA关注的人