numpy 学习笔记
numpy 学习笔记
导入 numpy 包
import numpy as np
声明 ndarray 的几种方法
方法一,从list中创建
l = [[1,2,3], [4,5,6], [7,8,9]]
matrix = np.array(l)
print(matrix)
[[1 2 3]
[4 5 6]
[7 8 9]]
方法二,指定维度,不赋值
matrix = np.ndarray(shape=(3,4))
print(matrix)
[[9.66308774e-312 2.47032823e-322 0.00000000e+000 0.00000000e+000]
[1.89146896e-307 2.42336543e-057 5.88854416e-091 9.41706373e-047]
[5.44949034e-067 1.46609735e-075 3.99910963e+252 3.43567991e+179]]
由上述的输出可见,矩阵内部的值未初始化,其实这都是原来对应内存地址中的数值
方法三,指定维度,初始化成全零的矩阵
matrix = np.zeros(shape=[3,4])
print(matrix)
[[0. 0. 0. 0.]
[0. 0. 0. 0.]
[0. 0. 0. 0.]]
方法四,使用默认参数,赋值成从0至arange的一组数
使用默认参数(arange),生成从0至arange的一组数据
matrix = np.arange(12).reshape(3,4)
print(matrix)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
方法五,生成随机数数组
arr = np.random.random((1,5)) # 生成 1 行 5 列的一组数
[[ 2.42219258 0.67773029 5.412364 6.21824333 1.2890334 ]]
数值计算
操作全部元素
乘法
print(matrix)
print("after times 10 on every elements:")
print(matrix * 10)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
after times 10 on every elements:
[[ 0 10 20 30]
[ 40 50 60 70]
[ 80 90 100 110]]
加法
print(matrix)
print("after plus 10 on every elements:")
print(matrix + 10)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
after plus 10 on every elements:
[[10 11 12 13]
[14 15 16 17]
[18 19 20 21]]
操作部分元素
print(matrix)
print("after times 10 on every elements:")
print(matrix[1] * 10)
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
after times 10 on every elements:
[40 50 60 70]
计算矩阵的秩
m = np.array([[1,2,3], [0,1,2], [0,0,1]])
np.linalg.matrix_rank(m, tol=None)
output:
3
索引部分元素
取一行数据
print(matrix)
print("a line of a matrix:")
print(matrix[1])
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
a line of a matrix:
[4 5 6 7]
取一列数据
以行的形式返回,得到一个行向量
print(matrix)
print("a column of a matrix:")
print(matrix[:,1])
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
a column of a matrix:
[1 5 9]
以列的形式返回,得到一个列向量
print(matrix)
print("a column of a matrix:")
print(matrix[:,1:2])
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
a column of a matrix:
[[1]
[5]
[9]]
类型转换
astype 方法可以完成类型转换
>>> import numpy as np
>>> x = np.array([0.1, 0.2, 1.2])
>>> x.astype('int')
array([0, 0, 1])
numpy 转 list
numpy 变量自带 tolist 方法
>>> a = np.array([[1, 2], [3, 4]])
>>> a.tolist()
[[1, 2], [3, 4]]
参考资料
《利用python进行数据分析》. https://book.douban.com/subject/25779298/
Numpy. Quickstart tutorial. https://docs.scipy.org/doc/numpy/user/quickstart.html
numpy 学习笔记的更多相关文章
- NumPy学习笔记 三 股票价格
NumPy学习笔记 三 股票价格 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.&l ...
- NumPy学习笔记 二
NumPy学习笔记 二 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分 ...
- NumPy学习笔记 一
NumPy学习笔记 一 <NumPy学习笔记>系列将记录学习NumPy过程中的动手笔记,前期的参考书是<Python数据分析基础教程 NumPy学习指南>第二版.<数学分 ...
- Numpy学习笔记(下篇)
目录 Numpy学习笔记(下篇) 一.Numpy数组的合并与分割操作 1.合并操作 2.分割操作 二.Numpy中的矩阵运算 1.Universal Function 2.矩阵运算 3.向量和矩阵运算 ...
- Numpy学习笔记(上篇)
目录 Numpy学习笔记(上篇) 一.Jupyter Notebook的基本使用 二.Jpuyter Notebook的魔法命令 1.%run 2.%timeit & %%timeit 3.% ...
- Python数据分析:Numpy学习笔记
Numpy学习笔记 ndarray多维数组 创建 import numpy as np np.array([1,2,3,4]) np.array([1,2,3,4,],[5,6,7,8]) np.ze ...
- 数据分析之Pandas和Numpy学习笔记(持续更新)<1>
pandas and numpy notebook 最近工作交接,整理电脑资料时看到了之前的基于Jupyter学习数据分析相关模块学习笔记.想着拿出来分享一下,可是Jupyter导出来h ...
- numpy学习笔记Ⅰ
一直被numpy和matplotlib困扰,打算好好学习一下,也是从自己的观点,学对自己帮助最大的部分 主要参考<https: www.runoob.com="" numpy ...
- Python numpy学习笔记(一)
下边代码是关于numpy的一些基本用法,包括数组和矩阵操作等... import numpy as np print "<== print version ==>" p ...
随机推荐
- 用Partimage创建或恢复分区备份
1 Preliminary Note Partimage is part of the system rescue CD found on http://www.sysresccd.org which ...
- 【漏洞公告】高危:Windows系统 SMB/RDP远程命令执行漏洞
2017年4月14日,国外黑客组织Shadow Brokers发出了NSA方程式组织的机密文档,包含了多个Windows 远程漏洞利用工具,该工具包可以可以覆盖全球70%的Windows服务器,为了确 ...
- sqli-labs(十)(过滤注释符)
第二十三关: 这关还是一个GET型.字符串.单引符号.的有报错的sql注入,输入?id=1' ,页面会报错 我们继续按照之前的套路来,先输入?id=1' or '1'='1 页面正常显示,说明这个地 ...
- gitlab4.0_工程提交
一,环境 gitlab linux系统 IP :10.2.177.31 ==>(我已经申请了一个账户 A@A) 客户端 windows系统 IP:10.2.256. ...
- 1.display:flex布局笔记
/*display:flex布局方式主要运用于垂直居中的效果*/ 一.Flex译为Flexible Box(弹性盒子),任何一个容器都可以指定为Flex布局 注:设置为Flex布局之后,子元素的flo ...
- Bukkit插件编程中.yml配置文件的创建和读取
package com.sklm.config; import java.io.BufferedOutputStream; import java.io.BufferedReader; import ...
- Lua之table
Lua table(表) 参考:http://www.runoob.com/lua/lua-tables.html table 是 Lua 的一种数据结构用来帮助我们创建不同的数据类型,如:数字.字典 ...
- 一 js数据类型
一.简单的数据对象 ------1.小数 var fNum = 1.02; ------2.整数 var iNum = 1; ------3.逻辑变量 var bNum = true; 二.复杂的数据 ...
- VS2013打包程序步骤
VS自带的打包程序默认是没有安装的,如果有打包的需要,需要自己去下载一个安装程序 1.右击解决方案,选择添加项目,在打开的对话框中找到[已安装]-[模板]-[其他项目]-[安装和部署],如图示.第一 ...
- BestCoder Round #55 ($)
C 构造一个矩阵,然后采用矩阵快速幂 #include <iostream> #include <algorithm> #include <string.h> #i ...