在学习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创建array的方法汇总

    创建numpy.array,是使用numpy这个核武器的基础,本文尽量汇总常用创建numpy.array的方法. array函数 >>> import numpy as np > ...

  5. python课程设计矩阵对角线之和_python对角矩阵

    广告关闭 腾讯云11.11云上盛惠 ,精选热门产品助力上云,云服务器首年88元起,买的越多返的越多,最高返5000元! #生成一个3*3的0-10之间的随机整数矩阵,如果需要指定下界则可以多加一个参数 ...

  6. python创建类的实例方法-Python中动态创建类实例的方法

    简介 在Java中我们可以通过反射来根据类名创建类实例,那么在Python我们怎么实现类似功能呢? 其实在Python有一个builtin函数import,我们可以使用这个函数来在运行时动态加载一些模 ...

  7. python 申请内存空间、用于创建多维数组_python 申请内存空间,用于创建多维数组的实例...

    以三维数组为例 先申请1个一维数组空间: mat = [None]*d1 d1是第一维的长度. 再把mat中每个元素扩展为第二维的长度: for i in range(len(mat)): mat[i ...

  8. Python图像拼接:创建全景图

    Python图像拼接:创建全景图 算法原理 基础流程 几何原理 核心步骤(RANSAC算法) 源代码 结果分析 综述 夜景(中华城) 室内(引桐楼) 多建筑(鹭江道周边) 单调场景(海滩) 复杂场景( ...

  9. 什么是Python线程?Python线程如何创建?

    相信正在学习Python技术或者对Python语言有一定了解的人对于Python线程应该都不陌生,但是也有刚接触Python的小伙伴对于Python线程并不了解,今天小编就跟大家聊聊什么是Python ...

最新文章

  1. java修改数据库表结构_数据库设计(一):设计传统系统表结构(Java开发)
  2. 注册界面php mysql_php:用户登录注册并存入数据库的简单网页示例
  3. 安卓mysql导出excel_Android开发实现的导出数据库到Excel表格功能【附源码下载】...
  4. NYOJ 875 小M的操作数
  5. pycharm 修改新建文件时的头部模板
  6. JetBrains CLion C++ IDE连接wsl2(Ubuntu)时,报错“Unable to establish SSL connection“解决方案
  7. iPhone在华智能机市场份额首次下滑
  8. Scala基础 - _root_ package的作用
  9. ACL2020 | FastBERT:放飞BERT的推理速度
  10. 图形驱动程序和显卡驱动什么区别_以后你的手机也需要单独安装显卡驱动程序了...
  11. python条件语句有哪些_Python 条件语句
  12. 测试化验加工费云服务器文献信息,监管▕ 科研经费使用中的 “红线”和“禁区”典型问题自查清单...
  13. 伯克利弹跳机器人再进化:超精准着陆,指哪打哪
  14. python 类 对象 方法 应用_Python 定制类与其对象的创建和应用
  15. 测试工程师值得被尊重!是否有此共鸣!
  16. java通过密钥得到谷歌验证码
  17. VS中打开C项目源文件、头文件分类文件夹不见了?这样操作打开!
  18. 大数据(042)机器学习【神经网络】
  19. JavaWeb实用项目之----化妆品销售网
  20. 向数据中添加高斯噪声

热门文章

  1. 怎样使用python画复杂函数_在python中绘制复杂的函数?
  2. [题解]Shorten IPv6 Address-模拟(2019牛客多校第六场B题)
  3. [转帖]linux /proc目录下的文件为何无法用vi编辑保存
  4. 【信息系统项目管理师】第5章-项目范围管理 知识点详细整理
  5. ImportError: No module named ‘numpy‘的解决办法
  6. Android8.1展讯平台之audio_policy_configuration.xml(四十二)
  7. WebRTC之linux ARM64交叉编译(七)
  8. python内置函数__init__及__str__的区别
  9. 深度学习自学(十五):人脸识别数据预处理方法
  10. 流程图的虚线是什么意思_这些新标识啥意思?交警教你怎么走