为什么要用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. Math 类

    Math 类 java.lang.Object java.lang.Math 方法签名 public final class Math extends Object public static fin ...

  2. Graph machine learning 工具

    OGB: Open Graph Benchmark https://ogb.stanford.edu/ https://github.com/snap-stanford/ogb OGB is a co ...

  3. PHP 去一定范围随机小数 随机浮点数

    例如取2到3中的 随机小数(一位)或整数 mt_rand(20,30)/10 mt_rand()是随机取整函数 先扩大一定倍数,再缩小相应倍数,倍数代表精确到哪一位

  4. Django框架之第四篇(视图层)--HttpRequest对象、HttpResponse对象、JsonResponse、CBV和FBV、文件上传

    视图层 一.视图函数 一个视图函数,简称视图,是一个简单的python函数,它接收web请求并且会返回web响应.响应可以是一张网页的html,一个重定向,或者是一张图片...任何东西都可以.无论是什 ...

  5. python3 安装 pyinstaller 时报错的解决办法

    如上图所示,在安装的过程中发现是所关联的一个 future模块安装失败,庵后我有单独安装了一下这个future,发现还是失败 然后在网上寻找解决办法,最后找到了这个指令,pip install fut ...

  6. golang ---常用函数:make

    简介 内建函数 make 用来为 slice,map 或 chan 类型分配内存和初始化一个对象(注意:只能用在这三种类型上) slice // 长度为5,容量为10的slice,slice中的元素是 ...

  7. 五 查询数据SELECT   一、单表查询

    一 单表查询的语法 二 关键字的执行优先级 三 简单查询 四 WHERE约束 五 分组查询:GROUP BY 六 HAVING过滤 七 查询排序:ORDER BY 八 限制查询的记录数:LIMIT 九 ...

  8. CSS疑难杂症

    1.text-align: center + letter-spacing: 2em 字体不居中 办法:添加text-indent: 2em 2.first-child伪类选择不到元素 办法:确保备选 ...

  9. 【转】Flex 布局教程:语法篇

    作者: 阮一峰 日期: 2015年7月10日 网页布局(layout)是 CSS 的一个重点应用. 布局的传统解决方案,基于盒状模型,依赖 display 属性 + position属性 + floa ...

  10. FreeRTOS软件定时器

    API函数 //创建 TimerHandle_t xTimerCreate( const char * const pcTimerName, const TickType_t xTimerPeriod ...