为什么要用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的更多相关文章

  1. Python 将numpy array由浮点型转换为整型

    Python 将numpy array由浮点型转换为整型 ——使用numpy中的astype()方法可以实现,如:

  2. 【python】numpy array特殊数据统一处理

    array中的某些数据坏掉,想要统一处理,找到了这个方法,做个笔记. 比如,把数组中所有小于0的数字置为0 import numpy as np t = np.array([-2, -1, 0, 1, ...

  3. python 中 numpy array 中的维度

    简介 numpy 创建的数组都有一个shape属性,它是一个元组,返回各个维度的维数.有时候我们可能需要知道某一维的特定维数. 二维情况 >>> import numpy as np ...

  4. [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 ...

  5. [Python Cookbook] Numpy Array Joint Methods: Append, Extend & Concatenate

    数组拼接方法一 思路:首先将数组转成列表,然后利用列表的拼接函数append().extend()等进行拼接处理,最后将列表转成数组. 示例1: import numpy as np a=np.arr ...

  6. [Python Cookbook] Numpy Array Manipulation

    1. Reshape: The np.reshape() method will give a new shape to an array without changing its data. Not ...

  7. python numpy array 的一些问题

    1 将list转换成array 如果list的嵌套数组是不规整的,如 a = [[1,2], [3,4,5]] 则a = numpy.array(a)之后 a的type是ndarray,但是a中得元素 ...

  8. 「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: ...

  9. Python Numpy Array

    Numpy 是Python中数据科学中的核心组件,它给我们提供了多维度高性能数组对象. Arrays Numpy.array   dtype 变量 dtype变量,用来存放数据类型, 创建数组时可以同 ...

随机推荐

  1. [转帖]NSA武器库知识整理

    NSA武器库知识整理 https://www.cnblogs.com/FrostDeng/p/7120812.html 美国国家安全局(NSA)旗下的“方程式黑客组织”(shadow brokers) ...

  2. 安卓学习日记第二天——Fragment

    一.基本概念 Fragment是依赖于Activity的,不能独立存在的. 一个Activity里可以有多个Fragment. 一个Fragment可以被多个Activity重用. Fragment有 ...

  3. JQuery高级(二)

    3. 事件绑定 1. jquery标准的绑定方式 * jq对象.事件方法(回调函数): * 注:如果调用事件方法,不传递回调函数,则会触发浏览器默认行为. * 表单对象.submit();//让表单提 ...

  4. day51——对象、BOM对象、DOM对象

    day51 JSON对象 var a = {'name':'太白','age':89}; 序列化:var b = JSON.stringify(a); 反序列化:var c = JSON.parse( ...

  5. Django框架(十二)-- 中间件、CSRF跨站请求伪造

    中间件 一.什么是中间件 请求的时候需要先经过中间件才能到达django后端(urls,views,templates,models) 响应的时候也需要经过中间件才能到达web服务网关接口 djang ...

  6. LeetCode第154场周赛(Java)

    估计要刷很久才能突破三道题了.还是刷的太少.尽管对了前两题,但是我觉得写的不怎么样.还是将所有题目都写一下吧. 5189. "气球" 的最大数量 题目比较简单.就是找出一个字符串中 ...

  7. 使用Filezilla Server配置FTP服务器

    一.下载Filezilla  Server 官网网址:https://filezilla-project.org 二.安装Filezilla  Server   Filezilla  Server的安 ...

  8. Python进阶(十一)----包,logging模块

    Python进阶(十一)----包,logging模块 一丶包的使用 什么是包: ​ 包是通过使用 .模块名的方式组织python模块名称空间的方式. 通俗来说,含有一个__init__.py文件的文 ...

  9. GoogleMap增加标记和路线轨迹的方法

    声明:本文基于JavaScript环境编写. 前言 按照目前的项目需求,我们需要在谷歌地图上标记出当前仓库的位置.司机补货的行车路径.司机当前班次需要补货的机器的位置,同时根据补货状态的不同标记成不同 ...

  10. token jwt配置

    1. token jwt配置 1.1. pom <!-- token验证 --> <dependency> <groupId>io.jsonwebtoken< ...