Python笔记 #06# NumPy Basis & Subsetting NumPy Arrays
原始的 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的更多相关文章
- python笔记06
python笔记06 数据类型 上个笔记内容补充 补充 列表 reverse,反转. v1 = [1,2,3111,32,13] print(v1) v1.reverse() print(v1) v1 ...
- 我的python笔记06
面向对象学习 本节内容: 面向对象编程介绍 为什么要用面向对象进行开发? 面向对象的特性:封装.继承.多态 类.方法. 引子 你现在是一家游戏公司的开发人员,现在需要你开发一款叫做< ...
- Python笔记 #07# NumPy 文档地址 & Subsetting 2D Arrays
文档地址:np.array() 1.<class 'numpy.ndarray'> ndarray 表示 n 维度(n D)数组 (= n 行数组). 2.打印 array 结构 —— n ...
- python学习笔记(三):numpy基础
Counter函数可以对列表中数据进行统计每一个有多少种 most_common(10)可以提取前十位 from collections import Counter a = ['q','q','w' ...
- 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 ...
- python数据分析系列(2)--numpy
NumPy(Numerical Python的简称)是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础. NumPy的部分功能如下: ndarray,一个具 ...
- python numPy模块 与numpy里的数据类型、数据类型对象dtype
学习链接:http://www.runoob.com/numpy/numpy-tutorial.html 官方链接:https://numpy.org/devdocs/user/quickstart. ...
- python 中range numpy.arange 和 numpy.linspace 的区别
1.返回值不同 range返回一个range对象,numpy.arange和numpy.linspace返回一个数组. 2.np.arange的步长可以为小数,但range的步长只能是整数. 与Pyt ...
- Python的工具包[0] -> numpy科学计算 -> numpy 库及使用总结
NumPy 目录 关于 numpy numpy 库 numpy 基本操作 numpy 复制操作 numpy 计算 numpy 常用函数 1 关于numpy / About numpy NumPy系统是 ...
随机推荐
- PHP常用必备函数
array_change_key_case — 返回字符串键名全为小写或大写的数组 array_chunk — 将一个数组分割成多个 array_combine — 创建一个数组,用一个数组的值作为其 ...
- PyQt4重写事件处理方法
PyQt中的事件处理主要以来重写事件处理函数来实现. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import Qt ...
- Parquet存储格式 - 论文翻译【转】
Apache Parquet是Hadoop生态圈中一种新型列式存储格式,它可以兼容Hadoop生态圈中大多数计算框架(Mapreduce.Spark等),被多种查询引擎支持(Hive.Impala.D ...
- poj_3662 最小化第k大的值
题目大意 有N个节点以及连接的P个无向边,现在要通过这P条边从1号节点连接到N号节点.若无法连接成功,则返回-1:若能够连接成功,那么其中用到了L条边,这L条边中有K条边可以免费,L-K条边不能免费, ...
- 【python系列】SyntaxError:Missing parentheses in call to 'print'
打印python2和python3的区别 如上图所示,我的 PyCharm安装的是python3.6如果使用print 10会出现语法错误,这是python2.x和python3.x的区别所导致的.
- mysql max_allowed_packet参数值改大后,莫名被还原
mysql数据库用innodb引擎,mysql max_allowed_packet在my.cnf中值加大后,够一段时间,系统会莫名把这个参数的值改小. innodb_buffer_pool_size ...
- postgresql----唯一索引,表达式索引,部分索引
一.唯一索引 唯一索引字面上理解就是在索引上增加唯一约束,不允许出现索引值相同的行,目前只有Btree索引可以声明唯一索引,唯一键会自动创建唯一索引. 测试表: test=# create table ...
- oracle的认证方式
使用as sysdba是使用操作系统验证方式,不需要输入密码
- drop user ora-604 ora-54
SQL> drop user EFMIS_87_LK cascade;drop user EFMIS_87_LK cascade*ERROR at line 1:ORA-00604: error ...
- NAT STURN,ICE
NAT原理与NAT穿越 最近在看东西的时候发现很多网络程序中都需要NAT穿越,特意在此总结一下. 先做一个约定: 内网A中有:A1(192.168.0.8).A2(192.168.0.9)两用户 网关 ...