美文网首页
numpy -- 基础

numpy -- 基础

作者: 爱修仙的道友 | 来源:发表于2019-04-19 13:58 被阅读0次

1 Numpy

什么是Numpy
数学库,c语言和Python语言混合, NumPy 主要用于大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。

  • 一个强大的 N 维数组对象 ndarray
  • 广播功能函数
  • 整合 C/C++/Fortran 代码的工具
  • 线性代数、傅里叶变换、随机数生成等功能

为什么要学这个

  1. 因为python在做计算时,计算非常慢,可以通过结合c语言进行优化,使得计算加速
  2. NumPy它本身将矩阵封装的更好,提供更多的方法,让我们可以快速计算矩阵,而不用重写方法
  3. Pandas、Matplotlib 都会应用到NumPy

最起码应该知道什么

  1. Array数组:相当于在Python里面使用了c的数组,同时融合了python-list和c-array的优缺点
    (每个元素类型都要一样,数组长度一定)
  2. Creating Arrays
  3. Data Types
  4. Inspecting Your Array
  5. Asking For Help
  6. Array Mathematics
  7. Copying Arrays
  8. Subsetting, Slicing, Indexing
  9. Array Manipulation
  10. Sorting Arrays

详情请参考Numpy_Python_Cheat_Sheet

1.1 Scipy 组织

SciPy 包含的模块有最优化、线性代数、积分、插值、特殊函数、快速傅里叶变换、信号处理和图像处理、常微分方程求解和其他科学与工程中常用的计算。


image.png
  • Matplotlib 是 Python 编程语言及其数值数学扩展包 NumPy 的可视化操作界面。
  • Sympy 符号计算
  • pandas 表格绘制,关于表的存储和应用的最基础的第三方包

1.2 安装

利用 pip 进行一键安装或者 anaconda 安装套件。检测安装是否成功


image.png

1.3 附加 Package

持久化存储 / 序列化与反序列化
h5py:https://www.h5py.org/
Pickle:https://docs.python.org/2.2/lib/module-cPickle.html
以上两者只在python的环境下使用的话,性能无差别,但h5py 可以作分发,分发到不同的语言

1.4 GPU运算

Google colab:https://colab.research.google.com/

2 基本运算

2.1 Ndarray 数据结构

背景
在计算机内存中,由于其本身线性结构的特性,无法存储n维数组等高维数据结构
数据结构
NumPy 最重要的一个特点是其 N 维数组对象 ndarray(张量 Tensor 部分略去,以后详解),它是一系列同类型数据的集合,以 0 下标为开始进行集合中元素的索引。ndarray 对象是用于存放同类型元素的多维数组。ndarray 中的每个元素在内存中都有相同存储大小的区域

image.png
image.png
numpy 支持的数据类型比 Python 内置的类型要多很多,基本上可以和 C 语言的数
据类型对应上,其中部分类型对应为 Python 内置的类型。

2.2 属性

NumPy 的数组中比较重要 ndarray 对象属性有:


image.png

3 基本操作详解
NumPy 数组属性
ndarray.ndim 用于返回数组的维数,等于秩

import numpy as np 
 
a = np.arange(24)  
print (a.ndim)             # a 现只有一个维度
# 现在调整其大小
b = a.reshape(2,4,3)  # b 现在拥有三个维度
print (b.ndim)

------------------------------------------------------
1
3

ndarray.shape 也可以用于调整数组大小

import numpy as np  
 
a = np.array([[1,2,3],[4,5,6]])  
print (a.shape)
------------------------------------------------------
(2, 3)

import numpy as np 
 
a = np.array([[1,2,3],[4,5,6]]) 
b = a.reshape(3,2)  
print (b)
------------------------------------------------------
[[1 2]
 [3 4]
 [5 6]]

NumPy 创建数组
numpy.empty numpy.zeros numpy.ones初始化

import numpy as np 
x = np.empty([3,2], dtype = int) 
print (x)

------------------------------------------------------
[[1 2]
 [3 4]
 [5 6]]
import numpy as np
 
# 默认为浮点数
x = np.zeros(5) 
print(x)
 
# 设置类型为整数
y = np.zeros((5,), dtype = np.int) 
print(y)
 
# 自定义类型
z = np.zeros((2,2), dtype = [('x', 'i4'), ('y', 'i4')])  
print(z)

------------------------------------------------------
[0. 0. 0. 0. 0.]
[0 0 0 0 0]
[[(0, 0) (0, 0)]
 [(0, 0) (0, 0)]]
import numpy as np
 
# 默认为浮点数
x = np.ones(5) 
print(x)
 
# 自定义类型
x = np.ones([2,2], dtype = int)
print(x)

------------------------------------------------------

[1. 1. 1. 1. 1.]
[[1 1]
 [1 1]]

NumPy 从数值范围创建数组

import numpy as np
 
x = np.arange(5)  
print (x)

------------------------------------------------------
[0 1 2 3 4]
import numpy as np
 
# 设置了 dtype
x = np.arange(5, dtype =  float)  
print (x)

------------------------------------------------------
[0. 1. 2. 3. 4.]
import numpy as np
a = np.linspace(1,10,10)
print(a)

------------------------------------------------------
[ 1.  2.  3.  4.  5.  6.  7.  8.  9. 10.]

切片

import numpy as np
 
a = np.arange(10)
print(a[2:])
---------------------------------------------
[2 3 4 5 6 7 8 9]
import numpy as np
 
a = np.arange(10)  # [0 1 2 3 4 5 6 7 8 9]
print(a[2:5])

------------------------------------------------------
[2 3 4]
import numpy as np
 
a = np.array([[1,2,3],[3,4,5],[4,5,6]], dtype=float)
print(a)
print(a[0])
# 从某个索引处开始切割
print('从数组索引 a[1:] 处开始切割')
print(a[1:])
------------------------------------------------------
[[1. 2. 3.]
 [3. 4. 5.]
 [4. 5. 6.]]
[1. 2. 3.]
从数组索引 a[1:] 处开始切割
[[3. 4. 5.]
 [4. 5. 6.]]
import numpy as np
 
a = np.array([[1,2,3],[3,4,5],[4,5,6]])
print (a)
print (a[...,1])   # 第2列元素
print (a[1,...])   # 第2行元素
print (a[...,1:])  # 第2列及剩下的所有元素

------------------------------------------------------
[[1 2 3]
 [3 4 5]
 [4 5 6]]
[2 4 5]
[3 4 5]
[[2 3]
 [4 5]
 [5 6]]
import numpy as np
 
a = np.array([[1,2,3], [4,5,6],[7,8,9]])
b = a[1:3, 1:3]
c = a[1:3,[1,2]]
d = a[...,1:]
print(b)
print(c)
print(d)

------------------------------------------------------
[[5 6]
 [8 9]]
[[5 6]
 [8 9]]
[[2 3]
 [5 6]
 [8 9]]

import numpy as np
 
a = np.array([[1,2,3], [4,5,6],[7,8,9]])
b = a[1:3, 1:3]
c = a[1:3,[1,2]]
d = a[...,1:]
print(a)
print(b)
print(c)
print(d)

------------------------------------------------------
[[1 2 3]
 [4 5 6]
 [7 8 9]]
[[5 6]
 [8 9]]
[[5 6]
 [8 9]]
[[2 3]
 [5 6]
 [8 9]]

NumPy 广播(Broadcast)
广播(Broadcast)是 numpy 对不同形状(shape)的数组进行数值计算的方式, 对数组的算术运算通常在相应的元素上进行。
如果两个数组 a 和 b 形状相同,即满足 a.shape == b.shape,那么 a*b 的结果就是 a 与 b 数组对应位相乘。这要求维数相同,且各维度的长度相同。

import numpy as np 
 
a = np.array([1,2,3,4]) 
b = np.array([10,20,30,40]) 
c = a * b 
print (c)

------------------------------------------------------
[ 10  40  90 160]

当运算中的 2 个数组的形状不同时,numpy 将自动触发广播机制。如:

import numpy as np 
 
a = np.array([[ 0, 0, 0],
           [10,10,10],
           [20,20,20],
           [30,30,30]])
b = np.array([1,2,3])
print(a + b)

------------------------------------------------------
[[ 1  2  3]
 [11 12 13]
 [21 22 23]
 [31 32 33]]

Numpy 数组操作
numpy.reshap 函数可以在不改变数据的条件下修改形状,格式如下: numpy.reshape(arr, newshape, order='C')

import numpy as np
 
a = np.arange(8)
print ('原始数组:')
print (a)
print ('\n')
 
b = a.reshape(4,2)
print ('修改后的数组:')
print (b)


------------------------------------------------------
原始数组:
[0 1 2 3 4 5 6 7]


修改后的数组:
[[0 1]
 [2 3]
 [4 5]
 [6 7]]

numpy.ndarray.flatten 返回一份数组拷贝,对拷贝所做的修改不会影响原始数组,格式如下:

ndarray.flatten(order='C'),order:'C' -- 按行,'F' -- 按列,'A' -- 原顺序,'K' -- 元素在内存中的出现顺序。

import numpy as np
 
a = np.arange(8).reshape(2,4)
 
print ('原数组:')
print (a)
print ('\n')
# 默认按行
 
print ('展开的数组:')
print (a.flatten())
print ('\n')
 
print ('以 F 风格顺序展开的数组:')
print (a.flatten(order = 'F'))
原数组:
[[0 1 2 3]
 [4 5 6 7]]


展开的数组:
[0 1 2 3 4 5 6 7]


以 F 风格顺序展开的数组:
[0 4 1 5 2 6 3 7]

转置

import numpy as np
 
a = np.arange(12).reshape(3,4)
 
print ('原数组:')
print (a)
print ('\n')
 
print ('转置数组:')
print (a.T)

原数组:
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]


转置数组:
[[ 0  4  8]
 [ 1  5  9]
 [ 2  6 10]
 [ 3  7 11]]

连接数组
numpy.concatenate 函数用于沿指定轴连接相同形状的两个或多个数组

import numpy as np
 
a = np.array([[1,2],[3,4]])
 
print ('第一个数组:')
print (a)
print ('\n')
b = np.array([[5,6],[7,8]])
 
print ('第二个数组:')
print (b)
print ('\n')
# 两个数组的维度相同
 
print ('沿轴 0 连接两个数组:')
print (np.concatenate((a,b)))
print ('\n')
 
print ('沿轴 1 连接两个数组:')
print (np.concatenate((a,b),axis = 1))

第一个数组:
[[1 2]
 [3 4]]


第二个数组:
[[5 6]
 [7 8]]


沿轴 0 连接两个数组:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]


沿轴 1 连接两个数组:
[[1 2 5 6]
 [3 4 7 8]]

numpy.hstack 是 numpy.stack 函数的变体,它通过水平堆叠来生成数组。

import numpy as np
 
a = np.array([[1,2],[3,4]])
 
print ('第一个数组:')
print (a)
print ('\n')
b = np.array([[5,6],[7,8]])
 
print ('第二个数组:')
print (b)
print ('\n')
 
print ('水平堆叠:')
c = np.hstack((a,b))
print (c)
print ('\n')

print ('竖直堆叠:')
c = np.vstack((a,b))
print (c)


第一个数组:
[[1 2]
 [3 4]]


第二个数组:
[[5 6]
 [7 8]]


水平堆叠:
[[1 2 5 6]
 [3 4 7 8]]


竖直堆叠:
[[1 2]
 [3 4]
 [5 6]
 [7 8]]

相关文章

  • Numpy入门

    1、熟悉 numpy 的基础属性 2、numpy 创建 array 3、numpy的基础运算 4、numpy索引 ...

  • Numpy | 基础操作(矩阵)

    NumPy 基础操作 什么是 NumPy NumPy是Python中科学计算的基础包。它是一个Python库,提供...

  • numpy 基础

    numpy 基础 导入numpy 版本 np常用方法 numpy.array 的基本属性 numpy.array ...

  • Python-Numpy学习1

    安装numpy conda install numpy 或者是 pip install numpy 基础用法 ax...

  • Numpy库(一)- 数组的创建

    1 Numpy预备基础知识: 1.1 Numpy数据类型: ​ 1.2 Numpy数组属性: ​ 1.2.1 各个...

  • python库用途说明

    numpy提供基础矩阵运算

  • 1. pandas apply

    1. numpy pandas基础 numpy底层C语言实现,速度快,pandas是numpy的包装版

  • Numpy

    NumPy是Numeric Python的简称 NumPy是Python科学计算的基础工具包 NumPy是Pyth...

  • Pytorch教程

    Pytorch 神经网络基础 1.1 Pytorch & Numpy 1.1.1 用Torch还是Numpy To...

  • Numpy学习笔记

    Numpy学习笔记 Numpy基础 Numpy的数组类也叫ndarray,也就是大家所熟悉的array;ndarr...

网友评论

      本文标题:numpy -- 基础

      本文链接:https://www.haomeiwen.com/subject/hcltgqtx.html