Python 笔记 #06# NumPy Basis & Subsetting NumPy Arrays
2018-01-10 23:58
465 查看
原始的 Python list 虽然很好用,但是不具备能够“整体”进行数学运算的性质,并且速度也不够快(按照视频上的说法),而 Numpy.array 恰好可以弥补这些缺陷。
初步应用就是“整体数学运算”和“subset(取子集、随机访问)”。
example - 2:
这种取子集的方式整体上看起来很自然,但是让我不解的是:为什么 bmi < 21 不直接返回一个子集呢?稍微思考了一下,bmi < 21 本身也是一个类似与 np_array1 < np_array2 的整体数学运算,返回值显然必须是一个布尔型的 np_array3
另外,我发现直接把一个布尔数组放进“[ ]”中取子集本身也非常巧妙、自然。
虽然 NumPy Array 很有“个性”,但是仍具备很多和 Python list 一样的共性:
Second, the typical arithmetic operators, such as
初步应用就是“整体数学运算”和“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,numpyarrays 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
numpyarrays.
相关文章推荐
- 《python for data analysis》笔记三--Numpy基础:arrays和向量化计算2
- 《python for data analysis》笔记二--Numpy 基础:arrays 和向量化计算 1
- 【python学习笔记】15:numpy创建各种数组
- python:numpy 学习笔记
- python——numpy包的一些功能笔记
- 【学习笔记】WEEK2_Programming Assignments_Python Basics with numpy (optional)
- python numpy学习笔记(4)
- Python Numpy学习笔记
- python学习笔记1-numpy/enumerate
- python numpy 使用笔记 矩阵操作
- python subprocess模块 学习笔记
- [python自学笔记]windows环境下安装Python和numpy,Matplotlib库
- 机器学习笔记--Python之Numpy
- Python数据分析--Numpy部分笔记
- python笔记 使用numpy中的savez(save)保存非数组数据
- Python学习笔记(二)——NumPy
- Python 笔记 #07# NumPy 文档地址 & Subsetting 2D Arrays
- 利用Python进行数据分析笔记-第四章Numpy
- Python Numpy 学习笔记(3)
- Python数据处理pandas、numpy等第三方库函数笔记(持续更新)