numpy手册

来源:互联网 发布:mysql 日期最近一周 编辑:程序博客网 时间:2024/05/16 12:26

tile函数    

    在看机器学习实战这本书时,遇到numpy.tile(A,B)函数,愣是没看懂怎么回事,装了numpy模块后,实验了几把,原来是这样子:

重复A,B次,这里的B可以时int类型也可以是远组类型。

>>> import numpy>>> numpy.tile([0,0],5)#在列方向上重复[0,0]5次,默认行1次array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])>>> numpy.tile([0,0],(1,1))#在列方向上重复[0,0]1次,行1次array([[0, 0]])>>> numpy.tile([0,0],(2,1))#在列方向上重复[0,0]1次,行2次array([[0, 0],       [0, 0]])>>> numpy.tile([0,0],(3,1))array([[0, 0],       [0, 0],       [0, 0]])>>> numpy.tile([0,0],(1,3))#在列方向上重复[0,0]3次,行1次array([[0, 0, 0, 0, 0, 0]])>>> numpy.tile([0,0],(2,3))#在列方向上重复[0,0]3次,行2次array([[0, 0, 0, 0, 0, 0],       [0, 0, 0, 0, 0, 0]])

zeros函数

zeros:创建3维数组,数值为0

>>> np.zeros((3,4,5))#array([[[ 0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.]],       [[ 0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.]],       [[ 0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.],        [ 0.,  0.,  0.,  0.,  0.]]])

多维数组与矩阵的转换

>>> arr = np.zeros((1,4,5))#记得维度必须匹配(3,3,3)转换失败>>> mat = np.matrix(arr)

数组创建和数据类型

#!/usr/bin/env python#-*-encoding:utf-8-*-import numpy as nparr = np.arange(10)#创建拥有10个元素的数组larr = arr.tolist()#转换listarr = np.zeros((1,3,3))#创建n维数组mat = np.matrix(arr)#将数组转换为矩阵,矩阵为3*3alist = [1, 2, 3]np.array(alist)#使用List创建数组#创建arr = np.arange(100)arr = np.arange(10,100)#创建包含10~99数组arr = np.linspace(1, 2, 100)#创建包含100个取值范围在1~2之间的数组arr = np.logspace(0, 1, 100, base=10)#返回包含100个取值范围在10+[0~1]之间的数组cube = np.zeros((5,5,5)).astype(int) + 1 #使用astype设置数据类型cube = np.ones((5, 5, 5)).astype(np.float32)#创建3维数组,元素为1#通过指定数据类型创建n维数组数组arr = np.zeros(2, dtype=int)arr = np.zeros(2, dtype=np.float32)arr1d = np.arange(1000)#一维数组arr3d = arr1d.reshape((10,10,10))#转换为3维数组arr3d = np.reshape(arr1d, (10, 10, 10))arr4d = np.zeros((10, 10, 10, 10))arr1d = arr4d.ravel()#将4维数组转换为1维数组recarr = np.zeros((2,), dtype=('i4,f4,a10'))#指定n维数组中每列的数据类型,2*3col1 = np.arange(2) + 1col2 = np.arange(2, dtype=np.float32)col3 = ['Hello', 'World']recarr[:]=zip(col1,col2,col3)#按列方式组装#为每列命名recarr.dtype.names = ('Integers' , 'Floats', 'Strings')

索引和切片

arr[0,1]#访问单个元?arr[:,1]#访问第2列  arr[1,:]#访问第2行


arr = np.arange(7)index = np.where(arr > 2)#查找>2的索引new_arr = np.delete(arr, index)#删除index对应的元素

index = arr > 2 #返回哪些元素>2,[TRUE,FALSE,...]

布尔表达式和数组

img1 = np.zeros((20, 20)) + 3img1[4:-4, 4:-4] = 6img1[7:-7, 7:-7] = 9index1 = img1 > 2index2 = img1 < 6compound_index = index1 & index2 #exp1compound_index = (img1 > 3) & (img1 < 7) #与expr1含义一样img2 = np.copy(img1[compound_index])print img2index3 = img1 == 9index4 = (index1 & index2) | index3

import numpy.random as randa = rand.random(100)#生成一个容纳100个随机数的数组print a

序列化和反序列化

#预定义数据栏位名称和类型table = np.loadtxt('example.txt',dtype='names': ('ID', 'Result', 'Type'),\'formats': ('S4', 'f4', 'i2'))np.savetxt('somenewfile.txt')#序列化#二进制文件加载,保存data = np.empty((1000, 1000))np.save('test.npy', data)np.savez('test.npz', data)#采用压缩newdata = np.load('test.npy')

Max和Min函数

mat.max(0)#n维数组axis=0维度的最小值,最大值mat.min(0)#