原始的 Python list 虽然很好用,但是不具备能够“整体”进行数学运算的性质,并且速度也不够快(按照视频上的说法),而 Numpy.array 恰好可以弥补这些缺陷。

初步应用就是“整体数学运算”和“subset(取子集、随机访问)”。

1、如何构造一个 Numpy array

# Create list baseball
baseball = [180, 215, 210, 210, 188, 176, 209, 200] # Import the numpy package as np
import numpy as np # Create a numpy array from baseball: np_baseball
np_baseball = np.array(baseball) # Print out type of np_baseball
print(type(np_baseball))

2、利用 Numpy 进行整体数学运算

example - 1:

# height is available as a regular list

# Import numpy
import numpy as np # Create a numpy array from height: np_height
np_height = np.array(height) # Print out np_height
print(np_height) # Convert np_height to m: np_height_m
np_height_m = np_height * 0.0254 # Print np_height_m
print(np_height_m)

example - 2:

# height and weight are available as a regular lists

# Import numpy
import numpy as np # Create array from height with correct units: np_height_m
np_height_m = np.array(height) * 0.0254 # Create array from weight with correct units: np_weight_kg
np_weight_kg = np.array(weight) * 0.453592 # Calculate the BMI: bmi
bmi = np_weight_kg / np_height_m ** 2 # Print out bmi
print(bmi)

3、Subset of Numpy array

# height and weight are available as a regular lists

# Import numpy
import numpy as np # Calculate the BMI: bmi
np_height_m = np.array(height) * 0.0254
np_weight_kg = np.array(weight) * 0.453592
bmi = np_weight_kg / np_height_m ** 2 # Create the light array
light = bmi < 21 # Print out light
print(light) # Print out BMIs of all baseball players whose BMI is below 21
print(bmi[light])

这种取子集的方式整体上看起来很自然,但是让我不解的是:为什么 bmi < 21 不直接返回一个子集呢?稍微思考了一下,bmi < 21 本身也是一个类似与 np_array1 < np_array2 的整体数学运算,返回值显然必须是一个布尔型的 np_array3

另外,我发现直接把一个布尔数组放进“[ ]”中取子集本身也非常巧妙、自然。

虽然 NumPy Array 很有“个性”,但是仍具备很多和 Python list 一样的共性:

# height and weight are available as a regular lists

# Import numpy
import numpy as np # Store weight and height lists as numpy arrays
np_weight = np.array(weight)
np_height = np.array(height) # Print out the weight at index 50
print(np_weight[50]) # Print out sub-array of np_height: index 100 up to and including index 110
print(np_height[100:111])

4、Numpy 的副作用(NumPy Side Effects)

First of all, numpy arrays cannot contain elements with different types. If you try to build such a list, some of the elements' types are changed to end up with a homogeneous list. This is known as type coercion.

Second, the typical arithmetic operators, such as +-* and / have a different meaning for regular Python lists and numpy arrays.

Python笔记 #06# NumPy Basis & Subsetting NumPy Arrays的更多相关文章

  1. python笔记06

    python笔记06 数据类型 上个笔记内容补充 补充 列表 reverse,反转. v1 = [1,2,3111,32,13] print(v1) v1.reverse() print(v1) v1 ...

  2. 我的python笔记06

    面向对象学习 本节内容:   面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法.     引子 你现在是一家游戏公司的开发人员,现在需要你开发一款叫做< ...

  3. Python笔记 #07# NumPy 文档地址 & Subsetting 2D Arrays

    文档地址:np.array() 1.<class 'numpy.ndarray'> ndarray 表示 n 维度(n D)数组 (= n 行数组). 2.打印 array 结构 —— n ...

  4. python学习笔记(三):numpy基础

    Counter函数可以对列表中数据进行统计每一个有多少种 most_common(10)可以提取前十位 from collections import Counter a = ['q','q','w' ...

  5. Intro to Python for Data Science Learning 6 - NumPy

    NumPy From:https://campus.datacamp.com/courses/intro-to-python-for-data-science/chapter-4-numpy?ex=1 ...

  6. python数据分析系列(2)--numpy

    NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...

  7. python numPy模块 与numpy里的数据类型、数据类型对象dtype

    学习链接:http://www.runoob.com/numpy/numpy-tutorial.html 官方链接:https://numpy.org/devdocs/user/quickstart. ...

  8. python 中range numpy.arange 和 numpy.linspace 的区别

    1.返回值不同 range返回一个range对象,numpy.arange和numpy.linspace返回一个数组. 2.np.arange的步长可以为小数,但range的步长只能是整数. 与Pyt ...

  9. Python的工具包[0] -> numpy科学计算 -> numpy 库及使用总结

    NumPy 目录 关于 numpy numpy 库 numpy 基本操作 numpy 复制操作 numpy 计算 numpy 常用函数 1 关于numpy / About numpy NumPy系统是 ...

随机推荐

  1. 图片上传根据stream生成image

    对于图片上传代码的整合 因为需要判断上传的图片的宽高是否符合尺寸,所以在最初拿到inputstream的时候,就直接获取image格式的图片 本来是想在下面的checkFile中获取的,不过直接使用S ...

  2. poj_1151 线段树

    题目大意 在平面上给定n个矩形,可以相互覆盖全部或者部分,求出矩形占据的总面积. 题目分析 将矩形按照x方向的进行分割之后,将平面沿着y方向划分一系列单元(不定高度),每个矩形在y方向上占据若干连续的 ...

  3. django 自定模板标签的注册

    首先注册方法一般都是先实例化一个template.Library.如: from django import template register = template.Library() 1.注册自定 ...

  4. LeetCode——Reverse Linked List

    反转链表,用了一个比较笨的方法. public class Solution { public ListNode reverseList(ListNode head) { if(head == nul ...

  5. IPMI相关漏洞利用及WEB端默认口令登录漏洞

    IPMI相关漏洞 0套件漏洞 使用0套件时,只需要Username,口令任意即可绕过身份鉴别执行指令.而且一般还有一个默认的账户admin或者ADMIN. 备注:IPMI是一套主机远程管理系统,可以远 ...

  6. Inflater与findViewById()区别

    /** * Inflater英文意思是膨胀,在Android中应该是扩展的意思吧. LayoutInflater的作用类似于 * findViewById(),不同点是LayoutInflater是用 ...

  7. MYSQL-max_binlog_cache_size参数

    max_binlog_cache_size 解释:这是设置最大二进制日志的缓存区大小的变量.若处理多语句事务时需要的内存大小比设置值大的话就会提示一个error:Multi-statement tra ...

  8. 微信小程序 --- https请求

    wx.request发起的是 https 请求,而不是 http 请求.一个小程序 同时 只能有 5个 网络请求. 参数: url:开发者服务器接口地址: data:请求的参数: header:设置请 ...

  9. jQuery Ajax 全解析(转载)

    本文地址: jQuery Ajax 全解析 本文作者:QLeelulu 转载请标明出处! jQuery确实是一个挺好的轻量级的JS框架,能帮助我们快速的开发JS应用,并在一定程度上改变了我们写Java ...

  10. mysql 选择优化的数据类型

    选择最小的数据类型,因为它们占更少的磁盘,内存和CPU缓存: 选择简单的数据类型,如用整型来存储ip: http://blog.csdn.net/lyd518/article/details/2070 ...