这里有新鲜出炉的 Python3 Cookbook 中文版,程序狗速度看过来!
Python 编程语言
Python 是一种面向对象,解释型计算机程序设计语言,由 Guido van Rossum 于 1989 年底发明,第一个公开发行版发行于 1991 年.Python 语法简洁而清晰,具有丰富和强大的类库.它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是 C/C++)很轻松地联结在一起.
这篇文章主要介绍了 Python numpy 生成矩阵,串联矩阵代码分享,具有一定参考价值,需要的朋友可以了解下.
import numpy
生成 numpy 矩阵的几个相关函数:
串联生成 numpy 矩阵的几个相关函数:
numpy.array()
numpy.zeros()
numpy.ones()
numpy.eye()
python 生成 1 行四列全 2 矩阵
numpy.array()
numpy.row_stack()
numpy.column_stack()
numpy.reshape()>>> 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
>>>
print np.ones((1,4))*2
总结
以上就是本文关于 Python numpy 生成矩阵,串联矩阵代码分享的全部内容,希望对大家有所帮助.感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出.感谢朋友们对本站的支持!
来源: http://www.phperz.com/article/18/0131/361491.html