Numpy 小结
Python 真火来学习一下,先来看一个库 NumPy。NumPy是Python语言的一个扩充程序库。支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。
1. 读取文件
numpy.genfromtxt() 用于读取 txt 文件,其中传入的参数依次为:
- 需要读取的 txt 文件位置,此处文件与程序位于同一目录下
- 分割的标记
- 转换类型,如果文件中既有文本类型也有数字类型,就先转成文本类型
help(numpy.genfromtxt)用于查看帮助文档:
如果不想看 API 可以启动一个程序用 help 查看指令的详细用法
Python
1
2
3
4
5
6
|
import numpy
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",",dtype=str)
print(type(world_alcohol))
print(world_alcohol)
print(help(numpy.genfromtxt))
|
2. 构造 ndarray
numpy.array()构造 ndarray
numpy.array()中传入数组参数,可以是一维的也可以是二维三维的。numpy 会将其转变成 ndarray 的结构。
Python
1
2
|
vector = numpy.array([1,2,3,4])
matrix = numpy.array([[1,2,3],[4,5,6]])
|
传入的参数必须是同一结构,不是同一结构将发生转换。
Python
1
2
3
|
vector = numpy.array([1,2,3,4])
array([1, 2, 3, 4])
|
均为 int 类型
Python
1
2
3
|
vector = numpy.array([1,2,3,4.0])
array([ 1., 2., 3., 4.])
|
转为浮点数类型
Python
1
2
3
|
vector = numpy.array([1,2,'3',4])
array(['1', '2', '3', '4'],dtype='<U21')
|
转为字符类型
利用 .shape 查看结构
能够了解 array 的结构,debug 时通过查看结构能够更好地了解程序运行的过程。
Python
1
2
3
4
|
print(vector.shape)
print(matrix.shape)
(4,)
(2, 3)
|
利用 dtype 查看类型
Python
1
2
3
4
|
vector = numpy.array([1,2,3,4])
vector.dtype
dtype('int64')
|
ndim 查看维度
一维
Python
1
2
3
4
|
vector = numpy.array([1,2,3,4])
vector.ndim
1
|
二维
Python
1
2
3
4
5
6
|
matrix = numpy.array([[1,2,3],
[4,5,6],
[7,8,9]])
matrix.ndim
2
|
size 查看元素数量
Python
1
2
|
matrix.size
9
|
3. 获取与计算
numpy 能使用切片获取数据
Python
1
2
3
|
matrix = numpy.array([[1,2,3],
[4,5,6],
[7,8,9]])
|
根据条件获取
numpy 能够依次比较 vector 和元素之间是否相同
Python
1
2
3
4
|
vector = numpy.array([5, 10, 15, 20])
vector == 10
array([False, True, False, False], dtype=bool)
|
根据返回值获取元素
Python
1
2
3
4
5
6
7
|
vector = numpy.array([5, 10, 15, 20])
equal_to_ten = (vector == 10)
print(equal_to_ten)
print(vector[equal_to_ten])
[False True False False]
[10]
|
进行运算之后获取
Python
1
2
|
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_and_five = (vector == 10) & (vector == 5)
|
Python
1
2
|
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5)
|
类型转换
将整体类型进行转换
Python
1
2
3
4
5
6
7
|
vector = numpy.array([5, 10, 15, 20])
print(vector.dtype)
vector = vector.astype(str)
print(vector.dtype)
int64
<U21
|
求和
sum() 能够对 ndarray 进行各种求和操作,比如分别按行按列进行求和
Python
1
2
3
4
5
6
7
8
9
10
|
matrix = numpy.array([[1,2,3],
[4,5,6],
[7,8,9]])
print(matrix.sum())
print(matrix.sum(1))
print(matrix.sum(0))
45
[ 6 15 24]
[12 15 18]
|
sum(1) 是 sum(axis=1)) 的缩写,1表示按照 x轴方向求和,0表示按照y轴方向求和
4. 常用函数
reshape
生成从 0-14 的 15 个数字,使用 reshape(3,5) 将其构造成一个三行五列的 array。
Python
1
2
3
4
5
6
7
|
import numpy as np
arr = np.arange(15).reshape(3, 5)
arr
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
|
zeros
生成指定结构的默认为 0. 的 array
Python
1
2
3
4
5
|
np.zeros ((3,4))
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
|
ones
生成一个三维的 array,通过 dtype 指定类型
Python
1
2
3
4
5
6
7
8
9
|
np.ones( (2,3,4), dtype=np.int32 )
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]])
|
range
指定范围和数值间的间隔生成 array,注意范围包左不包右
Python
1
2
3
|
np.arange(0,10,2)
array([0, 2, 4, 6, 8])
|
random 随机数
生成指定结构的随机数,可以用于生成随机权重
Python
1
2
3
4
|
np.random.random((2,3))
array([[ 0.86166627, 0.37756207, 0.94265883],
[ 0.9768257 , 0.96915312, 0.33495431]])
|
5. ndarray 运算
元素之间依次相减相减
Python
1
2
3
4
5
|
a = np.array([10,20,30,40])
b = np.array(4)
a - b
array([ 6, 16, 26, 36])
|
乘方
Python
1
2
|
a**2
array([ 100, 400, 900, 1600])
|
开根号
Python
1
2
3
4
|
np.sqrt(B)
array([[ 1.41421356, 0. ],
[ 1.73205081, 2. ]])
|
e 求方
Python
1
2
3
4
|
np.exp(B)
array([[ 7.3890561 , 1. ],
[ 20.08553692, 54.59815003]])
|
向下取整
Python
1
2
3
4
5
|
a = np.floor(10*np.random.random((2,2)))
a
array([[ 0., 0.],
[ 3., 6.]])
|
行列变换
Python
1
2
3
4
|
a.T
array([[ 0., 3.],
[ 0., 6.]])
|
变换结构
Python
1
2
3
4
|
a.resize(1,4)
a
array([[ 0., 0., 3., 6.]])
|
6. 矩阵运算
矩阵之间的运算
Python
1
2
3
4
|
A = np.array( [[1,1],
[0,1]] )
B = np.array( [[2,0],
[3,4]] )
|
对应位置一次相乘
Python
1
2
3
4
|
A*B
array([[2, 0],
[0, 4]])
|
矩阵乘法
Python
1
2
3
4
5
|
print (A.dot(B))
print(np.dot(A,B))
[[5 4]
[3 4]]
|
横向相加
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
|
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print(a)
print(b)
print(np.hstack((a,b)))
[[ 2. 3.]
[ 9. 3.]]
[[ 8. 1.]
[ 0. 0.]]
[[ 2. 3. 8. 1.]
[ 9. 3. 0. 0.]]
|
纵向相加
Python
1
2
3
4
5
6
|
print(np.vstack((a,b)))
[[ 2. 3.]
[ 9. 3.]
[ 8. 1.]
[ 0. 0.]]
|
矩阵分割
Python
1
2
3
4
|
#横向分割
print( np.hsplit(a,3))
#纵向风格
print(np.vsplit(a,3))
|
7. 复制的区别
地址复制
通过 b = a 复制 a 的值,b 与 a 指向同一地址,改变 b 同时也改变 a。
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
a = np.arange(12)
b = a
print(a is b)
print(a.shape)
print(b.shape)
b.shape = (3,4)
print(a.shape)
print(b.shape)
True
(12,)
(12,)
(3, 4)
(3, 4)
|
复制值
通过 a.view() 仅复制值,当对 c 值进行改变会改变 a 的对应的值,而改变 c 的 shape 不改变 a 的 shape
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
a = np.arange(12)
c = a.view()
print(c is a)
c.shape = 2,6
c[0,0] = 9999
print(a)
print(c)
False
[9999 1 2 3 4 5 6 7 8 9 10 11]
[[9999 1 2 3 4 5]
[ 6 7 8 9 10 11]]
|
完整拷贝
a.copy() 进行的完整的拷贝,产生一份完全相同的独立的复制
Python
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
a = np.arange(12)
c = a.copy()
print(c is a)
c.shape = 2,6
c[0,0] = 9999
print(a)
print(c)
False
[ 0 1 2 3 4 5 6 7 8 9 10 11]
[[9999 1 2 3 4 5]
[ 6 7 8 9 10 11]]
|
Numpy 小结的更多相关文章
- numpy小结
<python数据科学>笔记 在线版地址:https://github.com/jakevdp/PythonDataScienceHandbook 1.常用np简写 import num ...
- numpy小结(一)
1.np.zero(10) 创建一个包含10个元素的一维数组 np.ones((10,10)) 创建一个包含10*10个元素1的二维数组 2.np.arange(10,50) ...
- python 基础及资料汇总
Python 包.模块.类以及代码文件和目录的一种管理方案 Numpy 小结 用 Python 3 的 async / await 做异步编程 K-means 在 Python 中的实现 ...
- numpy用法小结
前言 个人感觉网上对numpy的总结感觉不够详尽细致,在这里我对numpy做个相对细致的小结吧,在数据分析与人工智能方面会有所涉及到的东西在这里都说说吧,也是对自己学习的一种小结! numpy用法的介 ...
- Numpy 用法小结
1. asarray 函数 可以将输入数据转化为矩阵格式. 输入数据可以是(列表,元组,列表的列表,元组的元组,元组的列表等这些数组形式). >>> asarray([(1,2,3 ...
- numpy.random模块用法小结
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/9751471.html 1.np.random.random()函数参数 np.random.r ...
- scikit-learn随机森林调参小结
在Bagging与随机森林算法原理小结中,我们对随机森林(Random Forest, 以下简称RF)的原理做了总结.本文就从实践的角度对RF做一个总结.重点讲述scikit-learn中RF的调参注 ...
- scikit-learn 梯度提升树(GBDT)调参小结
在梯度提升树(GBDT)原理小结中,我们对GBDT的原理做了总结,本文我们就从scikit-learn里GBDT的类库使用方法作一个总结,主要会关注调参中的一些要点. 1. scikit-learn ...
- scikit-learn Adaboost类库使用小结
在集成学习之Adaboost算法原理小结中,我们对Adaboost的算法原理做了一个总结.这里我们就从实用的角度对scikit-learn中Adaboost类库的使用做一个小结,重点对调参的注意事项做 ...
随机推荐
- poj 2932 Coneology (扫描线)
题意 平面上有N个两两不相交的圆,求全部最外层的,即不被其它圆包括的圆的个数并输出 思路 挑战程序竞赛P259页 代码 /* ************************************* ...
- C 标准库 - <errno.h>
C 标准库 - <errno.h> 简介 C 标准库的 errno.h 头文件定义了整数变量 errno,它是通过系统调用设置的,在错误事件中的某些库函数表明了什么发生了错误.该宏扩展为类 ...
- php性能监控扩展xhprof
XHProf是facebook开源出来的一个php轻量级的性能分析工具,跟Xdebug类似,但性能开销更低,还可以用在生产环境中,也可以由程序开 关来控制是否进行profile.总体来说是个不错的工具 ...
- mysql性能优化-慢查询分析、优化索引和配置 MySQL索引介绍
MySQL索引介绍 聚集索引(Clustered Index)----叶子节点存放整行记录辅助索引(Secondary Index)----叶子节点存放row identifier-------Inn ...
- C#文件操作与编程
一:驱动器System.IO 软盘,优盘,光盘,硬盘 DriveInfo/DriveType DriveInfo:确定有关驱动器的信息:盘符,类型,可用空间 DriveType:确定DriveInfo ...
- 基于Redis缓存的Session共享测试(转)
本机ip为192.168.1.101 1.准备测试环境 两个Tomcat 在Eclipse中新建2个Servers,指定对应的Tomcat,端口号错开. Tomcat1(18005.18080.180 ...
- 自己动手写CPU之第七阶段(5)——流水线暂停机制的设计与实现
将陆续上传本人写的新书<自己动手写CPU>,今天是第28篇.我尽量每周四篇 China-pub的预售地址例如以下(有文件夹.内容简单介绍.前言): http://product.china ...
- ruby on rails模拟HTTP请求错误发生:end of file reached
在文章 Ruby On Rails中REST API使用演示样例--基于云平台+云服务打造自己的在线翻译工具 中,利用ruby的Net::HTTP发起http请求訪问IBM Bluemix上的sour ...
- Swift高阶函数介绍(闭包、Map、Filter、Reduce)
Swift语言有非常多函数式编程的特性.常见的map,reduce,filter都有,初看和python几乎相同,以下简介下 闭包介绍: 闭包是自包括的功能代码块,能够在代码中使用或者用来作为參数传值 ...
- Android Material Design 中文版
http://www.google.com/design/spec/animation/authentic-motion.html http://www.oschina.net/question/14 ...