numpy手册

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 np
arr = np.arange(10)#创建拥有10个元素的数组
larr = arr.tolist()#转换list
arr = np.zeros((1,3,3))#创建n维数组
mat = np.matrix(arr)#将数组转换为矩阵,矩阵为3*3
alist = [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*3
col1 = np.arange(2) + 1
col2 = 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)) + 3

img1[4:-4, 4:-4] = 6
img1[7:-7, 7:-7] = 9
index1 = img1 > 2
index2 = img1 < 6
compound_index = index1 & index2 #exp1
compound_index = (img1 > 3) & (img1 < 7) #与expr1含义一样
img2 = np.copy(img1[compound_index])
print img2

index3 = img1 == 9
index4 = (index1 & index2) | index3

import numpy.random as rand

a = 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)#








  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值