原始的 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. 解决Qt程序在Linux下无法输入中文的办法(与下文连接)

    在安装QT集成开发工具包之前需要先安装build-essential和libncurses5-dev这两个开发工具和库,libncurses5-dev库是一个在Linux/Unix下广泛应用的图形函数 ...

  2. zookeeper-端口说明

    一.zookeeper有三个端口(可以修改) 1.2181 2.3888 3.2888 二.3个端口的作用 1.2181:对cline端提供服务 2.3888:选举leader使用 3.2888:集群 ...

  3. 【IOS6.0 自学瞎折腾】(四)Xib可视化编程

    其实在Interface Builder中,要把xib中的控件与代码联系起来用鼠标拖拉连线是非常方便的一件事,有的教程写的非常复杂要先点这后点那的. 一:IBOutlet,IB说明是Interface ...

  4. poj_2752 kmp

    题目大意 给定字符串S,求出S的所有可能相同前后缀的长度.比如: "alala"的前缀分别为{"a", "al", "ala&qu ...

  5. LeetCode——Search Insert Position

    Description: Given a sorted array and a target value, return the index if the target is found. If no ...

  6. LeetCode——Product of Array Except Self

    Description: Given an array of n integers where n > 1, nums, return an array output such that out ...

  7. LeetCode——Single Number II

    Description: Given an array of integers, every element appears three times except for one. Find that ...

  8. SimpleDateFormat使用详解 <转>

    public class SimpleDateFormat extends DateFormat SimpleDateFormat 是一个以国别敏感的方式格式化和分析数据的具体类. 它允许格式化 (d ...

  9. 关于sencha touch 用phonegap打包后,docked悬停的组件被手机软键盘遮挡的解决方法

    这个问题应该算是phonegap的一个bug,在mainifest.xml 里android:windowSoftInputMode设置成了adjustpan,理论上不会出现遮挡悬停组件这种情况, 不 ...

  10. 【BZOJ2901】矩阵求和

    Description 给出两个n*n的矩阵,m次询问它们的积中给定子矩阵的数值和. Input 第一行两个正整数n,m. 接下来n行,每行n个非负整数,表示第一个矩阵. 接下来n行,每行n个非负整数 ...