机器学习一

tile函数    

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

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

  1. >>> import numpy  
  2. >>> numpy.tile([0,0],5)#在列方向上重复[0,0]5次,默认行1次  
  3. array([0000000000])  
  4. >>> numpy.tile([0,0],(1,1))#在列方向上重复[0,0]1次,行1次  
  5. array([[00]])  
  6. >>> numpy.tile([0,0],(2,1))#在列方向上重复[0,0]1次,行2次  
  7. array([[00],  
  8.        [00]])  
  9. >>> numpy.tile([0,0],(3,1))  
  10. array([[00],  
  11.        [00],  
  12.        [00]])  
  13. >>> numpy.tile([0,0],(1,3))#在列方向上重复[0,0]3次,行1次  
  14. array([[000000]])  
  15. >>> numpy.tile([0,0],(2,3))<span style="font-family: Arial, Helvetica, sans-serif;">#在列方向上重复[0,0]3次,行2次</span>  
  16. array([[000000],  
  17.        [000000]])  

zeros函数

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

[python]  view plain copy print ?
  1. >>> np.zeros((3,4,5))#  
  2. array([[[ 0.,  0.,  0.,  0.,  0.],  
  3.         [ 0.,  0.,  0.,  0.,  0.],  
  4.         [ 0.,  0.,  0.,  0.,  0.],  
  5.         [ 0.,  0.,  0.,  0.,  0.]],  
  6.   
  7.        [[ 0.,  0.,  0.,  0.,  0.],  
  8.         [ 0.,  0.,  0.,  0.,  0.],  
  9.         [ 0.,  0.,  0.,  0.,  0.],  
  10.         [ 0.,  0.,  0.,  0.,  0.]],  
  11.   
  12.        [[ 0.,  0.,  0.,  0.,  0.],  
  13.         [ 0.,  0.,  0.,  0.,  0.],  
  14.         [ 0.,  0.,  0.,  0.,  0.],  
  15.         [ 0.,  0.,  0.,  0.,  0.]]])  


多维数组与矩阵的转换

[python]  view plain copy print ?
  1. >>> arr = np.zeros((1,4,5))#记得维度必须匹配(3,3,3)转换失败  
  2. >>> mat = np.matrix(arr)  

数组创建和数据类型

[python]  view plain copy print ?
  1. #!/usr/bin/env python  
  2. #-*-encoding:utf-8-*-  
  3. import numpy as np  
  4. arr = np.arange(10)#创建拥有10个元素的数组  
  5. larr = arr.tolist()#转换list  
  6. arr = np.zeros((1,3,3))#创建n维数组  
  7. mat = np.matrix(arr)#将数组转换为矩阵,矩阵为3*3  
  8. alist = [123]  
  9. np.array(alist)#使用List创建数组  
  10. #创建  
  11. arr = np.arange(100)  
  12. arr = np.arange(10,100)#创建包含10~99数组  
  13. arr = np.linspace(12100)#创建包含100个取值范围在1~2之间的数组  
  14. arr = np.logspace(01100, base=10)#返回包含100个取值范围在10+[0~1]之间的数组  
  15. cube = np.zeros((5,5,5)).astype(int) + 1 #使用astype设置数据类型  
  16. cube = np.ones((555)).astype(np.float32)#创建3维数组,元素为1  
  17. #通过指定数据类型创建n维数组数组  
  18. arr = np.zeros(2, dtype=int)  
  19. arr = np.zeros(2, dtype=np.float32)  
  20. arr1d = np.arange(1000)#一维数组  
  21. arr3d = arr1d.reshape((10,10,10))#转换为3维数组  
  22. arr3d = np.reshape(arr1d, (101010))  
  23.   
  24. arr4d = np.zeros((10101010))  
  25. arr1d = arr4d.ravel()#将4维数组转换为1维数组  
  26.   
  27. recarr = np.zeros((2,), dtype=('i4,f4,a10'))#指定n维数组中每列的数据类型,2*3  
  28. col1 = np.arange(2) + 1  
  29. col2 = np.arange(2, dtype=np.float32)  
  30. col3 = ['Hello''World']  
  31. recarr[:]=zip(col1,col2,col3)#按列方式组装  
  32. #为每列命名  
  33. recarr.dtype.names = ('Integers' , 'Floats''Strings')<span style="font-size:14px;">  
  34. </span>  

索引和切片

[python]  view plain copy print ?
  1. arr[0,1]#访问单个元?  
  2. arr[:,1]#访问第2列    
  3. arr[1,:]#访问第2行    


[python]  view plain copy print ?
  1. arr = np.arange(7)  
  2. index = np.where(arr > 2)#查找>2的索引  
  3. new_arr = np.delete(arr, index)#删除index对应的元素  

[python]  view plain copy print ?
  1. index = arr > 2 #返回哪些元素>2,[TRUE,FALSE,...]  

布尔表达式和数组

[python]  view plain copy print ?
  1. img1 = np.zeros((2020)) + 3  
  2.   
  3. img1[4:-44:-4] = 6  
  4. img1[7:-77:-7] = 9  
  5. index1 = img1 > 2  
  6. index2 = img1 < 6  
  7. compound_index = index1 & index2 #exp1  
  8. compound_index = (img1 > 3) & (img1 < 7#与expr1含义一样  
  9. img2 = np.copy(img1[compound_index])  
  10. print img2  
  11.   
  12. index3 = img1 == 9  
  13. index4 = (index1 & index2) | index3  

[python]  view plain copy print ?
  1. import numpy.random as rand  
  2.   
  3. a = rand.random(100)#生成一个容纳100个随机数的数组  
  4. print a  

序列化和反序列化

[python]  view plain copy print ?
  1. #预定义数据栏位名称和类型  
  2. table = np.loadtxt('example.txt',dtype='names': ('ID''Result''Type'),\  
  3.     'formats': ('S4''f4''i2'))  
  4. np.savetxt('somenewfile.txt')#序列化  
  5. #二进制文件加载,保存  
  6. data = np.empty((10001000))  
  7. np.save('test.npy', data)  
  8. np.savez('test.npz', data)#采用压缩  
  9. newdata = np.load('test.npy')     

Max和Min函数

[python]  view plain copy print ?
  1. mat.max(0)#n维数组axis=0维度的最小值,最大值  
  2. mat.min(0)#  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值