博客园LaTex的测试,附带开启方法
2018-07-09 15:28
357 查看
开启方法
内容部分先不用管,我这边只是做个测试,过几天会讲这些的,先忽略
矩阵系列¶
1.创建特殊矩阵¶
1.1.创建全为0的矩阵¶
np.zeros(tuple)
$$\begin{bmatrix} 0&0&0 \\ 0&0&0 \\ 0&0&0 \end{bmatrix}$$
In [1]:import numpy as npIn [2]:
help(np.zeros)
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')])In [3]:
# 一维 np.zeros(5) # 完整写法:np.zeros((5,))Out[3]:
array([0., 0., 0., 0., 0.])In [4]:
# 可以指定类型 np.zeros(5,dtype=int)Out[4]:
array([0, 0, 0, 0, 0])In [5]:
# 二维 np.zeros((2,5))Out[5]:
array([[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]])In [6]:
# 三维 ==> 可以这么理解,2个2*5(2行5列)的矩阵 np.zeros((2,2,5))Out[6]:
array([[[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]], [[0., 0., 0., 0., 0.], [0., 0., 0., 0., 0.]]])In [7]:
################### 扩展部分 ########################In [8]:
# 建议用元组,官方文档都是元组,而且shape返回类型就是元组 array1 = np.zeros([2,3]) print(array1) type(array1) print(array1.shape) # shape返回类型就是元组
[[0. 0. 0.] [0. 0. 0.]] (2, 3)
1.2.创建全为1的矩阵¶
np.ones(tuple)用法和
np.zeros(tuple)差不多
$$\begin{bmatrix} 1&1&1 \\ 1&1&1 \\ 1&1&1 \end{bmatrix}$$
In [9]:help(np.ones)
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.]])In [10]:
# 一维 np.ones(5) # 完整写法 np.ones((5,))Out[10]:
array([1., 1., 1., 1., 1.])In [11]:
# 可以指定类型 np.ones(5,dtype=int)Out[11]:
array([1, 1, 1, 1, 1])In [12]:
# 二维,传一个shape元组 np.ones((2,5))Out[12]:
array([[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]])In [13]:
# 三维 可以理解为两个二维数组 np.ones((2,2,5))Out[13]:
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:
$$\begin{bmatrix} 1&0&0 \\ 0&1&0 \\ 0&0&1 \end{bmatrix}$$
用 np.eye()
来定义(eye:眼睛)
扩展:
np.eye(rows,columns=rows)In [14]:
help(np.eye)
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.]])In [15]:
# 定义一个2行的单位矩阵(列默认和行一致) np.eye(2)Out[15]:
array([[1., 0.], [0., 1.]])In [16]:
np.eye(3,dtype=int)Out[16]:
array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])In [17]:
# 定义一个5行5列的单位矩阵 np.eye(5)Out[17]:
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.]])
相关文章推荐
- 自己实现的附带文件的压力测试方法
- 博客园使用LaTex输入公式测试
- 在博客园中使用Latex进行公式编写的简易方法
- zz 高效的中文字符串截取函数,附带一个性能测试方法Benchmark_Iterate
- PHP简单开启curl的方法(测试可行)[原创]_php技巧_脚本之家
- 微信小程序的正确开启方法,最新更新小程序附带详细图
- 多线程开启gprof性能测试的简易方法
- PHP简单开启curl的方法(测试可行) 原创
- 多线程开启gprof性能测试的简易方法
- 电脑无法升级到Win8.1两种解决方法(已测试)
- SQL SERVER 2005开启xp_cmdshell的方法
- 【学习日志】Python Web开发 TDD方法/“不测试常量”
- 用flask开发个人博客(40)—— Flask中三种测试方法
- vista音频服务尚未开启解决方法
- java测试单个方法 @Test
- A/B测试:实现方法
- 编写一个手机类(Mobile),包括手机品牌(brand)、手机型号(type), 方法包括显示手机信息,并编写测试类进行对象的创建
- 【转】真机测试xcode方法
- JavaScript 执行性能比较和性能测试的方法(作者-万戈)
- FastDFS详细安装步骤,测试;Nginx中配置FastDFS,并提供优化,下载方法,楼主已测