Numpy 的一些属性
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Date : 2017-10-31 17:20:59
# @Author : leboryi
# @What : test for python.numpy; Numpy 的一些属性
import numpy as np
array = np.array([[1,2,3],
[4,5,6]])
print(array)
print('number of dim:',array.ndim)
print('shape:',array.shape)
print('size:',array.size)
Numpy 创建 array
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2017-10-31 17:39:12
# @Author : leboryi
# @What : test for python.numpy; Numpy 创建 array
import numpy as np
def prt(x,y):
print('%s:' % x)
print(y)
print('######################')
a = np.array([1,23,4],dtype=np.float)
#dtype = np.int,np.float,np.int32,np.float32
prt('a.dtype:',a.dtype)
a2 = np.array([[1,2,3],
[1,2,3]])
prt('a2',a2)
a3 = np.zeros((3,4))
prt('a3',a3)
a4 = np.ones((3,4),dtype=np.int16)
prt('a4',a4)
a5 = np.empty((3,4))
prt('a5',a5)
a6 = np.arange(12).reshape((3,4))
prt('a6',a6)
a7 = np.linspace(1,10,6).reshape((2,3))
prt('a7',a7)
Numpy 的基础运算1
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Date : 2017-10-31 21:19:01
# @Author : leboryi
# @What : Numpy 的基础运算1
import numpy as np
# a = np.array([10,20,30,40])
# b = np.arange(4)
# print(a,b)
# c = a - b
# d = a + b
# e = b**2
# f = 10*np.sin(a)
# print(c,d,e)
# print(f)
# a = np.array([[1,1],
# [0,1]])
# b = np.arange(4).reshape((2,2))
# c = a*b
# c_dot = np.dot(a,b)
# c_dot_2 = a.dot(b)
# #矩阵运算
# print(c)
# print(c_dot)
# print(c_dot_2)
a = np.random.random((2,4))
print(a)
print(np.sum(a,axis=1))
print(np.min(a,axis=0))
print(np.max(a,axis=1))
#axis=0,行;axis=1,列