python中numpy.ndarray.shape的用法
2023-09-11 14:17:11 时间
今天用到了shape,就顺便学习一下,这个shape的作用就是要把矩阵进行行列转换,请看下面的几个例子就明白了:
>>> import numpy as np >>> x = np.array([1,2,3,4]) >>> x.shape (4,) >>> y = np.zeros([2,3,4]) >>> y.shape (2, 3, 4) >>> y.shape = (3,8) >>> y array([[0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0., 0., 0.]]) >>> y.shape = (3,6) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: cannot reshape array of size 24 into shape (3,6) >>> y.shape = (4,6) >>> y.shape (4, 6) >>> y array([[0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0.], [0., 0., 0., 0., 0., 0.]]) >>> y = np.zeros([2,3,4]) >>> y array([[[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]], [[0., 0., 0., 0.], [0., 0., 0., 0.], [0., 0., 0., 0.]]]) >>> y.shape = (-1,2) >>> y array([[0., 0.], [0., 0.], [0., 0.], [0., 0.], [0., 0.], [0., 0.], [0., 0.], [0., 0.], [0., 0.], [0., 0.], [0., 0.], [0., 0.]]) >>> x.shape (4,) >>> x.shape = (-1,2) >>> x array([[1, 2], [3, 4]]) >>>
参考文档: https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.shape.html
相关文章
- 单柱柱形图与多柱柱形图的绘制 基于python-matplotlib库
- python list类型输出不规范,越界输出,可以转换成numpy再输出
- python - 如何使用 Docker 运行多个 Python 脚本和一个可执行文件?
- python数据分析有什么用
- 详解Python-Numpy库的函数diagonal()【并附函数diagonal()与函数diag()的区别】
- MATLAB、C++、OpenCV、Python、Python-Numpy近似值(取整)求取函数总结【round()、ceil()、floor()、fix()】
- 《python 与数据挖掘 》一 第2章 Python基础入门
- python实现K-Means算法
- gyp ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- Python爬虫技术--基础篇--面向对象编程(下)
- 【Python】时间转换(可用于计算耗时场景)
- 【Python】【PyPI】twine模块打包python项目上传pypi
- Python 进制转换 二进制 八进制 十进制 十六进制
- python模块之HTMLParser(原理很大程度上就是对类构造的熟练运用)
- A million requests per second with Python
- 《NLTK基础教程——用NLTK和Python库构建机器学习应用》——2.1 何谓文本歧义
- centos 7下独立的python 2.7环境安装
- 数据科学必备Python使用Numpy方法汇总
- python : 将txt文件中的数据读为numpy数组或列表
- ( Ubuntu环境 ) Vim下一键运行python代码
- 188、【栈与队列】leetcode ——84. 柱状图中最大的矩形:暴力解法+单调栈(C++/Python版本)
- python和numpy matplotlib版本匹配,以及安装指定版本库
- python自动化扫描,多线程枚举获取wifi信息,让你走在任何一个地方都能上网
- Python numpy shape 矩阵