Python - NumPy

NumPy 基础

ndarray 对象的基本属性

  • NumPy数组的数据类型:ndarray.
  • ndarray 对象的基本属性:
    1. ndarray.ndim :数组的维度。
    2. ndarray.shape:数组的形状。
    3. ndarray.size:数组的大小(数组中元素的个数)。
    4. ndarray.dtype: 数组中元素的数据类型。
    5. ndarray.itemsize: 数组中元素的itemsize。 the size in bytes of each element of the array.
    6. ndarray.data:the buffer containing the actual elements of the array
>>> import numpy as np
>>> a = np.array([[1,2,3],[4,5,6]])
>>> a
array([[1, 2, 3],
      	[4, 5, 6]])
>>> type(a)
numpy.ndarray
>>> a.ndim
2
>>> a.shape
(2, 3)
>>> a.size
6
>>> a.dtype
dtype('int64')
>>> a.itemsize
8
>>> a.data
Out[143]: <memory at 0x10787ca68>

ndarray 对象的方法

  1. ndarray.astype(dtype)
    1. 将数组中元素的数据类型改成‘dtype’。
    2. >>> import numpy as np
      >>> a = np.array([[1.0, 2.1], [2.0, 3]])
      >>> a.astype(int)
      	array([[1, 2],
      		 [2, 3]])
      

numpy.dot()

  1. 基本用法numpy.dot(a, b)
  2. 解释: 点乘(实数之间, 矩阵之间, 实数与矩阵之间)
  3. >>> np.dot(2, 3)
    6
    >>> a = [[1, 2], [3, 4]]
    >>> b = [[1, 1], [2, 2]]
    >>> np.dot(a, b)
    array([[ 5,  5],
       [11, 11]])
    >>> np.dot(3, b)
    array([[3, 3],
       [6, 6]])
    

numpy.argmax()

  1. 基本用法numpy.argmax(a, axis=None, out=None)
  2. 解释: 返回轴上最大值的第一个索引
  3. >>> a = np.arange(8).reshape(2,4)
    >>> a
    array([[0, 1, 2, 3],
       [4, 5, 6, 7]])
    >>> np.argmax(a)
    7
    >>> a[0][0]=7
    >>> a
    array([[7, 1, 2, 3],
       [4, 5, 6, 7]])
    >>> np.argmax(a, 0)
    array([0, 1, 1, 1])
    >>> np.argmax(a, 1)
    array([0, 3])
    

numpy

numpy.frombuffer(buffer, dtype=float, count=-1, offset=0)

创建 ndarray 对象

TODO

  • 设置dtype
  • 创建ndarray对象
  • astype
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值