NumPy 之 存储文件和线性代数
import numpy as np
File Input and Output
NumPy is able to save and load data to and from disk either in text or binary format. In this section I only discuss NumPy's built-in binary format, since most users wil prefer pandas and other tools for loading text or tabular data.
np.save and np.load are the two workhorse functions(主要的函数) for efficiently saving and loading array data on disk. Arrays are saved by default in an uncompressed(未压缩的) raw binary format with file extension .npy:
arr = np.arange(10)
"保存数组"
np.save('../examples/some_arry', arr)
'保存数组'
If the file path does not already end in .npy, the extension will be appended. The array o disk can then be oaded with np.load.
np.load('../examples/some_array.npy')
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], dtype=int64)
You save multiple arrays in an uncompressed(未解压的) archive using np.savez and passing the arrays as keyword arguments:
When loading an .npz file, you get back a dict-like object that loads the individual(个别的) arrays lazily.
np.savez("../examples/array_archive.npz", a=arr, b=arr)
arch = np.load('../examples/array_archive.npz')
'多个数组取出时, 是惰性加载的, 类似生成器'
arch['b']
'多个数组取出时, 是惰性加载的, 类似生成器'
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
if your data compresses well, you may wish to use numpy.savez_compressed instead:
np.savez_compressed('../examples/arrays_compressed.npz', a=arr, b=arr)
Linear Algebra
Linear algera, like matrix multiplication(乘法), decompositions(分解), determinats(秩), and other square matrix math, is an important part of any array library. Unlike some languages lile MATLIB, multiplying two, two-dimensional array with * is an element-wise product instead of a matrix dot product.(python 中的星号, 表示两个数组的对应元素乘, 而非矩阵乘哦) Thus, there is a function dot, both an array method and a function in the numpy namespace, for matrix multiplication.
"2x3"
x = np.array([[1,2,3], [4,5,6]])
"3x2"
y = np.array([[6,23], [-1,7], [8,9]])
x
y
'2x3'
'3x2'
array([[1, 2, 3],
[4, 5, 6]])
array([[ 6, 23],
[-1, 7],
[ 8, 9]])
"2x3 * 3x2 = 2x2, 对左边列向量的线性组合嘛"
x.dot(y)
"x.dot(y) is equivalent to np.dot(x,y)"
np.dot(x,y)
'2x3 * 3x2 = 2x2, 对左边列向量的线性组合嘛'
array([[ 28, 64],
[ 67, 181]])
'x.dot(y) is equivalent to np.dot(x,y)'
array([[ 28, 64],
[ 67, 181]])
A matrix product between a two-dimensional array and a suitably sized one-dimensional array results in a one-dimensional array:
np.dot(x, np.ones(3))
array([ 6., 15.])
The @ symbol also works as an infix operator(强制插入) that performs matrix multiplication:
x @ np.ones(3)
array([ 6., 15.])
numpy.linalg has a stardard set of matrix decompositions and things like inverse and determinant. These are implemented(被运行) under the hood via(通过) the same industry-standard linear algebra libraies used in other languages like MATLAB and R.. -> Python的这些矩阵的函数都是和像这样的语言用的同一个标准.
from numpy.linalg import inv, qr
x = np.random.randn(5,5)
"计算内积 $A^TA$"
mat = x.T.dot(x)
'矩阵求逆 inv'
inv(mat)
'计算内积 $A^TA$'
'矩阵求逆 inv'
array([[ 2.85462863, -0.08842397, -0.01719878, 0.28840731, 1.33531619],
[-0.08842397, 0.36385157, 0.05790547, 0.21733807, -0.04179607],
[-0.01719878, 0.05790547, 0.16845103, 0.03607368, -0.07364311],
[ 0.28840731, 0.21733807, 0.03607368, 0.33697629, 0.22115989],
[ 1.33531619, -0.04179607, -0.07364311, 0.22115989, 0.89288396]])
"矩阵与其逆的积-> 单位阵"
mat.dot(inv(mat))
'矩阵的QR分解, Q是正交矩阵, R是上三角矩阵'
q, r = qr(mat)
q
r
'矩阵与其逆的积-> 单位阵'
array([[ 1.00000000e+00, -1.34588695e-17, 1.20337000e-17,
1.43461087e-16, 1.19991758e-16],
[-1.44647137e-16, 1.00000000e+00, -2.44723192e-19,
-3.05504861e-16, 1.85480504e-17],
[-5.64972079e-16, 1.06290868e-17, 1.00000000e+00,
-1.24805205e-17, -1.52382375e-17],
[ 2.64575648e-17, -3.37897541e-18, -3.46206228e-17,
1.00000000e+00, -2.61606309e-16],
[-4.05280510e-16, -4.83836278e-17, 2.70272255e-17,
-3.85392887e-17, 1.00000000e+00]])
'矩阵的QR分解, Q是正交矩阵, R是上三角矩阵'
array([[-0.49090965, -0.10814586, -0.24518935, 0.10423654, 0.82239231],
[ 0.00323168, -0.7867822 , -0.00793056, -0.61663426, -0.02574129],
[ 0.32640742, 0.12703087, -0.92399902, -0.14659492, -0.04535519],
[-0.1390847 , 0.59414019, 0.14745407, -0.76639263, 0.13620759],
[ 0.79568267, -0.01178262, 0.25357916, -0.00701326, 0.54990788]])
array([[-2.59814895, 1.59367406, 4.62351953, -3.75681459, 6.16316304],
[ 0. , -7.15657133, 0.57430456, 7.77949798, -2.22774585],
[ 0. , 0. , -6.05158388, 1.46469814, -0.57791469],
[ 0. , 0. , 0. , -2.70965304, 0.66330379],
[ 0. , 0. , 0. , 0. , 0.61587833]])
The express x.T.dot(x) computes the dot product of x with its transpose x.T
See Table 4-7 for a list of some of the most commonly used linear algbrea functions.
- diag 对角化
- dot 矩阵乘法
- trace 迹: Compute the sum of the diagonal elements
- det 行列式 Compute the matrix determinant
- inv 逆 Compute the inverse of a square matrix
- eig, qr, svd 矩阵的谱分解, QR分解, SVD 分解
- solve 线性方程的解 Solve the linear system Ax=b for x where A is a square matrix
- lstsq 最小二乘近似解 Compute the least-squares solution to Ax=b
NumPy 之 存储文件和线性代数的更多相关文章
- Numpy入门(三):Numpy概率模块和线性代数模块
Numpy中经常使用到的两个模块是概率模块和线性代数模块,random 和 linalg 两个模块. 概率模块 产生二项分布的随机数:np.random.binomial(n,p,size=-),其中 ...
- python之numpy的基本使用
https://blog.csdn.net/cxmscb/article/details/54583415 一.numpy概述 numpy(Numerical Python)提供了python对多维数 ...
- AI炼丹 - 深度学习必备库 numpy
目录 深度学习必备库 - Numpy 1. 基础数据结构ndarray数组 1.1 为什么引入ndarray数组 1.2 如何创建ndarray数组 1.3 ndarray 数组的基本运算 1.4 n ...
- 利用Python进行数据分析(1) 简单介绍
一.处理数据的基本内容 数据分析 是指对数据进行控制.处理.整理.分析的过程. 在这里,“数据”是指结构化的数据,例如:记录.多维数组.Excel 里的数据.关系型数据库中的数据.数据表等. 二.说说 ...
- 笔记之Python网络数据采集
笔记之Python网络数据采集 非原创即采集 一念清净, 烈焰成池, 一念觉醒, 方登彼岸 网络数据采集, 无非就是写一个自动化程序向网络服务器请求数据, 再对数据进行解析, 提取需要的信息 通常, ...
- 利用Python进行数据分析——重要的Python库介绍
利用Python进行数据分析--重要的Python库介绍 一.NumPy 用于数组执行元素级计算及直接对数组执行数学运算 线性代数运算.傅里叶运算.随机数的生成 用于C/C++等代码的集成 二.pan ...
- AI - TensorFlow - 张量(Tensor)
张量(Tensor) 在Tensorflow中,变量统一称作张量(Tensor). 张量(Tensor)是任意维度的数组. 0阶张量:纯量或标量 (scalar), 也就是一个数值,例如,\'Howd ...
- 第01章 准备工作.md
第1章 准备工作 1.1 本书的内容 本书讲的是利用Python进行数据控制.处理.整理.分析等方面的具体细节和基本要点.我的目标是介绍Python编程和用于数据处理的库和工具环境,掌握这些,可以让你 ...
- AI之路,第一篇:python数学知识1
python 数学知识1 1,向量: 一个向量是一列数.这些数是有序排列的:通过次序中的索引,可以确定每个单独的数: 2, 矩阵: 由m x n 个数aij(i=1,2,3,…, m; j=1,2, ...
随机推荐
- webpack中路径的理解
webpack 前端打包工具, 开发人员要面对的路径主要是: 打包前的路径(开发环境路径)和打包后的路径(生产环境路径) 在webpack.config.js中配置的output.path, outp ...
- 列表:list
#_*_coding:utf-8_*_#作者:王佃元#日期:2019/12/7'''数据类型整数字符串列表.元组name = 'wuchao'name = 'jinxin'name = 'xiaohu ...
- Stuts 文件上传
Stuts 文件上传 三种上传方案 1.上传到tomcat服务器 上传图片的存放位置与tomcat服务器的耦合度太高 2.上传到指定文件目录,添加服务器与真实目录的映射 ...
- OpenResty: 介绍 (摘抄)
原文链接:https://www.cnblogs.com/duanxz/p/10396160.html Nginx 是俄罗斯人发明的, Lua 是巴西几个教授发明的,中国人章亦春把 LuaJIT VM ...
- contest5 CF991 div2 ooooxx ooooox ooooox
题意 div2D 给出一个棋盘, 有一些点不能放, 总共\(2\)排, 长度\(n(\le 100)\) 在上面空位摆放\('L'\)字形的牌, 问最多能放几个 例如: 00X00X0XXX0 0XX ...
- nginx 日志之 access_log分割
如果任由访问日志写下去,日志文件会变得越来越大,甚至是写满磁盘. 所以,我们需要想办法把日志做切割,比如每天生成一个新的日志,旧的日志按规定时间删除即可. 实现日志切割可以通过写shell脚本或者系统 ...
- 爬虫(一)基础知识(python)
1.1 定义 网络爬虫,也叫网络蜘蛛(Web Spider),如果把互联网比喻成一个蜘蛛网,Spider就是一只在网上爬来爬去的蜘蛛.网络爬虫就是根据网页的地址来寻找网页的,也就是URL.举一个简单的 ...
- ASP.Net Core使用分布式缓存Redis从入门到实战演练
一.课程介绍 人生苦短,我用.NET Core!缓存在很多情况下需要用到,合理利用缓存可以一方面可以提高程序的响应速度,同时可以减少对特定资源访问的压力. 所以经常要用到且不会频繁改变且被用户共享的 ...
- Appium 滑动踩坑记
前言 对于不同java-client版本,很多的API已经产生大的变化,所以一些API大家会发现已经失效或者使用方式发生了变化,滑动就是其中一项,这篇文章对滑动在不同的java-client版本以及不 ...
- R语言dataframe的常用操作总结
前言:近段时间学习R语言用到最多的数据格式就是data.frame,现对data.frame常用操作进行总结,其中函数大部分来自dplyr包,该包由Hadley Wickham所作,主要用于数据的清洗 ...