前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >博客园LaTex的测试,附带开启方法

博客园LaTex的测试,附带开启方法

作者头像
逸鹏
发布2018-07-23 18:09:22
5860
发布2018-07-23 18:09:22
举报
文章被收录于专栏:逸鹏说道逸鹏说道

开启方法

内容部分先不用管,我这边只是做个测试,过几天会讲这些的,先忽略


矩阵系列

1.创建特殊矩阵

1.1.创建全为0的矩阵

np.zeros(tuple)

代码语言:javascript
复制
import numpy as np
代码语言:javascript
复制
help(np.zeros)
代码语言:javascript
复制
Help on built-in function zeros in module numpy.core.multiarray:

zeros(...)
    zeros(shape, dtype=float, order='C')

    Return a new array of given shape and type, filled with zeros.

    Parameters
    ----------
    shape : int or sequence of ints
        Shape of the new array, e.g., ``(2, 3)`` or ``2``.
    dtype : data-type, optional
        The desired data-type for the array, e.g., `numpy.int8`.  Default is
        `numpy.float64`.
    order : {'C', 'F'}, optional
        Whether to store multidimensional data in C- or Fortran-contiguous
        (row- or column-wise) order in memory.

    Returns
    -------
    out : ndarray
        Array of zeros with the given shape, dtype, and order.

    See Also
    --------
    zeros_like : Return an array of zeros with shape and type of input.
    ones_like : Return an array of ones with shape and type of input.
    empty_like : Return an empty array with shape and type of input.
    ones : Return a new array setting values to one.
    empty : Return a new uninitialized array.

    Examples
    --------
    >>> np.zeros(5)
    array([ 0.,  0.,  0.,  0.,  0.])

    >>> np.zeros((5,), dtype=int)
    array([0, 0, 0, 0, 0])

    >>> np.zeros((2, 1))
    array([[ 0.],
           [ 0.]])

    >>> s = (2,2)
    >>> np.zeros(s)
    array([[ 0.,  0.],
           [ 0.,  0.]])

    >>> np.zeros((2,), dtype=[('x', 'i4'), ('y', 'i4')]) # custom dtype
    array([(0, 0), (0, 0)],
          dtype=[('x', '<i4'), ('y', '<i4')])
代码语言:javascript
复制
# 一维
np.zeros(5) # 完整写法:np.zeros((5,))
代码语言:javascript
复制
array([0., 0., 0., 0., 0.])
代码语言:javascript
复制
# 可以指定类型
np.zeros(5,dtype=int)
代码语言:javascript
复制
array([0, 0, 0, 0, 0])
代码语言:javascript
复制
# 二维
np.zeros((2,5))
代码语言:javascript
复制
array([[0., 0., 0., 0., 0.],
       [0., 0., 0., 0., 0.]])
代码语言:javascript
复制
# 三维 ==> 可以这么理解,2个2*5(2行5列)的矩阵
np.zeros((2,2,5))
代码语言:javascript
复制
array([[[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]],

       [[0., 0., 0., 0., 0.],
        [0., 0., 0., 0., 0.]]])
代码语言:javascript
复制
################### 扩展部分 ########################
代码语言:javascript
复制
# 建议用元组,官方文档都是元组,而且shape返回类型就是元组
array1 = np.zeros([2,3])
print(array1)
type(array1)
print(array1.shape) # shape返回类型就是元组
代码语言:javascript
复制
[[0. 0. 0.]
 [0. 0. 0.]]
(2, 3)

1.2.创建全为1的矩阵

np.ones(tuple) 用法和 np.zeros(tuple)差不多

代码语言:javascript
复制
help(np.ones)
代码语言:javascript
复制
Help on function ones in module numpy.core.numeric:

ones(shape, dtype=None, order='C')
    Return a new array of given shape and type, filled with ones.

    Parameters
    ----------
    shape : int or sequence of ints
        Shape of the new array, e.g., ``(2, 3)`` or ``2``.
    dtype : data-type, optional
        The desired data-type for the array, e.g., `numpy.int8`.  Default is
        `numpy.float64`.
    order : {'C', 'F'}, optional
        Whether to store multidimensional data in C- or Fortran-contiguous
        (row- or column-wise) order in memory.

    Returns
    -------
    out : ndarray
        Array of ones with the given shape, dtype, and order.

    See Also
    --------
    zeros, ones_like

    Examples
    --------
    >>> np.ones(5)
    array([ 1.,  1.,  1.,  1.,  1.])

    >>> np.ones((5,), dtype=int)
    array([1, 1, 1, 1, 1])

    >>> np.ones((2, 1))
    array([[ 1.],
           [ 1.]])

    >>> s = (2,2)
    >>> np.ones(s)
    array([[ 1.,  1.],
           [ 1.,  1.]])
代码语言:javascript
复制
# 一维
np.ones(5) # 完整写法 np.ones((5,))
代码语言:javascript
复制
array([1., 1., 1., 1., 1.])
代码语言:javascript
复制
# 可以指定类型
np.ones(5,dtype=int)
代码语言:javascript
复制
array([1, 1, 1, 1, 1])
代码语言:javascript
复制
# 二维,传一个shape元组
np.ones((2,5))
代码语言:javascript
复制
array([[1., 1., 1., 1., 1.],
       [1., 1., 1., 1., 1.]])
代码语言:javascript
复制
# 三维 可以理解为两个二维数组
np.ones((2,2,5))
代码语言:javascript
复制
array([[[1., 1., 1., 1., 1.],
        [1., 1., 1., 1., 1.]],

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

1.3.单位矩阵

先普及一个数学基础:任何 矩阵 x 单位矩阵 都等于其 本身

单位矩阵是个方阵,从左上角到右下角的对角线(称为主对角线)上的元素均为1。其他全都为0,eg:

np.eye() 来定义(eye:眼睛)

扩展: np.eye(rows,columns=rows)

代码语言:javascript
复制
help(np.eye)
代码语言:javascript
复制
Help on function eye in module numpy.lib.twodim_base:

eye(N, M=None, k=0, dtype=<class 'float'>, 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.]])
代码语言:javascript
复制
# 定义一个2行的单位矩阵(列默认和行一致)
np.eye(2)
代码语言:javascript
复制
array([[1., 0.],
       [0., 1.]])
代码语言:javascript
复制
np.eye(3,dtype=int)
代码语言:javascript
复制
array([[1, 0, 0],
       [0, 1, 0],
       [0, 0, 1]])
代码语言:javascript
复制
# 定义一个5行5列的单位矩阵
np.eye(5)
代码语言:javascript
复制
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.]])
本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2018-07-10,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 我为Net狂 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 开启方法
  • 矩阵系列
    • 1.创建特殊矩阵
      • 1.1.创建全为0的矩阵
      • 1.2.创建全为1的矩阵
      • 1.3.单位矩阵
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档