《用Python做科学计算》学习笔记(4)--numpy矩阵的操作

来源:互联网 发布:web项目压缩js 编辑:程序博客网 时间:2024/06/05 18:47

1 基本操作

1.1 numpy.copyto(dst, src, casting=’same_kind’, where=None)

函数功能:复制矩阵src的值到矩阵dst。函数参数:dst:    目标矩阵        src:    原矩阵        casting:选择复制矩阵的时候的操作             {‘no’, ‘equiv’, ‘safe’, ‘same_kind’, ‘unsafe’}            • ‘no’    :不复制矩阵元素类型.            • ‘equiv’ :允许字节顺序可变            • ‘safe’ means only casts which can preserve values are allowed.            • ‘same_kind’ means only safe casts or casts within a kind, like float64 to float32, are allowed.            • ‘unsafe’ means any data conversions may be done.         where : array_like of bool, optionalA boolean array which is broadcasted to match the dimensions of dst, and selects elements to copy from src to dst wherever it contains the value True.      

2 改变矩阵尺寸
这里写图片描述
2.1 numpy.reshape(a, newshape, order=’C’)

函数功能:改变矩阵a的尺寸函数参数:a        : 需要改变的矩阵        newshape : 矩阵新的尺寸        order    : 操作选项 {‘C’, ‘F’, ‘A’}这三个参数与矩阵的储存有关

2.2 numpy.ravel(a, order=’C’)

函数功能:将矩阵转换为一维数组函数参数:a       :需要改变的矩阵        order    : 操作选项 {‘C’, ‘F’, ‘A’}这三个参数与矩阵的储存有关

2.3 ndarray.flat 将矩阵变为一维数组,返回一个迭代实体。可以用下标检索。
2.4 ndarray.flatten 将矩阵变为一维数组并返回整个数组

范例:

import numpy as npa1 = np.arange(1,10,1)a3 = a1.reshape(3,3)a2 = np.ravel(a1)print('建立数组是',a1)print('改变数组的尺寸:',a3)print('将矩阵变换为一维数组:',a2)print('flat举例:',a1.flat[0])print('flatten()举例:',a1.flatten())

屏幕输出:

建立数组是 [1 2 3 4 5 6 7 8 9]改变数组的尺寸: [[1 2 3] [4 5 6] [7 8 9]]将矩阵变换为一维数组: [1 2 3 4 5 6 7 8 9]flat举例: 1flatten()举例: [1 2 3 4 5 6 7 8 9]

3 二进制操作
3.1 元素位操作
这里写图片描述
元素的位操作是将矩阵x1和矩阵x2对应的元素进行位操作,包括常用的与、或、非、异或,左移、右移的操作。需要注意的是它操作的是对应元素的二进制位。

3.2 位打包
这里写图片描述
将矩阵打包成8位无符号矩阵,似乎一般用于图像,节约储存空间

3.3 格式输出:binary_repr(num[, width])
将矩阵转换为二进制形式

4 字符串操作
这里写图片描述

1 0