Python numpy生成矩阵、串联矩阵
2016-12-13 13:58
573 查看
版权声明:本文为博主原创文章,未经博主允许不得转载。
import numpy
生成numpy矩阵的几个相关函数:
numpy.array()
numpy.zeros()
numpy.ones()
numpy.eye()
串联生成numpy矩阵的几个相关函数:
numpy.array()
numpy.row_stack()
numpy.column_stack()
numpy.reshape()
[python]
view plain
copy
print?
>>> import numpy
>>> numpy.eye(3)
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> numpy.zeros(3)
array([ 0., 0., 0.])
>>> numpy.ones(3)
array([ 1., 1., 1.])
>>> x1 = numpy.array((1, 2, 3))
>>> x1
array([1, 2, 3])
>>> x2 = numpy.array([4, 5, 6])
>>> x2
array([4, 5, 6])
>>> x3 = numpy.array((x1, x2))
>>> x3
array([[1, 2, 3],
[4, 5, 6]])
>>> x4 = x3.reshape(2, 3)
>>> x4
array([[1, 2, 3],
[4, 5, 6]])
>>> x4 = x3.reshape(3, 2)
>>> x4
array([[1, 2],
[3, 4],
[5, 6]])
>>> x5 = numpy.row_stack(x1, x2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: vstack() takes exactly 1 argument (2 given)
>>> x5 = numpy.row_stack((x1, x2))
>>> x5
array([[1, 2, 3],
[4, 5, 6]])
>>> x6 = numpy.row_stack([x1, x2])
>>> x6
array([[1, 2, 3],
[4, 5, 6]])
>>> x7 = numpy.row_stack((x6, x2))
>>> x7
array([[1, 2, 3],
[4, 5, 6],
[4, 5, 6]])
>>> x7[0]
array([1, 2, 3])
>>> x7[1]
array([4, 5, 6])
>>> x7[2]
array([4, 5, 6])
>>> x8 = numpy.column_stack([x1, x2, x1, x2])
>>> x8
array([[1, 4, 1, 4],
[2, 5, 2, 5],
[3, 6, 3, 6]])
>>> x8[0]
array([1, 4, 1, 4])
>>> x8[1]
array([2, 5, 2, 5])
>>> x8[2]
array([3, 6, 3, 6])
>>> x8[3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: index 3 is out of bounds for axis 0 with size 3
>>> x8[0][3]
4
>>>
版权声明:本文为博主原创文章,未经博主允许不得转载。
import numpy
生成numpy矩阵的几个相关函数:
numpy.array()
numpy.zeros()
numpy.ones()
numpy.eye()
串联生成numpy矩阵的几个相关函数:
numpy.array()
numpy.row_stack()
numpy.column_stack()
numpy.reshape()
[python]
view plain
copy
print?
>>> import numpy
>>> numpy.eye(3)
array([[ 1., 0., 0.],
[ 0., 1., 0.],
[ 0., 0., 1.]])
>>> numpy.zeros(3)
array([ 0., 0., 0.])
>>> numpy.ones(3)
array([ 1., 1., 1.])
>>> x1 = numpy.array((1, 2, 3))
>>> x1
array([1, 2, 3])
>>> x2 = numpy.array([4, 5, 6])
>>> x2
array([4, 5, 6])
>>> x3 = numpy.array((x1, x2))
>>> x3
array([[1, 2, 3],
[4, 5, 6]])
>>> x4 = x3.reshape(2, 3)
>>> x4
array([[1, 2, 3],
[4, 5, 6]])
>>> x4 = x3.reshape(3, 2)
>>> x4
array([[1, 2],
[3, 4],
[5, 6]])
>>> x5 = numpy.row_stack(x1, x2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: vstack() takes exactly 1 argument (2 given)
>>> x5 = numpy.row_stack((x1, x2))
>>> x5
array([[1, 2, 3],
[4, 5, 6]])
>>> x6 = numpy.row_stack([x1, x2])
>>> x6
array([[1, 2, 3],
[4, 5, 6]])
>>> x7 = numpy.row_stack((x6, x2))
>>> x7
array([[1, 2, 3],
[4, 5, 6],
[4, 5, 6]])
>>> x7[0]
array([1, 2, 3])
>>> x7[1]
array([4, 5, 6])
>>> x7[2]
array([4, 5, 6])
>>> x8 = numpy.column_stack([x1, x2, x1, x2])
>>> x8
array([[1, 4, 1, 4],
[2, 5, 2, 5],
[3, 6, 3, 6]])
>>> x8[0]
array([1, 4, 1, 4])
>>> x8[1]
array([2, 5, 2, 5])
>>> x8[2]
array([3, 6, 3, 6])
>>> x8[3]
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: index 3 is out of bounds for axis 0 with size 3
>>> x8[0][3]
4
>>>
相关文章推荐
- Python numpy生成矩阵、串联矩阵代码分享
- Python numpy生成矩阵、串联矩阵
- Python 生成矩阵
- Python和numpy生成随机数
- 利用python的numpy创建矩阵并对其赋值
- Python基础-numpy中矩阵增加行或列
- python numpy矩阵信息,shape,size,dtype
- Python之路——numpy各函数简介之生成数组函数(Array creation routines)
- [转]numpy线性代数基础 - Python和MATLAB矩阵处理的不同
- python numpy模块玩转矩阵与科学计算
- python numpy包的数组与矩阵一些差异
- Python中矩阵库Numpy基本操作
- python numpy包---矩阵运算
- 关于用mapreduce做kmeans聚类以及python的numpy和list做矩阵、向量乘法的速度对比
- python numpy矩阵和数组的转换
- python ——numpy各函数简介之生成数组函数(Array creation routines)
- numpy线性代数基础 - Python和MATLAB矩阵处理的不同
- Python Numpy的数组array和矩阵matrix
- python numpy 矩阵操作
- 【矩阵分解】Python下基于Numpy的四种矩阵基本分解的实现