Numpy

np.shape

shape函数返回list,array,matrix等的一维和二维长度值。

list

列表只有一维,二维为0

>>> a = [1,2,3]
>>> np.shape(a)
(3,)

list是没有shape函数的

>>> a.shape[0]
Traceback (most recent call last):
  File "<pyshell#32>", line 1, in <module>
    a.shape[0]
AttributeError: 'list' object has no attribute 'shape'

array

>>> a = np.eye(4,3)
>>> a
array([[ 1.,  0.,  0.],
       [ 0.,  1.,  0.],
       [ 0.,  0.,  1.],
       [ 0.,  0.,  0.]])
>>> np.shape(a)
(4, 3)
>>> a.shape[0]
4
>>> a.shape[1]
3

matrix

>>> b = mat([[1,2,3],[2,3,5]])
>>> b
matrix([[1, 2, 3],
        [2, 3, 5]])
>>> np.shape(b)
(2, 3)

np.arange

arange函数用于创建等差数组

np.arange([start, ]stop, [step, ]dtype=None)
start:可忽略不写,默认从0开始;起始值
stop:结束值;生成的元素不包括结束值
step:可忽略不写,默认步长为1;步长
dtype:默认为None,设置显示元素的数据类型
#举例
import numpy as np
nd1 = np.arange(5)#array([0, 1, 2, 3, 4])
nd2 = np.arange(1,5)#array([1, 2, 3, 4])
nd3 = np.arange(1,5,2)#nd3 = np.arange(1,5,2)
#可以对生成的等差一维数组,进行重塑,使用的是reshape(行,列)
nd2.reshape(2,2)#array([[1, 2], [3, 4]])
#注意:对数组重塑后的元素个数不能大于原来本身的元素个数,不然会报错
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值