python Numpy学习(三)

#coding=utf-8

import numpy as np

txt=np.genfromtxt('/home/troy/Desktop/1.txt',delimiter=',',dtype=str)
print txt          #['return output' 'tgnb' 'hmmj' 'hmdm']
#print help(np.genfromtxt)

a=np.arange(15).reshape(3,5)
print a
'''
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]]
'''
print a.ravel()   #[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14]
b=a.copy()
print b
print id(a)   #140096989609696
print id(b)   #140096979950128

print a*b   #元素乘法
b=b.T   #矩阵转置
print a.dot(b)  #矩阵乘法

c=10*np.random.random((3,5))
print c
'''
[[2.12545878e+00 4.53883119e+00 5.13060049e-04 4.17777432e+00
  6.05965286e+00]
 [7.36592247e+00 8.17097100e+00 8.43972115e-01 3.07412568e+00
  7.82258202e+00]
 [5.11421913e+00 9.08226511e+00 9.69798382e+00 3.11886342e+00
  3.19986281e+00]]
'''
print np.floor(c)  #向下取整
'''
[[2. 4. 0. 4. 6.]
 [7. 8. 0. 3. 7.]
 [5. 9. 9. 3. 3.]]
'''

#拼接
e=np.ones((3,4),dtype=np.int32)
f=e+1
print f
'''
[[2 2 2 2]
 [2 2 2 2]
 [2 2 2 2]]

'''
h=np.vstack((e,f))  #垂直摞在一起
print h
'''
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]
 [2 2 2 2]
 [2 2 2 2]
 [2 2 2 2]]
'''
w=np.hstack((e,f))  #水平摞在一起
print w
'''
[[1 1 1 1 2 2 2 2]
 [1 1 1 1 2 2 2 2]
 [1 1 1 1 2 2 2 2]]
'''
#分割
k=np.vsplit(h,3)  #即分割线是水平的
print k
'''
[array([[1, 1, 1, 1],
       [1, 1, 1, 1]], dtype=int32), array([[1, 1, 1, 1],
       [2, 2, 2, 2]], dtype=int32), array([[2, 2, 2, 2],
       [2, 2, 2, 2]], dtype=int32)]

'''
m=np.hsplit(w,(3,4))  #即分割线是垂直的
print m
'''
[array([[1, 1, 1],
       [1, 1, 1],
       [1, 1, 1]], dtype=int32), array([[1],
       [1],
       [1]], dtype=int32), array([[2, 2, 2, 2],
       [2, 2, 2, 2],
       [2, 2, 2, 2]], dtype=int32)]

'''
a=np.arange(10,30,2)
print a.size
b=a.reshape(2,5)
print b
'''
[[10 12 14 16 18]
 [20 22 24 26 28]]
'''
r= np.tile(b,(2,3))  #即行数变为原来的2倍,列数也变为原来的3倍
print r
'''
[[10 12 14 16 18 10 12 14 16 18 10 12 14 16 18]
 [20 22 24 26 28 20 22 24 26 28 20 22 24 26 28]
 [10 12 14 16 18 10 12 14 16 18 10 12 14 16 18]
 [20 22 24 26 28 20 22 24 26 28 20 22 24 26 28]]

'''
print np.sort(r,axis= 1)#按行方向
'''
[[10 10 10 12 12 12 14 14 14 16 16 16 18 18 18]
 [20 20 20 22 22 22 24 24 24 26 26 26 28 28 28]
 [10 10 10 12 12 12 14 14 14 16 16 16 18 18 18]
 [20 20 20 22 22 22 24 24 24 26 26 26 28 28 28]]
'''

print np.sort(r,axis=0)  #按列方向
'''
[[10 12 14 16 18 10 12 14 16 18 10 12 14 16 18]
 [10 12 14 16 18 10 12 14 16 18 10 12 14 16 18]
 [20 22 24 26 28 20 22 24 26 28 20 22 24 26 28]
 [20 22 24 26 28 20 22 24 26 28 20 22 24 26 28]]

'''

print r.argmax(axis=1)   #[4 4 4 4]
print r.argmax(axis=0)   #[1 1 1 1 1 1 1 1 1 1 1 1 1 1 1]


你可能感兴趣的:(python学习)