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系统是 ...
随机推荐
- ubuntu安装TexturePicker
TexturePacker网页:https://www.codeandweb.com/texturepackerTexturePacker下载页面:https://www.codeandweb.com ...
- 在fedora 18中将普通用户设置为sudo权限
将一般的用户加入sudo组is not in the sudoers file. This incident will be reported 解决方法 在一般用户下执行sudo命令提示xxx is ...
- LeetCode——Move Zeroes
Description: Given an array nums, write a function to move all 0's to the end of it while maintainin ...
- 2.实现官网环境, 搭建HTTP服务器
1.建立 HTTP 服务器 Node.js 是为网络而诞生的平台,但又与 ASP.PHP 有很大的不同,究竟不同在哪里呢?如果你有 PHP 开发经验,会知道在成功运行 PHP 之前先要配置一个功能强大 ...
- Unity3D笔记九 发送广播与消息、利用脚本控制游戏
一.发送广播与消息 游戏对象之间发送的广播与消息分为三种:第一种向子对象发送,将发送至该对象的同辈对象或者子孙对象中:第二种为给自己发送,发送至自己本身对象:第三种为向父对象发送,发送至该对象的同辈或 ...
- 解读 Android TTS 语音合成播报
随着从事 Android 开发年限增加,负责的工作项目也从应用层开发逐步过渡到 Android Framework 层开发.虽然一开始就知道 Android 知识体系的庞大,但是当你逐渐从 Appli ...
- EasyUI DataGrid 时间格式化、字符串长度截取
需要格式化日期时间和标题的方法,显示如下: 日期:2017-03-03 时间:2017-03-0 11:11 标题:标题名称 <table id="tbList" style ...
- 百度输入法引起的Mac远程桌面Ctrl+.快捷键不起作用
被这个问题困扰已久!在Mac中通过远程桌面(Remote Desktop)连接至Windows服务器时,Ctrl+.快捷键不起作用,而这是用Visual Studio写代码时常用的快捷键(对应的命令是 ...
- jquery tab选项卡、轮播图、无缝滚动
最近做一个页写了一个星期,觉得自己对jquery还是很不熟悉 自己查了一下资料写了几个封装好的tab选项卡.轮播图.无缝滚动 $(function(){ //tab选项卡 jQuery.tab=fun ...
- 拨开障目的叶,一览CMDB庐山真面目
人们往往用"一叶障目,不见泰山"来形容一个人被局部现象所迷惑,看不到事物发展的整体脉络,从而做出一些不是十分正确的决策.小编觉得对于运维何尝不是这样呢. 大多数企业资产配置维护的现 ...