Python numpy生成矩阵、串联矩阵
2016-05-11 13:13
761 查看
import numpy
生成numpy矩阵的几个相关函数:
numpy.array()
numpy.zeros()
numpy.ones()
numpy.eye()
串联生成numpy矩阵的几个相关函数:
numpy.array()
numpy.row_stack()
numpy.column_stack()
numpy.reshape()
生成numpy矩阵的几个相关函数:
numpy.array()
numpy.zeros()
numpy.ones()
numpy.eye()
串联生成numpy矩阵的几个相关函数:
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 >>>
相关文章推荐
- Python numpy 平方、乘方和平方根函数
- Python运行报错IndentationError: unindent does not match any outer indentation level
- Python实现杨辉三角形
- Python tar.gz格式压缩、解压
- Python获取当前目录下所有文件的绝对路径并存储在文件中
- Python enumerate
- Python开发环境配置及工具库安装
- Python学习笔记:条件、循环
- python 多行匹配
- Python的functools模块
- python self introspection
- Python匹配中文的正则表达式
- Python模块常用的几种安装方式
- Python3使用requests发闪存的方法
- 设置TAB键为4个空格(为了写Python脚本)
- 在python3.X下修改了HTMLTestRunner.py
- 使用python创建excel表格 --- XlsxWriter模板详解
- Python开发工具PyCharm个性化设置
- 《机器学习实战》AdaBoost算法的分析与实现
- python脚本测试局域网中的电脑是否连通