python的numpy.array
为什么要用numpy
Python中提供了list容器,可以当作数组使用。但列表中的元素可以是任何对象,因此列表中保存的是对象的指针,这样一来,为了保存一个简单的列表[1,2,3]。就需要三个指针和三个整数对象。对于数值运算来说,这种结构显然不够高效。
Python虽然也提供了array模块,但其只支持一维数组,不支持多维数组(在TensorFlow里面偏向于矩阵理解),也没有各种运算函数。因而不适合数值运算。
NumPy的出现弥补了这些不足。
(——摘自张若愚的《Python科学计算》)
import numpy as np
数组创建
## 常规创建方法
a = np.array([2,3,4])
b = np.array([2.0,3.0,4.0])
c = np.array([[1.0,2.0],[3.0,4.0]])
d = np.array([[1,2],[3,4]],dtype=complex) # 指定数据类型
print a, a.dtype
print b, b.dtype
print c, c.dtype
print d, d.dtype
[2 3 4] int32
[ 2. 3. 4.] float64
[[ 1. 2.]
[ 3. 4.]] float64
[[ 1.+0.j 2.+0.j]
[ 3.+0.j 4.+0.j]] complex128

数组的常用函数
print np.arange(0,7,1,dtype=np.int16) # 0为起点,间隔为1时可缺省(引起歧义下不可缺省)
print np.ones((2,3,4),dtype=np.int16) # 2页,3行,4列,全1,指定数据类型
print np.zeros((2,3,4)) # 2页,3行,4列,全0
print np.empty((2,3)) #值取决于内存
print np.arange(0,10,2) # 起点为0,不超过10,步长为2
print np.linspace(-1,2,5) # 起点为-1,终点为2,取5个点
print np.random.randint(0,3,(2,3)) # 大于等于0,小于3,2行3列的随机整数
[0 1 2 3 4 5 6]
[[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]
[[1 1 1 1]
[1 1 1 1]
[1 1 1 1]]]
[[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]]
[[ 1.39069238e-309 1.39069238e-309 1.39069238e-309]
[ 1.39069238e-309 1.39069238e-309 1.39069238e-309]]
[0 2 4 6 8]
[-1. -0.25 0.5 1.25 2. ]
[[1 0 1]
[0 1 0]]
类型转换
print float(1)
print int(1.0)
print bool(2)
print float(True)
1.0
1
True
1.0
数组输出
从左到右,从上向下
一维数组打印成行,二维数组打印成矩阵,三维数组打印成矩阵列表
print np.arange(1,6,2)
print np.arange(12).reshape(3,4) # 可以改变输出形状
print np.arange(24).reshape(2,3,4)# 2页,3行,4页
[1 3 5]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
基本运算
## 元素级运算
a = np.array([1,2,3,4])
b = np.arange(4)
print a, b
print a-b
print a*b
print a**2
print 2*np.sin(a)
print a>2
print np.exp(a) # 指数
[1 2 3 4] [0 1 2 3]
[1 1 1 1]
[ 0 2 6 12]
[ 1 4 9 16]
[ 1.68294197 1.81859485 0.28224002 -1.51360499]
[False False True True]
[ 2.71828183 7.3890561 20.08553692 54.59815003]
## 矩阵运算(二维数组)
a = np.array([[1,2],[3,4]]) # 2行2列
b = np.arange(6).reshape((2,-1)) # 2行3列
print a,b
print a.dot(b) # 2行3列

原文:https://blog.csdn.net/fu6543210/article/details/83240024
python的numpy.array的更多相关文章
- Python 将numpy array由浮点型转换为整型
Python 将numpy array由浮点型转换为整型 ——使用numpy中的astype()方法可以实现,如:
- 【python】numpy array特殊数据统一处理
array中的某些数据坏掉,想要统一处理,找到了这个方法,做个笔记. 比如,把数组中所有小于0的数字置为0 import numpy as np t = np.array([-2, -1, 0, 1, ...
- python 中 numpy array 中的维度
简介 numpy 创建的数组都有一个shape属性,它是一个元组,返回各个维度的维数.有时候我们可能需要知道某一维的特定维数. 二维情况 >>> import numpy as np ...
- [Python Cookbook] Numpy Array Slicing and Indexing
1-D Array Indexing Use bracket notation [ ] to get the value at a specific index. Remember that inde ...
- [Python Cookbook] Numpy Array Joint Methods: Append, Extend & Concatenate
数组拼接方法一 思路:首先将数组转成列表,然后利用列表的拼接函数append().extend()等进行拼接处理,最后将列表转成数组. 示例1: import numpy as np a=np.arr ...
- [Python Cookbook] Numpy Array Manipulation
1. Reshape: The np.reshape() method will give a new shape to an array without changing its data. Not ...
- python numpy array 的一些问题
1 将list转换成array 如果list的嵌套数组是不规整的,如 a = [[1,2], [3,4,5]] 则a = numpy.array(a)之后 a的type是ndarray,但是a中得元素 ...
- 「Python」Convert map object to numpy array in python 3
转自Stackoverflow.备忘用. Question In Python 2 I could do the following: import numpy as np f = lambda x: ...
- Python Numpy Array
Numpy 是Python中数据科学中的核心组件,它给我们提供了多维度高性能数组对象. Arrays Numpy.array dtype 变量 dtype变量,用来存放数据类型, 创建数组时可以同 ...
随机推荐
- es内存不够
8核32G 3台 ELS机器,当时只给es配置了8G内存,es的总体数据量大小将近30G(查看GET /_cat/shards?v)
- Windows下安装gcc环境
安装GCC环境 https://gcc.gnu.org/ 点进去后 然后 然后 点击 再点击 点击 (啊,这是跳了多少个页面) 开始下载了.完成之后打开:(自动执行的) 弹出 点击OK,弹出个窗口,让 ...
- MySQL恩恩怨怨
数据库基础 Windows安装MySQL Mac安装MySQL Linux安装MySQL MySQL存储引擎概述 MySQL表操作 MySQL支持的数据类型 MySQL表的完整性约束 MySQL记录操 ...
- 线程池及Executor框架
1.为什么要使用线程池? 诸如 Web 服务器.数据库服务器.文件服务器或邮件服务器之类的许多服务器应用程序都面向处理来自某些远程来源的大量短小的任务.请求以某种方式到达服务器,这种方式可能是通过 ...
- 利用HashMap计算一个字符串中每个字符出现的次数
问题描述:计算一个字符串中每个字符出现的次数 问题分析:每个字符串对应着它的次数,且字符串唯一不重复,这让我们想到了HashMap中的键值对. 1.使用Scanner获取字符串 2.遍历字符串,获取每 ...
- div中的“内容”水平垂直居中
1. div高度自适应的情况 div在不设置高度的时候,会被里面的内容撑开,内容自动填充在div中,无论是一行内容还是多行内容,此时不需要设置垂直居中,内容自动在中间的, 想要看的更直观些,只需要加上 ...
- data:image/png;base64应用
原文:https://blog.csdn.net/deng_xj/article/details/93731850 data:image/png;base64应用 我们知道任何图片都可以通过base6 ...
- 存储管理器 S3C2440A
CPU通过存储管理器来控制外部设备 SDRAM存储结构 S3C2440A内存控制器 原理图 HY57V561620(L)T 4Banks x 4M x 16Bit Synchronous DRAM S ...
- 笔谈kxmovie开源播放器库的使用
开源播放器项目 kxmovie(https://github.com/kolyvan/kxmovie),现在仍然是很多刚开始接触播放器开发的程序员的参照范本.以下是我操作kxmovie项目的过程: ( ...
- VLAN实验4:利用单臂路由实现VLAN间路由
单臂路由: 实验环境: 实验拓扑图: 实验编址: 实验步骤:1.创建VLAN并配置Access.Trunk接口. 我们在S2上创建VLAN10和VLAN20,并且吧链接PC1的E0/0/1和链接PC2 ...