机器学习笔记--Python之Numpy
来源:互联网 发布:程序员技能的重要性 编辑:程序博客网 时间:2024/06/05 05:32
终于不用看枯燥的数学公式了,虽然比较喜欢数学,但是没有实际的应用也就失去了意义,这里开始学习机器学习中用到的一些库,主要包括了numpy,scipy,matplotlib等。先学习下numpy吧,至于python基础可以看Python的简单语法就差不多了。
1 Numpy简介
Numpy支持高级大量的维度数组与矩阵运算,此外也针对数组运算提供大量的数学函数库。
2 基本功能
快速高效的多维数组对象ndarray 用于对数组执行元素级计算以及直接对数组执行数学运算的函数 用于读写硬盘上基于数组的数据集的工具 线性代数运算、傅里叶变换,以及随机数生成 用于将C、C++、Fortran代码集成到Python的工具 除了为Python提供快速的数组处理能力,NumPy在数据分析方面还有另外一个主要作用,即作为在算法之间传递数据的容器。
3 NumPy数组ndarray
3.1 ndarray属性
NumPy数组的维数称为秩(rank),一维数组的秩为1,二维数组的秩为2,以此类推。在NumPy中,每一个线性的数组称为是一个轴(axes),秩其实是描述轴的数量。比如说,二维数组相当于是两个一维数组,其中第一个一维数组中每个元素又是一个一维数组。所以一维数组就是NumPy中的轴(axes),第一个轴相当于是底层数组,第二个轴是底层数组里的数组。而轴的数量——秩,就是数组的维数。 比较重要ndarray对象属性有:
3.2 ndarray创建
3.2.1 array函数
from numpy import *a = array([1, 2, 3])print aprint a.dtypeb = array([1.1, 2.2, 3.3])print bprint b.dtypec = array([(1, 2, 3), (1.1, 2.2, 3.3)])print cprint c.dtype
运行结果:
[1 2 3]int64[ 1.1 2.2 3.3]float64[[ 1. 2. 3. ] [ 1.1 2.2 3.3]]float64
3.2.2 zeros函数
创建全0的数组
from numpy import *d = zeros((3, 5))print dprint d.dtype
运行结果:
[[ 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0.]]float64
3.2.3 ones函数
创建一个全为1的数组
from numpy import *d = ones((2, 5, 6))print dprint d.dtype
运行结果:
[[[ 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1.]] [[ 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1.] [ 1. 1. 1. 1. 1. 1.]]]float64
3.2.4 empty函数
创建一个内容随机并且依赖与内存状态的数组
from numpy import *d = empty((2, 5))print dprint d.dtype
运行结果:
[[ -2.68156159e+154 -1.73059747e-077 6.93327761e-310 6.93327762e-310 6.93327762e-310] [ 6.93327762e-310 6.93327762e-310 6.93327762e-310 6.93327762e-310 6.93327762e-310]]float64
3.2.5 arange函数
返回一个数列形式的数组
从0开始,差值为5的等差数列:
from numpy import *d = arange(0, 20, 5)print d
运行结果:
[ 0 5 10 15]
从0开始,差值为0.5的等差数列:
from numpy import *d = arange(0, 5, 0.5)print d
运行结果:
[ 0. 0.5 1. 1.5 2. 2.5 3. 3.5 4. 4.5]
3.2.6 linespace函数
从0到3,个数为5个的数列:
from numpy import *d = linspace(0, 3, 5)print d
运行结果如下:
[ 0. 0.75 1.5 2.25 3. ]
3.3 基本运算
3.3.1 基本
算数运算,每个元素的加减乘除
from numpy import *a = array([1, 3, 5, 8])b = arange(4)print aprint bprint a - bprint b**2print 2*a + 3*b
运行结果:
[1 3 5 8][0 1 2 3][1 2 3 5][0 1 4 9][ 2 9 16 25]
3.3.2 乘法
普通*乘法就是各个元素相乘,矩阵乘法用dot函数,可实现矩阵的乘法运算
from numpy import *a = array([[1, 1], [2, 2]])b = array([[2, 0], [1, 2]])print a*bprint dot(a, b)
运行结果:
[[2 0] [2 4]][[3 2] [6 4]]
3.3.3 最大值,最小值,求和
求数列的所有元素中的最大值,最小值,以及求和
from numpy import *a = random.random((3, 2))print aprint a.max()print a.min()print a.sum()
运行结果:
[[ 0.18127389 0.41583799] [ 0.79348563 0.46761359] [ 0.79804995 0.26569052]]0.7980499536050.1812738931652.92195157501
求每一行中的元素的最小值,求和
#coding=utf-8from numpy import *b = arange(9).reshape(3, 3)print b#计算每一列的和print b.sum(axis=0)#每一行的最小值print b.min(axis=1)#计算每一行的累积和print b.cumsum(axis=1)
运行结果如下:
[[0 1 2] [3 4 5] [6 7 8]][ 9 12 15][0 3 6][[ 0 1 3] [ 3 7 12] [ 6 13 21]]
3.3.4 索引,切片和迭代
- 索引: 取出数组的元素,也即是下标。
- 切片: 取list中的部分元素采用切片操作。
- 迭代: 如果给定一个list,我们可以通过for循环来遍历这个list,这种遍历我们称迭代(Iteration)。
针对一维数组:
from numpy import *a = arange(10)**2print a#索引print a[2]#切片print a[2:6]#迭代for i in a: print i**(1/2.) a[:7:3] = -10print a
运行结果如下:
[ 0 1 4 9 16 25 36 49 64 81]4[ 4 9 16 25]0.01.02.03.04.05.06.07.08.09.0[-10 1 4 -10 16 25 -10 49 64 81]
针对多维数组:
#coding=utf-8from numpy import *def f(x, y): return 2*x+yb = fromfunction(f, (5, 4))print b#第2行,第3列print b[2, 3]#每行的第2个元素print b[0:5, 1]#每列的第2和第3个元素print b[1:3,]
运行结果:
[[ 0. 1. 2. 3.] [ 2. 3. 4. 5.] [ 4. 5. 6. 7.] [ 6. 7. 8. 9.] [ 8. 9. 10. 11.]]7.0[ 1. 3. 5. 7. 9.][[ 2. 3. 4. 5.] [ 4. 5. 6. 7.]]
3.4 shape操作
数组的形状取决于其每个轴上的元素个数
#coding=utf-8from numpy import *a = array([[1, 2, 3], [2, 3, 4]])print aprint a.shape
运行结果:
[[1 2 3] [2 3 4]](2, 3)
改变数组形状
#coding=utf-8from numpy import *a = array([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]])print aprint a.shapea.ravel()a.shape=(2, 6)a.transpose()print a
运行结果:
[[1 2 3 4] [2 3 4 5] [3 4 5 6]](3, 4)[[1 2 3 4] [2 3 4 5] [3 4 5 6]][[1 2 3 4 2 3] [4 5 3 4 5 6]]
这里把3行4列的数组转换为2行6列的数组
resize函数
#coding=utf-8from numpy import *a = array([[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6]])a.resize((2, 6))print a
运行结果如上述例子一致。
4 NumPy的linalg函数
先看下跟线性代数有关的一些函数,如下表所示:
# -*- coding: utf-8 -*-import numpy as npfrom numpy.linalg import det, eig, inv, pinv, qr, svd, solvea = np.array([[1, 2], [3, 4]])b = np.array([[1, 1], [2, 2]])print aprint np.diag(a)#矩阵乘法print np.dot(a, b)#对角线元素和print np.trace(a)#行列式print det(a)#特征值和特征向量print eig(a)#逆print inv(a)print pinv(a)#QR分解c = np.array([[1, 0, 0], [1, 1, 0], [1, 1, 1], [1, 1, 1]])print cprint qr(c)#奇异值分解d = np.array([[1, -1], [-2, 2], [2, -2]])print svd(d)#线性方程求解#3*x+1*y=9#1*x+2*y=8e = np.array([[3, 1], [1, 2]])f = np.array([9, 8]) print solve(e, f)
运行结果:
[[1 2] [3 4]][[1 1] [2 2]][1 4][[ 5 5] [11 11]]5-2.0(array([-0.37228132, 5.37228132]), array([[-0.82456484, -0.41597356], [ 0.56576746, -0.90937671]]))[[-2. 1. ] [ 1.5 -0.5]][[-2. 1. ] [ 1.5 -0.5]][[1 0 0] [1 1 0] [1 1 1] [1 1 1]](array([[-0.5 , 0.8660254 , 0. ], [-0.5 , -0.28867513, 0.81649658], [-0.5 , -0.28867513, -0.40824829], [-0.5 , -0.28867513, -0.40824829]]), array([[-2. , -1.5 , -1. ], [ 0. , -0.8660254 , -0.57735027], [ 0. , 0. , -0.81649658]]))(array([[-0.33333333, 0.66666667, -0.66666667], [ 0.66666667, 0.66666667, 0.33333333], [-0.66666667, 0.33333333, 0.66666667]]), array([ 4.24264069, 0. ]), array([[-0.70710678, 0.70710678], [ 0.70710678, 0.70710678]]))[ 2. 3.]
5 NumPy的random函数
随机数生成
# -*- coding: utf-8 -*-import numpy as npnp.random.seed(10)print np.random.random()#0-10的数组print np.random.permutation(10)#打乱数组的顺序a = range(6)print anp.random.shuffle(a)print a#均匀分布样本print np.random.rand(4, 4)#10-20的随机整数print np.random.randint(10, 20) #正态分布print np.random.randn(2, 4)#二项分布n,p=10,.5print np.random.binomial(n, p, 10) #正态分布mu, sigma = 0, 0.1print np.random.normal(mu, sigma, 10) #Gamma分布的样本值shape, scale = 2., 2print np.random.gamma(shape, scale, 10) #产生在[0, 1]中均匀分布的样本值print np.random.uniform(-1, 0, 10)
运行结果:
0.771320643267[2 6 8 5 7 9 3 1 0 4][0, 1, 2, 3, 4, 5][4, 3, 5, 0, 2, 1][[ 0.26360285 0.15037787 0.68381843 0.81660184] [ 0.33607158 0.89081653 0.19812181 0.03061665] [ 0.87761494 0.72743551 0.54088093 0.13145815] [ 0.41366737 0.77872881 0.58390137 0.18263144]]19[[ 1.99652279 -1.11867153 2.18032831 0.19046688] [ 1.2616859 0.41765968 0.89395769 0.04735517]][4 3 4 3 7 2 6 5 6 4][-0.03896465 0.09372965 -0.08169742 0.10196154 -0.03414363 0.07512814 -0.03622518 -0.11291313 -0.03497105 -0.10527265][ 7.86128574 2.90912083 3.54093073 6.31143809 1.91601924 2.79290514 0.51218382 1.85868267 4.69867246 4.25069805][-0.48653337 -0.40216335 -0.73778434 -0.69912869 -0.97460022 -0.69693744 -0.75792412 -0.44242181 -0.43449298 -0.52486775]
这里应该是结合图形化的,还没学就直接打印出数据,后续结合matplotlib,可以非常直观。
- 机器学习笔记--Python之Numpy
- 机器学习-Python-Numpy
- python学习笔记之numpy入门
- python 机器学习库:Numpy
- 机器学习之Python安装和numpy配置
- 机器学习之旅-重要的Python库:Numpy
- 「机器学习」Python数据分析之Numpy进阶
- 「机器学习」Python数据分析之Numpy
- Python与机器学习之常用的Numpy操作
- Python机器学习:numpy的应用之基本操作命令
- Python机器学习:numpy之矩阵操作命令
- Python与机器学习之Numpy描述性统计
- Python与机器学习之Numpy描述性统计(二)
- 【python】numpy学习笔记
- Python Numpy学习笔记
- Python Numpy 学习笔记
- python笔记之NUMPY
- Tang机器学习课程笔记之六(Numpy库)
- Mybatis工程架构
- FastReport报表控件内置函数介绍之字符处理函数
- java类型转换
- poj 3683 Priest John's Busiest Day
- java对象序列化为xml格式
- 机器学习笔记--Python之Numpy
- 湖北汽车工业学院
- C#面向对象OOP之三
- TensorFlow学习笔记(5)——训练全连接神经网络
- 【Hibernate】一级缓存
- unity 全息和xRay shader
- 简单枚举 uva725
- 【Hibernate】组件映射与继承映射
- 个人学习第二章节:雷电游戏