Numpy 中数组上的算术运算符使用元素级别。最后的结果使用新的一个数组来返回。

import numpy as np
a = np.array( [20,30,40,50] )
b = np.arange(4)
b
Out[113]: array([0, 1, 2, 3])
c = a -b
c
Out[114]: array([20, 29, 38, 47])
b ** 2
Out[115]: array([0, 1, 4, 9], dtype=int32)
a < 34
Out[116]: array([ True, True, False, False])

 需要注意的是,乘法运算符*的运算在NumPy数组中也是元素级别的(这与许多矩阵语言不同)。如果想要执行矩阵乘积,可以使用dot函数:

A = np.array( [[1,1], [0,1]] )
B = np.array( [[2,0], [3,4]] )
A
Out[117]:
array([[1, 1],
[0, 1]])
B
Out[118]:
array([[2, 0],
[3, 4]])
A * B
Out[119]:
array([[2, 0],
[0, 4]])
A.dot(B)
Out[120]:
array([[5, 4],
[3, 4]])
np.dot(A,B)
Out[121]:
array([[5, 4],
[3, 4]])

  某些操作(如+=*=)可以修改现有数组,而不是创建新数组。

a = np.ones((2,3), dtype=np.int32)
a *= 3
a
Out[122]:
array([[3, 3, 3],
[3, 3, 3]])
b = np.random.random((2,3))
b
Out[124]:
array([[0.39895014, 0.30638211, 0.9011525 ],
[0.6135912 , 0.02488626, 0.67726569]])
a.dtype
Out[125]: dtype('int32')
b.dtype
Out[126]: dtype('float64')
b += a
b
Out[128]:
array([[3.39895014, 3.30638211, 3.9011525 ],
[3.6135912 , 3.02488626, 3.67726569]])
a += b
Traceback (most recent call last):
File "D:\pytho3.6\lib\site-packages\IPython\core\interactiveshell.py", line 2963, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-129-294cacd62d6f>", line 1, in <module>
a += b
TypeError: Cannot cast ufunc add output from dtype('float64') to dtype('int32') with casting rule 'same_kind' 

当使用不同类型的数组操作时,结果数组的类型对应于更一般或更精确的数组(称为向上转换的行为)。

由于定义 a时,数据类型指定为np.int32,而 a+b 生成的数据类型为 np.float64,所以自动转换出错。

around

np.around 返回四舍五入后的值,可指定精度。

around(a, decimals=0, out=None)

输入数组

decimals 要舍入的小数位数。 默认值为0。 如果为负,整数将四舍五入到小数点左侧的位置

import numpy as np

n = np.array([-0.746, 4.6, 9.4, 7.447, 10.455, 11.555])

around1 = np.around(n)
print(around1) # [ -1. 5. 9. 7. 10. 12.] around2 = np.around(n, decimals=1)
print(around2) # [ -0.7 4.6 9.4 7.4 10.5 11.6] around3 = np.around(n, decimals=-1)
print(around3) # [ -0. 0. 10. 10. 10. 10.]

floor

np.floor 返回不大于输入参数的最大整数。 即对于输入值 x ,将返回最大的整数 i ,使得 i <= x。 注意在Python中,向下取整总是从 0 舍入。

import numpy as np
n = np.array([-1.7, -2.5, -0.2, 0.6, 1.2, 2.7, 11])
floor = np.floor(n)
print(floor) # [ -2. -3. -1. 0. 1. 2. 11.]
[-2. -3. -1. 0. 1. 2. 11.]

ceil

np.ceil 函数返回输入值的上限,即对于输入 x ,返回最小的整数 i ,使得 i> = x。

import numpy as np

n = np.array([-1.7, -2.5, -0.2, 0.6, 1.2, 2.7, 11])

ceil = np.ceil(n)
print(ceil) # [ -1. -2. -0. 1. 2. 3. 11.]

np.where

numpy.where(condition[, x, y])

根据 condition 从 x 和 y 中选择元素,当为 True 时,选 x,否则选 y。

https://docs.scipy.org/doc/numpy/reference/generated/numpy.where.html

import numpy as np
data = np.random.random([2, 3])
print(data)
[[0.37618029 0.09114803 0.12294256]
[0.59006572 0.18597964 0.46023678]]
result = np.where(data > 0.5, data, 0)
print(result)
[[0. 0. 0. ]
[0.59006572 0. 0. ]]

Numpy系列(三)- 基本运算操作的更多相关文章

  1. Numpy数组的基本运算操作

    一.算术运算符 In [3]: a = np.arange(0,5) Out[3]array([0, 1, 2, 3, 4]) In [4]: a+4 Out[4]: array([4, 5, 6, ...

  2. 5.3Python数据处理篇之Sympy系列(三)---简化操作

    目录 5.3简化操作 目录 前言 (一)有理数与多项式的简化 1.最简化-simplify() 2.展开-expand() 3.提公因式-factor() 4.合并同类项-ceiling() 5.简化 ...

  3. 3.3Python数据处理篇之Numpy系列(三)---数组的索引与切片

    目录 (一)数组的索引与切片 1.说明: 2.实例: (二)多维数组的索引与切片 1.说明: 2.实例: 目录: 1.一维数组的索引与切片 2.多维数组的索引与切片 (一)数组的索引与切片 1.说明: ...

  4. oracle系列(三)表操作基础

    支持的数据类型: 字符型char 定长 最大2000varchar2() 变长 最大4000clob 字符型大对象 最大4G 数字型number范围 -10的38次方到10的+38次方;number( ...

  5. DocX开源WORD操作组件的学习系列三

    DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...

  6. .net reactor 学习系列(三)---.net reactor代码自动操作相关保护功能

    原文:.net reactor 学习系列(三)---.net reactor代码自动操作相关保护功能         接上篇,上篇已经学习了界面的各种功能以及各种配置,这篇准备学习下代码控制许可证. ...

  7. MySQL并发复制系列三:MySQL和MariaDB实现对比

    http://blog.itpub.net/28218939/viewspace-1975856/ 并发复制(Parallel Replication) 系列三:MySQL 5.7 和MariaDB ...

  8. SQL Server 2008空间数据应用系列三:SQL Server 2008空间数据类型

    原文:SQL Server 2008空间数据应用系列三:SQL Server 2008空间数据类型 友情提示,您阅读本篇博文的先决条件如下: 1.本文示例基于Microsoft SQL Server ...

  9. 系列三VisualSvn Server

    原文:系列三VisualSvn Server VisualSvn Server介绍 1 .VisualSvn Server  VisualSvn Server是免费的,而VisualSvn是收费的.V ...

  10. java‘小秘密’系列(三)---HashMap

    java'小秘密'系列(三)---HashMap java基础系列 java'小秘密'系列(一)---String.StringBuffer.StringBuilder java'小秘密'系列(二)- ...

随机推荐

  1. android SDK 无法更新

    android-windows-sdk无法更新解决办法:   1.在host文件新增如下配置 (host文件位置:c:\Windows\System32\drivers\etc文件夹下面,用文本编辑器 ...

  2. RH2288V3服务器 硬件安装以及更换硬件

    滑道类型:L型滑道.可伸缩滑道.报轨 L型滑道:只适用于华为机柜. 准备工具:防静电带或者防静电手套.螺丝刀.浮动螺母安装条. 安装服务器步骤: 1.安装L型滑道   2.安装浮动螺母  3.安装服务 ...

  3. Saltstack_使用指南01_部署

    1. 主机规划 服务器名称 操作系统版本 内网IP 外网IP(模拟) Hostname 部署模块 salt100 CentOS7.5 172.16.1.100 10.0.0.100 salt100 s ...

  4. IDEA包名显示设置

    项目结构视图右上角那个齿轮 选择[Compact Empty Middle Packages],包会合并显示 [Hide Empty Middle Packages]去掉前面的√,不分层级显示

  5. Cocos2d-x游戏开发之lua编辑器 Sublime 搭建,集成cocos2dLuaApi和自有类

    版权声明:本文为博主原创文章.未经博主同意不得转载. https://blog.csdn.net/wisdom605768292/article/details/34085969 Sublime Te ...

  6. Golang 入门系列(二)学习Go语言需要注意的坑

    上一章节我们已经了解了 Go 环境的配置,不了解的,请查看前面的文章 https://www.cnblogs.com/zhangweizhong/p/9459945.html,本章节我们将学习 Go ...

  7. 基于C#的socket编程的TCP异步实现

    一.摘要 本篇博文阐述基于TCP通信协议的异步实现. 二.实验平台 Visual Studio 2010 三.异步通信实现原理及常用方法 3.1 建立连接 在同步模式中,在服务器上使用Accept方法 ...

  8. windows下redis安装及配置

    1.简介: redis是一个高性能的key-value数据库:redis能读的速度为11万次/秒,写的速度是8.1万次/秒 redis支持丰富的数据类型:String, List, Hash(map) ...

  9. linux下开启、关闭、重启mysql服务命令

    一. 启动1.使用 service 启动:service mysql start2.使用 mysqld 脚本启动:/etc/inint.d/mysql start3.使用 safe_mysqld 启动 ...

  10. 如何搭建SVN的客户端和使用

    1.下载安装TortoiseSVN 首先我们需要从官方网站下载TortoiseSVN客户端工具 可以选择32位和64位.也可以直接使用搜索引擎搜索TortoiseSVN 也会出现直接的下载方式.这里不 ...