numpy基础代码操练
In [20]: b[0,:,1] Out[20]: array([1, 5, 9]) In [21]: b[0,:,1] Out[21]: array([1, 5, 9]) In [22]: b[0,:,-1] Out[22]: array([ 3, 7, 11]) In [23]: b[0,::-1, -1] Out[23]: array([11, 7, 3]) In [24]: b[0,::-2, -1] Out[24]: array([11, 3]) In [25]: b[::-1] Out[25]: array([[[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]], [[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]]]) In [26]: b Out[26]: array([[[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11]], [[12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]]) In [27]: b.ravel() Out[27]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]) In [28]: b.flatten() Out[28]: array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]) In [29]: b.shape = (6,4) In [30]: b Out[30]: array([[ 0, 1, 2, 3], [ 4, 5, 6, 7], [ 8, 9, 10, 11], [12, 13, 14, 15], [16, 17, 18, 19], [20, 21, 22, 23]]) In [31]: b.transpose() Out[31]: array([[ 0, 4, 8, 12, 16, 20], [ 1, 5, 9, 13, 17, 21], [ 2, 6, 10, 14, 18, 22], [ 3, 7, 11, 15, 19, 23]]) In [32]: b.resize(2,12)) File "<ipython-input-32-91b83b9b6cad>", line 1 b.resize(2,12)) ^ SyntaxError: invalid syntax In [33]: b.resize((2,12)) In [34]: b Out[34]: array([[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11], [12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]]) In [35]: a = arange(3).reshape(3,3) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-35-0863ec9f918e> in <module>() ----> 1 a = arange(3).reshape(3,3) ValueError: cannot reshape array of size 3 into shape (3,3) In [36]: a = arange(9).reshape(3,3) In [37]: a Out[37]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) In [38]: b = 2 * a In [39]: b Out[39]: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) In [40]: hstack((a,b)) Out[40]: array([[ 0, 1, 2, 0, 2, 4], [ 3, 4, 5, 6, 8, 10], [ 6, 7, 8, 12, 14, 16]]) In [41]: concatenate((a,b), axis=1) Out[41]: array([[ 0, 1, 2, 0, 2, 4], [ 3, 4, 5, 6, 8, 10], [ 6, 7, 8, 12, 14, 16]]) In [42]: vstack((a,b)) Out[42]: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) In [43]: concatenate((a,b), axis=0) Out[43]: array([[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8], [ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) In [44]: dstack((a,b)) Out[44]: array([[[ 0, 0], [ 1, 2], [ 2, 4]], [[ 3, 6], [ 4, 8], [ 5, 10]], [[ 6, 12], [ 7, 14], [ 8, 16]]]) In [45]: a Out[45]: array([[0, 1, 2], [3, 4, 5], [6, 7, 8]]) In [46]: hsplit(a, 3) Out[46]: [array([[0], [3], [6]]), array([[1], [4], [7]]), array([[2], [5], [8]])] In [47]: hsplit(a, 3) Out[47]: [array([[0], [3], [6]]), array([[1], [4], [7]]), array([[2], [5], [8]])] In [48]: split(a, 3, axis=1) Out[48]: [array([[0], [3], [6]]), array([[1], [4], [7]]), array([[2], [5], [8]])] In [49]: vsplit(a, 3) Out[49]: [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])] In [50]: split(a, 3, axis=0) Out[50]: [array([[0, 1, 2]]), array([[3, 4, 5]]), array([[6, 7, 8]])] In [51]: c = arange(27).reshape(3, 3, 3) In [52]: c Out[52]: array([[[ 0, 1, 2], [ 3, 4, 5], [ 6, 7, 8]], [[ 9, 10, 11], [12, 13, 14], [15, 16, 17]], [[18, 19, 20], [21, 22, 23], [24, 25, 26]]]) In [53]: dsplit(c,3) Out[53]: [array([[[ 0], [ 3], [ 6]], [[ 9], [12], [15]], [[18], [21], [24]]]), array([[[ 1], [ 4], [ 7]], [[10], [13], [16]], [[19], [22], [25]]]), array([[[ 2], [ 5], [ 8]], [[11], [14], [17]], [[20], [23], [26]]])] In [54]: b Out[54]: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) In [55]: b Out[55]: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) In [56]: b.ndim Out[56]: 2 In [57]: b.size Out[57]: 9 In [58]: b.itemsize Out[58]: 4 In [59]: b.nbytes Out[59]: 36 In [60]: b.size * b.itemsize Out[60]: 36 In [61]: b.resize(6,4) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-61-a30f6357ab78> in <module>() ----> 1 b.resize(6,4) ValueError: cannot resize an array that references or is referenced by another array in this way. Use the resize function In [62]: b.resize(6,6) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-62-5d4e603729e0> in <module>() ----> 1 b.resize(6,6) ValueError: cannot resize an array that references or is referenced by another array in this way. Use the resize function In [63]: b.resize() In [64]: b Out[64]: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) In [65]: b Out[65]: array([[ 0, 2, 4], [ 6, 8, 10], [12, 14, 16]]) In [66]: b.tolist() Out[66]: [[0, 2, 4], [6, 8, 10], [12, 14, 16]] In [67]:
numpy基础代码操练的更多相关文章
- 《利用python进行数据分析》读书笔记--第四章 numpy基础:数组和矢量计算
http://www.cnblogs.com/batteryhp/p/5000104.html 第四章 Numpy基础:数组和矢量计算 第一部分:numpy的ndarray:一种多维数组对象 实话说, ...
- 利用Python进行数据分析——Numpy基础:数组和矢量计算
利用Python进行数据分析--Numpy基础:数组和矢量计算 ndarry,一个具有矢量运算和复杂广播能力快速节省空间的多维数组 对整组数据进行快速运算的标准数学函数,无需for-loop 用于读写 ...
- 《利用Python进行数据分析·第2版》第四章 Numpy基础:数组和矢量计算
<利用Python进行数据分析·第2版>第四章 Numpy基础:数组和矢量计算 numpy高效处理大数组的数据原因: numpy是在一个连续的内存块中存储数据,独立于其他python内置对 ...
- 利用python进行数据分析--numpy基础
随书练习,第四章 NumPy基础:数组和矢量运算 # coding: utf-8 # In[1]: # 加注释的三个方法1.用一对"""括起来要注释的代码块. # 2. ...
- NumPy 基础知识·翻译完成
原文:Numpy Essentials 协议:CC BY-NC-SA 4.0 欢迎任何人参与和完善:一个人可以走的很快,但是一群人却可以走的更远. 在线阅读 ApacheCN 面试求职交流群 7241 ...
- 【学习笔记】 第04章 NumPy基础:数组和矢量计算
前言 正式开始学习Numpy,参考用书是<用Python进行数据清洗>,计划本周五之前把本书读完,关键代码全部实现一遍 NumPy基础:数组和矢量计算 按照书中所示,要搞明白具体的性能差距 ...
- Mysql基础代码(不断完善中)
Mysql基础代码,不断完善中~ /* 启动MySQL */ net start mysql /* 连接与断开服务器 */ mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限 ...
- 利用Python进行数据分析(5) NumPy基础: ndarray索引和切片
概念理解 索引即通过一个无符号整数值获取数组里的值. 切片即对数组里某个片段的描述. 一维数组 一维数组的索引 一维数组的索引和Python列表的功能类似: 一维数组的切片 一维数组的切片语法格式为a ...
- numpy 基础操作
Numpy 基础操作¶ 以numpy的基本数据例子来学习numpy基本数据处理方法 主要内容有: 创建数组 数组维度转换 数据选区和切片 数组数据计算 随机数 数据合并 数据统计计算 In [1]: ...
随机推荐
- 专题训练之LCA
推荐几个博客:https://www.cnblogs.com/JVxie/p/4854719.html Tarjan离线算法的基本思路及其算法实现 https://blog.csdn.net/shah ...
- Python之数据库导入(py3.5)
数据库版本:MySQL Python版本:3.5 之前用想用MySQLdb来着,后来发现py3.5版本不支持,现选择pymysql 现在想将数据库adidas中的表jd_comment读取至pytho ...
- 【题解】Inspection UVa 1440 LA 4597 NEERC 2009
题目传送门:https://vjudge.net/problem/UVA-1440 看上去很像DAG的最小路径覆盖QwQ? 反正我是写了一个上下界网络流,建模方法清晰易懂. 建立源$s$,向每个原图中 ...
- OpenCV---图像直方图
一:直方图的直接使用 from matplotlib import pyplot as plt def plot_demo(image): print(image.ravel()) plt.hist( ...
- nginx 与 tomcat 组合搭建web服务
部分内容转自 http://www.cnblogs.com/naaoveGIS/ 1. Web服务 nginx是常用的web服务器,用于获取静态资源,类似的服务器还有apache. tomcat是基于 ...
- ZOJ 3964 NIM变形
LINK 题意:n堆石子,Alice 和 Bob 轮流取石子,谁不能再取或被对方取完为败.但是对于alice拥有限制:b=0此堆正常无限制:b=1此堆Alice只能取奇数个石子:b=2只能取偶数个石子 ...
- win10本地搭建php运行环境
一.下载搭建环境所需软件,安装顺序也要按照列表顺序安装 1.Vc2015(根据需要安装Vc2012或者Vc2015) Vc2015:https://www.microsoft.com/zh-CN/do ...
- js关闭当前页面跳转新页面
页面代码: <p class="info"><span style="font-weight: bold">所属项目:</span ...
- 【leetcode 简单】第六题 有效的括号
给定一个只包括 '(',')','{','}','[',']' 的字符串,判断字符串是否有效. 有效字符串需满足: 左括号必须用相同类型的右括号闭合. 左括号必须以正确的顺序闭合. 注意空字符串可被认 ...
- VC字体对话框的初始化
本代码需要先添加类成员 LOGFONT lf; void CMyDlg::OnButton3() { // TODO: Add your control notification handler c ...