Python Numpy学习笔记
来源:互联网 发布:清镇市各乡镇人口数据 编辑:程序博客网 时间:2024/06/04 19:58
Python Numpy学习笔记
1. 数据类型
2. 创建矩阵
>>>np.zeros((2, 3)) array([[ 0., 0., 0.], [ 0., 0., 0.]])>>>np.arange(10) array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])>>>np.arange(2, 10, dtype=np.float) array([ 2., 3., 4., 5., 6., 7., 8., 9.])>>>np.arange(2, 3, 0.1)array([ 2. , 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9])>>> np.linspace(1., 4., 6)array([ 1. , 1.6, 2.2, 2.8, 3.4, 4. ])>>> np.indices((3,3))array([[[0, 0, 0], [1, 1, 1], [2, 2, 2]], [[0, 1, 2], [0, 1, 2], [0, 1, 2]]])
3. 索引
>>> x = np.arange(10)>>> x[2]2>>> x[-2]8>>> x.shape = (2,5) # now x is 2-dimensional>>> x[1,3]8>>> x[1,-1]9>>> x[0]array([0, 1, 2, 3, 4])>>> x[0][2]2>>> x = np.arange(10)>>> x[2:5]array([2, 3, 4])>>> x[:-7]array([0, 1, 2])>>> x[1:7:2]array([1, 3, 5])>>> y = np.arange(35).reshape(5,7)>>> y[1:5:2,::3]array([[ 7, 10, 13],[21, 24, 27]])>>> x = np.arange(10,1,-1)>>> xarray([10, 9, 8, 7, 6, 5, 4, 3, 2])>>> x[np.array([3, 3, 1, 8])]array([7, 7, 9, 2])>>> x[np.array([3,3,-3,8])]array([7, 7, 4, 2])>>> x[np.array([3, 3, 20, 8])]<type ’exceptions.IndexError’>: index 20 out of bounds 0<=index<9>>> x[np.array([[1,1],[2,3]])]array([[9, 9],[8, 7]])>>> y[np.array([0,2,4]), np.array([0,1,2])]array([ 0, 15, 30])>>> y[np.array([0,2,4]), np.array([0,1])]<type ’exceptions.ValueError’>: shape mismatch: objects cannot bebroadcast to a single shape>>> y[np.array([0,2,4]), 1]array([ 1, 15, 29])>>> y[np.array([0,2,4])]array([[ 0, 1, 2, 3, 4, 5, 6],[14, 15, 16, 17, 18, 19, 20],[28, 29, 30, 31, 32, 33, 34]])>>> b = y>20>>> y[b]array([21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34])>>> b[:,5] # use a 1-D boolean whose first dim agrees with the first dim of yarray([False, False, False, True, True], dtype=bool)>>> y[b[:,5]]array([[21, 22, 23, 24, 25, 26, 27],[28, 29, 30, 31, 32, 33, 34]])>>> x = np.arange(30).reshape(2,3,5)>>> xarray([[[ 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, 27, 28, 29]]])>>> b = np.array([[True, True, False], [False, True, True]])>>> x[b]array([[ 0, 1, 2, 3, 4],[ 5, 6, 7, 8, 9],[20, 21, 22, 23, 24],[25, 26, 27, 28, 29]])
4. 矩阵局部赋值
>>> x = np.arange(10)>>> x[2:7] = 1>>> x[2:7] = np.arange(5)>>> x[1] = 1.2>>> x[1]1>>> x[1] = 1.2j<type ’exceptions.TypeError’>: can’t convert complex to long; uselong(abs(z))>>> x = np.arange(0, 50, 10)>>> xarray([ 0, 10, 20, 30, 40])>>> x[np.array([1, 1, 3, 1])] += 1>>> xarray([ 0, 11, 20, 31, 40])
0 0
- 【python】numpy学习笔记
- Python Numpy学习笔记
- Python Numpy 学习笔记
- python numpy 库学习笔记
- python numpy 学习笔记(9)
- python numpy 学习笔记(10)
- CS231n学习笔记-1.Python&Numpy学习
- [Python] Numpy学习笔记(一)
- [Python] Numpy学习笔记(二)
- python学习笔记之numpy入门
- python学习笔记(2)--------Numpy
- Python Numpy 学习笔记(2)
- Python Numpy 学习笔记(3)
- 【python学习笔记】16:numpy数组四则运算
- 【python学习笔记】17:numpy数组排序
- python numpy学习笔记(4)
- python numpy学习笔记(5)
- 【python学习笔记】21:numpy傅里叶变换
- 机器学习之2-多变量线性回归(Linear Regression with Multiple Variables)
- ActiveMQ学习教程(一)——安装与示例
- [16]匿名函数和闭包
- John Z. Sonmez(约翰 Z.森梅兹【美】)之《软技能:代码外的生存指南》
- 毕业设计那点事 — 源码安装Mysql
- Python Numpy学习笔记
- Spring第一天
- Maven项目中使用jetty启动项目[今后会不定期更新矫正本文]
- Python学习之循环
- 事件的发布和订阅 以及消息队列
- 支付宝支付-扫码支付详解
- Docker常用命令(非swarm模式)
- hdu I Hate It (线段树裸题)
- 转:有关常量的知识点