numpy基础

ndarray类属性

numpy的数据结构

All ndarrays are homogenous: every item takes up the same size block of memory, and all blocks are interpreted in exactly the same way.

其实ndarray中的数据也可以是字符串

numpy的两个重要特点vectorization与broadcasting

Vectorization describes the absence of any explicit looping, indexing, etc., inthe code - these things are taking place, of course, just “behind the scenes”in optimized, pre-compiled C code. Vectorized code has many advantages, amongwhich are:

  1. vectorized code is more conciseand easier to read

  1. fewer lines of code generallymeans fewer bugs

  1. the code more closely resemblesstandard mathematical notation (making it easier, typically, to correctly code mathematicalconstructs)

  1. vectorization results in more“Pythonic” code. Without vectorization, our code would be littered withinefficient and difficult to read for loops.

Broadcasting is the term used to describe the implicitelement-by-element behavior of operations; generally speaking,in NumPy alloperations, not just arithmetic operations, but logical, bit-wise, functional,etc., behave in this implicit element-by-element fashion, i.e., they broadcast.

Numpy中broadcasting

Broadcasting can be understood by four rules:

  1. 让所有输入数组都向其中shape最长的数组看齐,shape中不足的部分都通过在前面加1补齐

  1. 输出数组的shape是输入数组shape的各个轴上的最大值

  1. 如果输入数组的某个轴 (和输出数组的对应轴的长度相同)或者(其长度为1)时,这个输入数组能够用来计算,否则出错

  1. 当输入数组的某个轴的长度为1时,沿着此轴运算时都用此轴上的第一个数据实体

A set of arrays is called “broadcastable” to the same shape if the above rules produce a validresult, i.e., one of the following is true:

索引多维数组的某一列时返回的是一个行向量

>>> X = np.array([[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12]])
>>> X[:, 1]
array([2, 6, 10])       % 这里是一个行
>>> X[:, 1].shape       % X[:, 1]的用法完全等同于一个行,而不是一个列
(3, )

所以,一种正确的索引方式是:

>>>X[:, 1][:, np.newaxis]
array([[2],
      [6],
      [10]])

也就是说shape为(m, )的ndarray数组,其真实的shape为(1, m),即行向量

numpy和scipy的关系

numpy 准确地说提供了一个在python中做科学计算的基础库,侠义地讲它重在数值计算,甚至可以说是用于多维数组处理的库;而 scipy 则是基于numpy,提供了一个在python中做科学计算的工具集,也就是说它是更上一个层次的库

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wugou2014

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值