在学习linear regression时经常处理的数据一般多是矩阵或者n维向量的数据形式,所以必须对矩阵有一定的认识基础。

numpy中创建单位矩阵借助identity()函数。更为准确的说,此函数创建的是一个n*n的单位数组,返回值的dtype=array数据形式。其中接受的参数有两个,第一个是n值大小,第二个为数据类型,一般为浮点型。单位数组的概念与单位矩阵相同,主对角线元素为1,其他元素均为零,等同于单位1。而要想得到单位矩阵,只要用mat()函数将数组转换为矩阵即可。

>>> import numpy as np

>>> help(np.identity)

Help on function identity in module numpy:

identity(n, dtype=None)

Return the identity array.

The identity array is a square array with ones on

the main diagonal.

Parameters

----------

n : int

Number of rows (and columns) in `n` x `n` output.

dtype : data-type, optional

Data-type of the output. Defaults to ``float``.

Returns

-------

out : ndarray

`n` x `n` array with its main diagonal set to one,

and all other elements 0.

Examples

--------

>>> np.identity(3)

array([[ 1., 0., 0.],

[ 0., 1., 0.],

[ 0., 0., 1.]])

>>> np.identity(5)

array([[1., 0., 0., 0., 0.],

[0., 1., 0., 0., 0.],

[0., 0., 1., 0., 0.],

[0., 0., 0., 1., 0.],

[0., 0., 0., 0., 1.]])

>>> A = np.mat(np.identity(5))

>>> A

matrix([[1., 0., 0., 0., 0.],

[0., 1., 0., 0., 0.],

[0., 0., 1., 0., 0.],

[0., 0., 0., 1., 0.],

[0., 0., 0., 0., 1.]])

矩阵的运算中还经常使用对角阵,numpy中的对角阵用eye()函数来创建。eye()函数接受五个参数,返回一个单位数组。第一个和第二个参数N,M分别对应表示创建数组的行数和列数,当然当你只设定一个值时,就默认了N=M。第三个参数k是对角线指数,跟diagonal中的offset参数是一样的,默认值为0,就是主对角线的方向,上三角方向为正,下三角方向为负,可以取-n到+m的范围。第四个参数是dtype,用于指定元素的数据类型,第五个参数是order,用于排序,有‘C'和‘F'两个参数,默认值为‘C',为行排序,‘F'为列排序。返回值为一个单位数组。

>>> help(np.eye)

Help on function eye in module numpy:

eye(N, M=None, k=0, dtype=, order='C')

Return a 2-D array with ones on the diagonal and zeros elsewhere.

Parameters

----------

N : int

Number of rows in the output.

M : int, optional

Number of columns in the output. If None, defaults to `N`.

k : int, optional

Index of the diagonal: 0 (the default) refers to the main diagonal,

a positive value refers to an upper diagonal, and a negative value

to a lower diagonal.

dtype : data-type, optional

Data-type of the returned array.

order : {'C', 'F'}, optional

Whether the output should be stored in row-major (C-style) or

column-major (Fortran-style) order in memory.

.. versionadded:: 1.14.0

Returns

-------

I : ndarray of shape (N,M)

An array where all elements are equal to zero, except for the `k`-th

diagonal, whose values are equal to one.

See Also

--------

identity : (almost) equivalent function

diag : diagonal 2-D array from a 1-D array specified by the user.

Examples

--------

>>> np.eye(2, dtype=int)

array([[1, 0],

[0, 1]])

>>> np.eye(3, k=1)

array([[ 0., 1., 0.],

[ 0., 0., 1.],

[ 0., 0., 0.]])

numpy中的diagonal()方法可以对n*n的数组和方阵取对角线上的元素,diagonal()接受三个参数。第一个offset参数是主对角线的方向,默认值为0是主对角线,上三角方向为正,下三角方向为负,可以取-n到+n的范围。第二个参数和第三个参数是在数组大于2维时指定一个2维数组时使用,默认值axis1=0,axis2=1。

>>> help(A.diagonal)

Help on built-in function diagonal:

diagonal(...) method of numpy.matrix instance

a.diagonal(offset=0, axis1=0, axis2=1)

Return specified diagonals. In NumPy 1.9 the returned array is a

read-only view instead of a copy as in previous NumPy versions. In

a future version the read-only restriction will be removed.

Refer to :func:`numpy.diagonal` for full documentation.

See Also

--------

numpy.diagonal : equivalent function

>>> help(np.diagonal)

Help on function diagonal in module numpy:

diagonal(a, offset=0, axis1=0, axis2=1)

Return specified diagonals.

If `a` is 2-D, returns the diagonal of `a` with the given offset,

i.e., the collection of elements of the form ``a[i, i+offset]``. If

`a` has more than two dimensions, then the axes specified by `axis1`

and `axis2` are used to determine the 2-D sub-array whose diagonal is

returned. The shape of the resulting array can be determined by

removing `axis1` and `axis2` and appending an index to the right equal

to the size of the resulting diagonals.

In versions of NumPy prior to 1.7, this function always returned a new,

independent array containing a copy of the values in the diagonal.

In NumPy 1.7 and 1.8, it continues to return a copy of the diagonal,

but depending on this fact is deprecated. Writing to the resulting

array continues to work as it used to, but a FutureWarning is issued.

Starting in NumPy 1.9 it returns a read-only view on the original array.

Attempting to write to the resulting array will produce an error.

In some future release, it will return a read/write view and writing to

the returned array will alter your original array. The returned array

will have the same type as the input array.

If you don't write to the array returned by this function, then you can

just ignore all of the above.

If you depend on the current behavior, then we suggest copying the

returned array explicitly, i.e., use ``np.diagonal(a).copy()`` instead

of just ``np.diagonal(a)``. This will work with both past and future

versions of NumPy.

Parameters

----------

a : array_like

Array from which the diagonals are taken.

offset : int, optional

Offset of the diagonal from the main diagonal. Can be positive or

negative. Defaults to main diagonal (0).

axis1 : int, optional

Axis to be used as the first axis of the 2-D sub-arrays from which

the diagonals should be taken. Defaults to first axis (0).

axis2 : int, optional

Axis to be used as the second axis of the 2-D sub-arrays from

which the diagonals should be taken. Defaults to second axis (1).

Returns

-------

array_of_diagonals : ndarray

If `a` is 2-D, then a 1-D array containing the diagonal and of the

same type as `a` is returned unless `a` is a `matrix`, in which case

a 1-D array rather than a (2-D) `matrix` is returned in order to

maintain backward compatibility.

If ``a.ndim > 2``, then the dimensions specified by `axis1` and `axis2`

are removed, and a new axis inserted at the end corresponding to the

diagonal.

Raises

------

ValueError

If the dimension of `a` is less than 2.

See Also

--------

diag : MATLAB work-a-like for 1-D and 2-D arrays.

diagflat : Create diagonal arrays.

trace : Sum along diagonals.

Examples

--------

>>> a = np.arange(4).reshape(2,2)

>>> a

array([[0, 1],

[2, 3]])

>>> a.diagonal()

array([0, 3])

>>> a.diagonal(1)

array([1])

A 3-D example:

>>> a = np.arange(8).reshape(2,2,2); a

array([[[0, 1],

[2, 3]],

[[4, 5],

[6, 7]]])

>>> a.diagonal(0, # Main diagonals of two arrays created by skipping

... 0, # across the outer(left)-most axis last and

... 1) # the "middle" (row) axis first.

array([[0, 6],

[1, 7]])

The sub-arrays whose main diagonals we just obtained; note that each

corresponds to fixing the right-most (column) axis, and that the

diagonals are "packed" in rows.

>>> a[:,:,0] # main diagonal is [0 6]

array([[0, 2],

[4, 6]])

>>> a[:,:,1] # main diagonal is [1 7]

array([[1, 3],

[5, 7]])

>>> A = np.random.randint(low=5, high=30, size=(5, 5))

>>> A

array([[25, 15, 26, 6, 22],

[27, 14, 22, 16, 21],

[22, 17, 10, 14, 25],

[11, 9, 27, 20, 6],

[24, 19, 19, 26, 14]])

>>> A.diagonal()

array([25, 14, 10, 20, 14])

>>> A.diagonal(offset=1)

array([15, 22, 14, 6])

>>> A.diagonal(offset=-2)

array([22, 9, 19])

以上这篇numpy创建单位矩阵和对角矩阵的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

python矩阵对角化_numpy创建单位矩阵和对角矩阵的实例相关推荐

  1. python 对角矩阵_numpy创建单位矩阵和对角矩阵的实例

    在学习linear regression时经常处理的数据一般多是矩阵或者n维向量的数据形式,所以必须对矩阵有一定的认识基础. numpy中创建单位矩阵借助identity()函数.更为准确的说,此函数 ...

  2. python 对角阵_numpy创建单位矩阵和对角矩阵的实例

    在学习linear regression时经常处理的数据一般多是矩阵或者n维向量的数据形式,所以必须对矩阵有一定的认识基础. numpy中创建单位矩阵借助identity()函数.更为准确的说,此函数 ...

  3. python生成单位矩阵_numpy创建单位矩阵和对角矩阵的实例

    在学习linear regression时经常处理的数据一般多是矩阵或者n维向量的数据形式,所以必须对矩阵有一定的认识基础. numpy中创建单位矩阵借助identity()函数.更为准确的说,此函数 ...

  4. python矩阵教程_numpy教程:矩阵matrix及其运算

    numpy矩阵简介 NumPy函数库中存在两种不同的数据类型(矩阵matrix和数组array),都可以用于处理行列表示的数字元素.虽然它们看起来很相似,但是在这两个数据类型上执行相同的数学运算可能得 ...

  5. python 矩阵库_NumPy 矩阵库(Matrix)

    NumPy 中包含了一个矩阵库 numpy.matlib,该模块中的函数返回的是一个矩阵,而不是 ndarray 对象. 由 m × n 个数aij排成的 m 行 n 列的数表称为 m 行 n 列的矩 ...

  6. python矩阵拼接_numpy数组拼接简单示例_python

    这篇文章主要介绍了numpy数组拼接简单示例,涉及对numpy数组的介绍,numpy数组的属性等内容,具有一定借鉴价值,需要的朋友可以参考下. NumPy数组是一个多维数组对象,称为ndarray.其 ...

  7. 程序员的自我修养之数学基础04:特殊矩阵(零矩阵、单位矩阵、对角矩阵、逆矩阵、转置矩阵、对称矩阵)

    零矩阵 零矩阵就是所有元素都是0的矩阵,一般记做O.可以在后面加 m,n 表示其规模. 在前一章,我们讲到,矩阵就是映射.零矩阵,就表示了将所有的点都映到原点的映射. 因此,对于任意向量 x,都有 O ...

  8. python 矩阵合并_numpy 的矩阵合并与分割

    aiblog4.jpg 这次分享下numpy中矩阵的合并与分割,希望能帮助到大家. 在此附上视频链接 一.引入numpy第三方库 首先我们引入numpy这个第三方库,如果有同学没安装numpy可在命令 ...

  9. python 矩阵拼接_Numpy基础4 矩阵取整 拉平 拼接 切分 复制等函数操作

    Github源码In [1]: import numpy as np B = np.arange(3) print(B) print(np.exp(B)) #返回e的幂次方,e是一个常数为2.7182 ...

最新文章

  1. Java stream! Kafka steam!流式处理这么火!它究竟是个啥?
  2. Android ViewPager嵌套ViewPager滑动冲突处理方法
  3. ip映射后 前端无法调用接口_基于S7300400 CPU集成PN接口的Modbus TCPW
  4. 网易云音乐Android版使用的开源组件
  5. linux下的arm仿真,使用QEMU仿真ARM Linux系统
  6. 【STM32】FreeRTOS简介
  7. 怎么做图片文字二维码一起_怎么做?才能让文字编排更出彩
  8. class 命名规范
  9. 带父节点的平衡二叉树_平衡二叉树的左右旋以及双旋转的图文详解
  10. 3D视觉创新方案分享:仓储VSLAM/商品三维重建/静态场景重建/表情识别等多个方向...
  11. 易实战Spring Boot 2 资源汇总 从入门到精通 内含实战github代码 毫无保留分享
  12. web多媒体标签,表格标签,超链接标签,语义化标签练习
  13. python timer详解_Python timer定时器两种常用方法解析
  14. The following signatures were invalid: EXPKEYSIG F42ED6FBAB17C654 的解决方法
  15. Python语言的应用前景如何,应用方向有哪些。
  16. vue3 倒计时功能
  17. 字符串统计不同类型字符的个数
  18. 自动填充空白单元格_使用自动填充插入或删除单元格
  19. ML-Agents学习之RollerBall项目
  20. 【Soul】用户运营策略分析报告

热门文章

  1. 【转】windows 7系统安装与配置Tomcat服务器环境
  2. 正确加载 Javascript 和 CSS 到 WordPress
  3. Excel2003怎样拆分单元格
  4. mysqldump的几个主要选项探究
  5. 为什么很多人C语言学不下去
  6. 最全的BI工具选型指南!给你五大箴言要记住
  7. SQL中的表 与关系数据库
  8. 飞秋 一个程序员的老作品。
  9. 经典面试题(11):关于变量提升,以下代码将输出什么?
  10. 第十一节:动态绑定class和style