原始的 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. SecureCRT 详细使用图文教程(按步骤)

    一.安装和激活 安装 1.在http://www./softs/55471.html下载相关包激活工具,运行scrt622-x86.exe. 2.进入安装向导程序,默认下一步,直到安装完成既可以. 破 ...

  2. ajax如何上传文件

    PHP: <?php /** * Created by PhpStorm. * User: DELL * Date: 2017/11/23 * Time: 10:57 */ header(&qu ...

  3. Linux下多任务间通信和同步-mmap共享内存

    Linux下多任务间通信和同步-mmap共享内存 嵌入式开发交流群280352802,欢迎加入! 1.简介 共享内存可以说是最有用的进程间通信方式.两个不用的进程共享内存的意思是:同一块物理内存被映射 ...

  4. BNU4206:单向行走

    给定一个m*n的矩阵,请写一个程序计算一条从左到右走过矩阵且权和最小的路径.一条路径可以从第1列的任意位置出发,到达第n列的任意位置.每一步只能从第i列走到第i+1列的同一行或者相邻行(第一行和最后一 ...

  5. The 70th problem,UVa10396 Vampire Numbers

    今天看Thinking in Java看到一个吸血鬼数的问题,于是查找UVa里也有类似的问题就动手写了先是用Java写的,不过WA了两次,然后没有发现错误,又用c++写的还是不行.最后发现要排序去重. ...

  6. Android 测量调用方法耗时的方法

    private long mDrawnRealTime; 起点:mDrawnRealTime = SystemClock.elapsedRealtime(); 终点: long delay = Sys ...

  7. Request.getRequestURL

    getRequestURI()就相当于你在写一个JSP页面的时候会有这样的东西"action='/WebRoot/xxx'"这个方法就是获得'/WebRoot/xxx',也就是说它 ...

  8. Linux数据链路层的包解析

    仅以此文作为学习笔记,初学者,如有错误欢迎批评指正,但求轻喷.一般而言,Linux系统截获数据包后,会通过协议栈,按照TCP/IP层次进行解析,那我们如何直接获得更为底层的数据报文呢,这里用到一个类型 ...

  9. 常用的jq插件

    1.时间版翻转 FlipClock-master 2.整屏切换  fullpage 3.轮播图 swiper 4.滚动条样式 mCustomScrollbar 5.过滤和排序布局插件 Isotope. ...

  10. 部署软件RDMA的步骤

    date:  2018-08-28   19:46:56 参考原文原文:http://corasql.blog.51cto.com/5908329/1930455                    ...