CSDN机器学习笔记五 Numpy简单操作

来源:互联网 发布:武汉大学网络教学平台 编辑:程序博客网 时间:2024/06/05 23:58

一、Numpy库简介

机器学习算法涉及很多线性代数知识,因此经常使用NumPy函数库。NumPy函数库是Python开发环境的一个独立模块,而且大多数Python发行版没有默认安装NumPy函数库,需要单独进行安装。
Anaconda集成了NumPy,可以更方便的使用该库。
使用示例:
如:

import numpyvector=numpy.array([5,10,15,20])matrix=numpy.array([[5,10,15],[20,25,30],[35,40,45]])print (vector)print (matrix)
vector=numpy.array([1,2,3,4])print (vector.shape) #查看,常在debug使用
import numpynumbers=numpy.array([1,2,3,4])  #这里元素必须是相同类型print (numbers)numbers.dtype  #输出数据类型

这里写图片描述

二、 一些数组运算

内积

np.dot(a,b)np.inner(a,b)np.outer(a,b)

求和

np.sum()

平均值

np.mean()

方差,标准差

np.std()

三、Numpy的一些操作

Numpy是Python的科学计算的库,提供了矩阵运算的功能,其一般与Scipy、matplotlib一起使用。

1.导入numpy

import numpy as npprint (np.version.version)

2.NumPy矩阵与数组的区别

NumPy函数库中的matrix与MATLAB中matricses等价。矩阵和数组显然是两种不同的数据类型。调用mat()函数可以将数组转化为矩阵,例如:

import numpy as nprandMat = np.mat(np.random.rand(4,4))print (randMat)

输出结果:
[[ 0.68961916 0.17159703 0.59995799 0.99626607]
[ 0.92436968 0.97710376 0.03818586 0.30673391]
[ 0.3851621 0.58558701 0.94255313 0.31884537]
[ 0.83106025 0.61143961 0.82635502 0.95168527]]

3.多维数组 numpy.ndarray

以list或tuple变量为参数产生一维数组

print (np.array([1,2,3,4]))print (np.array((1.2,2,3,4)))print (type(np.array((1.2,2,3,4))))

这里写图片描述

生成数组的时候,可以指定数据类型。

print (np.array((1.2,2,3,4),dtype=np.int32))[1 2 3 4]

使用numpy.arange方法

print (np.arange(15))print (type(np.arange(15)))<class 'numpy.ndarray'>

挨个元素判断值

import numpyvector=numpy.array([5,10,15,20])vector == 10

返回结果:
array([False, True, False, False], dtype=bool)

matrix=numpy.array([ [5,10,15], [20,25,30], [35,40,45]])matrix==25

返回结果:
array([[False, False, False],
[False, True, False],
[False, False, False]], dtype=bool)

布尔值判断

vector=numpy.array([5,10,15,20])equal_to_len = (vector==10)print (equal_to_len)print (vector[equal_to_len])

输出结果:
[False True False False]
[10]

类型转换

vector = numpy.array(["1","2","3"])print (vector.dtype)print (vector)vector = vector.astype(float)print (vector.dtype)print (vector)

输出结果:

matrix=numpy.array([[5,10,15],[20,25,30],[35,40,45]])matrix.sum(axis=1)

输出:
array([ 30, 75, 120])
按指定维度1相加,会按行加。

matrix.sum(axis=0)
array([60, 75, 90])
按指定维度0相加,会按列加。

求逆操作

import numpy as nprandMat = np.mat(np.random.rand(4,4))print (randMat.I)

输出:
[[ 1.01943747 -0.25509941 -1.51081141 1.64622233]
[ 0.87747412 -0.56051253 1.17143183 -1.09210262]
[-2.21496654 0.77250249 1.49641539 -0.12661331]
[ 1.05598907 1.25225748 -2.77331073 1.30848761]]

常用函数

import numpy as npprint (np.arange(15))a=np.arange(15).reshape(3,5) #把原来的元素转为矩阵,3行5列print (a)

输出:
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]]

ndarray的属性

  • a.shape 返回维度
import numpy as npa=np.arange(15).reshape(3,5) #把原来的元素转为矩阵,3行5列print (a.shape)

输出:
(3, 5)

  • 维度
a.ndim

输出:
2

  • a.dtype.name
    输出:
    是int32 等

  • a.size
    一共多少个元素

  • np.zeros((3,4)) 初始化全零矩阵,维度是2

import numpy as npnp.zeros((3,4))

输出:
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
初始化,比如对权重进行初始化。

  • 初始化1
np.ones((2,3,4),dtype=np.int32) 初始化1
  • numpy.eye构造特定的矩阵

  • 等差数列

np.arange(10,30,5)array([10, 15, 20, 25])
  • 随机值
np.random.random((2,3))
  • linspace
from numpy import pinp.linspace(0,2*pi,100)   #从0到2pi当中找100个值,a=np.array([20,30,40,50])b=np.arange(4)c=a-bprint (a)print (b)print (c)

输出:
[20 30 40 50]
[0 1 2 3]
[20 29 38 47]

c = c-1

[19 28 37 46]

print (b**2)

输出:
[0 1 4 9]

print (a<5)

输出:
[False False False False]

使用for操作元素

import numpy as npfor x in np.linspace(1,3,3):    print (x)

输出:
1.0
2.0
3.0

矩阵算术

A = np.array([[1,1],[0,1]])B = np.array([[2,0],[3,4]])print (A)print (B)

输出:
[[1 1]
[0 1]]
[[2 0]
[3 4]]

点乘 、 矩阵相乘

print (A*B)  #对应位置相乘

输出结果:
[[2 0]
[0 4]]

print (A.dot(B))  #正常的矩阵相乘,或print (np.dot(A,B))

输出结果:
[[5 4]
[3 4]]

import numpy as npB=np.arange(3)print (B)print (np.exp(B))print (np.sqrt(B))

输出:
[0 1 2]
[ 1. 2.71828183 7.3890561 ]
[ 0. 1. 1.41421356]

a = np.floor(10*np.random.random((3,4)))print (a)

输出示例:
[[ 1. 7. 9. 0.]
[ 8. 7. 8. 2.]
[ 7. 8. 5. 0.]]

把矩阵拉为向量

print (a.ravel())

改维度

a.shape=(6,2)

转置

a.T 

拼接

import numpy as npa = np.floor(10*np.random.random((2,2)))b = np.floor(10*np.random.random((2,2)))print (a)print (np.vstack((a,b)))

[[ 5. 9.]
[ 1. 6.]
[ 0. 9.]
[ 1. 2.]]
横着拼

print (np.hstack((a,b)))

切分

a = np.floor(10*np.random.random((2,12)))print (np.hsplit(a,3)) #平均切分 print (np.hsplit(a,(3,4)))

输出:
[array([[ 4., 1., 4., 5.],
[ 9., 4., 9., 9.]]), array([[ 3., 5., 5., 7.],
[ 9., 7., 6., 3.]]), array([[ 2., 1., 7., 5.],
[ 5., 6., 8., 9.]])]
[array([[ 4., 1., 4.],
[ 9., 4., 9.]]), array([[ 5.],
[ 9.]]), array([[ 3., 5., 5., 7., 2., 1., 7., 5.],
[ 9., 7., 6., 3., 5., 6., 8., 9.]])]

vsplit竖着切

引用

a = np.arange(12)b = a  #a和b是同一个对象。b.shape = 3,4print (a.shape)print (id(a))print (id(b))

输出:
(3, 4)
2134912537744
2134912537744

复制(赋值操作)

c=a.view()c.shape=2,6print (a.shape)c[0,4] = 1234print (a)print (id(a))print (id(c))

(3, 4)
[[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
2134912537744
2134912459520

纯粹复制,深拷贝

d = a.copy()d is ad[0,0]=9999print (d)print (a)

输出:
[[9999 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]

排序

import numpy as npdata = np.sin(np.arange(20)).reshape(5,4)print (data)ind = data.argmax(axis=0)print (ind)data_max = data[ind]print (data_max)

输出:
[[ 0. 0.84147098 0.90929743 0.14112001]
[-0.7568025 -0.95892427 -0.2794155 0.6569866 ]
[ 0.98935825 0.41211849 -0.54402111 -0.99999021]
[-0.53657292 0.42016704 0.99060736 0.65028784]
[-0.28790332 -0.96139749 -0.75098725 0.14987721]]
[2 0 3 1]
[[ 0.98935825 0.41211849 -0.54402111 -0.99999021]
[ 0. 0.84147098 0.90929743 0.14112001]
[-0.53657292 0.42016704 0.99060736 0.65028784]
[-0.7568025 -0.95892427 -0.2794155 0.6569866 ]]
扩充矩阵

import numpy as npa=np.arange(0,40,10)print (a)np.tile(a,(4,3)) #把a 扩到4行三列,按倍数扩充print (b)

输出:
[ 0 10 20 30]
[[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]

对numpy排序

import numpy as npa = np.arange(0,40,10)a.sort()print (a)

输出:
[ 0 10 20 30]

argsort

import numpy as npx = np.array([3, 1, 2])np.argsort(x)

输出:
array([1, 2, 0], dtype=int64)

原创粉丝点击