python Numpy 学习(一)

#coding=utf-8
import numpy as np
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.shape  #(3,5)
print a.ndim  #2
print a.dtype.name  #int 64

(一)生成数组

#coding=utf-8
import numpy as np

b=np.array([(1,3,5),(2,4,6)],dtype=complex)
print b
'''
[[1.+0.j 3.+0.j 5.+0.j]
 [2.+0.j 4.+0.j 6.+0.j]]
'''
c=np.zeros((3,4))
print c
'''
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]

'''
d=np.ones((2,3,4))
print d
'''
[[[1. 1. 1. 1.]
  [1. 1. 1. 1.]
  [1. 1. 1. 1.]]

 [[1. 1. 1. 1.]
  [1. 1. 1. 1.]
  [1. 1. 1. 1.]]]

当打印数组时,numpy会以类似嵌套列表的形式显示,呈以下布局

  • 最后的轴从左到右打印

  • 次后的轴从顶向下打印

  • 剩下的轴从顶向下打印,每个切片通过一个空行与下一个隔开

 '''f=np.linspace(0,10,20)print f'''[ 0. 0.52631579 1.05263158 1.57894737 2.10526316 2.63157895 3.15789474 3.68421053 4.21052632 4.73684211 5.26315789 5.78947368 6.31578947 6.84210526 7.36842105 7.89473684 8.42105263 8.94736842 9.47368421 10. ]'''(二)数组的计算
#coding=utf-8
import numpy as np

a=np.array([1,2,3])
b=np.arange(5,8,1)
#print b  #[5,6,7]
c=a+b
print c   #[6,8,10]
print a<2  #[ True False False]
print b**2 #[25 36 49]

#NumPy中的乘法运算符*指示按元素计算,矩阵乘法可以使用dot函数或创建矩阵对象实现
A = np.array([[1, 2], [2, 3], [3, 4]])  # matrix
print A
B = np.array([[10, 20], [20, 30], [30, 40]])
print B
print A * B
'''
[[ 10  40]
 [ 40  90]
 [ 90 160]]
'''
C = np.array([[10, 20], [20, 30]])
print A.dot(C)  #矩阵乘法
'''
[[ 50  80]
 [ 80 130]
 [110 180]]
'''
#当运算的是不同类型的数组时,结果数组和更普遍和精确的已知,这种行为叫做 upcast
a = np.ones(3,dtype=np.int32)
print a
b = np.linspace(0, np.pi, 3)
print b
print b.dtype.name
c = a + b
print c
d = np.exp(c * 1j)
print d
print d.dtype.name  # # 许多非数组运算,如计算数组所有元素之和,被作为ndarray类的方法实现
#对矩阵中元素的计算
a = np.random.random((2, 3))
print a
'''
[[0.80651812 0.55211326 0.1865884 ]
 [0.33706556 0.91536976 0.5231098 ]]
'''
print a.sum() #3.3207649127632113
print a.mean()  #0.5534608187938685
print a.max() #0.9153697633997828
print a.min()  #0.18658840248723674
(三)对行列的计算
b = np.arange(12).reshape(3,4)
'''
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
'''
print b
print b.sum(axis=0) # in columns  [12 15 18 21]
print b.min(axis=1) # in rows   [0 4 8]
print b.cumsum(axis=1) # cumulative sum along each row
'''
[[ 0  1  3  6]
 [ 4  9 15 22]
 [ 8 17 27 38]]
'''
(四)函数。 numpy提供常见的数学函数如sin,cos,exp,其统称为通用函数ufunc,在numpy里这些函数作用按数组的元素运算,产生一个数组作为输出。
#coding=utf-8
import numpy as np

# ufunc
B = np.arange(3)
print B  #[0 1 2]
print np.exp(B)   #[1.         2.71828183 7.3890561 ]
print np.sqrt(B)  #[0.         1.         1.41421356]
C = np.array([2.,-1.,4.])
print np.add(B,C)  #[2. 0. 6.]

# **n : 表示n次方运算
a =np.arange(10)**3
print a   #[  0   1   8  27  64 125 216 343 512 729]

(五)切片,索引
#coding=utf-8
import numpy as np

C = np.array([2., -1., 4.])
print C.shape  #(3,)
B=np.arange(3)
print B  #[0 1 2]
print B[2]  # output the third element of B   2
print B[0:2]  #[0 1]
def f(x,y):
    return 10*x+y
b = np.fromfunction(f,(5,4),dtype=int)  #每个位置上元素的计算。例如(1,2) 1*10+2=12
print b
'''
[[ 0  1  2  3]
 [10 11 12 13]
 [20 21 22 23]
 [30 31 32 33]
 [40 41 42 43]]
'''
c = np.fromfunction(f,(3,3),dtype=float)
print c
'''
[[ 0.  1.  2.]
 [10. 11. 12.]
 [20. 21. 22.]]
'''
print b[2,3] #23
print b[1,1]  #11
print b[0:5,1] #[ 1 11 21 31 41]
print b[0:4,0] #[ 0 10 20 30]
print b[:,1]  #[ 1 11 21 31 41]
print b[:,2]  #[ 2 12 22 32 42]
print b[1:3,2]  #[12 22]
# 当少于轴数的索引被提供时,确失的索引被认为是整个切片
print b[-1]  #[40 41 42 43]

(六)迭代

#coding=utf-8
import numpy as np

def f(x,y):
    return 10*x+y
b = np.fromfunction(f,(5,4),dtype=int)  #每个位置上元素的计算。例如(1,2) 1*10+2=12
print b
'''
[[ 0  1  2  3]
 [10 11 12 13]
 [20 21 22 23]
 [30 31 32 33]
 [40 41 42 43]]
'''
#迭代多维数组是就第一个轴而言的
for row in b:
    print (row)
'''
[0 1 2 3]
[10 11 12 13]
[20 21 22 23]
[30 31 32 33]
[40 41 42 43]
'''
for column in b:
    print (column)
'''
[0 1 2 3]
[10 11 12 13]
[20 21 22 23]
[30 31 32 33]
[40 41 42 43]
'''
# 如果一个人想对每个数组中元素进行运算,我们可以使用flat属性,该属性是数组元素的一个迭代器
for element in b.flat:
    print element,
'''
0 1 2 3 10 11 12 13 20 21 22 23 30 31 32 33 40 41 42 43
'''
(七) 改变数组形状
#coding=utf-8
import numpy as np

# shape operation
a = np.floor(10*np.random.random((3,4)))
'''
[[7. 8. 8. 2.]
 [5. 7. 9. 2.]
 [7. 2. 0. 0.]]
'''
print a
print a.shape  #(3,4)
print a.ravel() # flatten the array   [7. 8. 8. 2. 5. 7. 9. 2. 7. 2. 0. 0.]

a.shape = (6,2)
print a.transpose() # 矩阵转置transpose
'''
[[9. 2. 3. 1. 4. 1.]
 [6. 7. 5. 5. 5. 5.]]
'''
a.shape = (2,6)
print a.transpose()
'''
[[9. 1.]
 [6. 5.]
 [2. 4.]
 [7. 5.]
 [3. 1.]
 [5. 5.]]
'''
# reshape函数改变参数形状并返回它,而resize函数改变数组自身
print a
a.resize((2,6))
print a
'''
[[2. 8. 8. 0. 5. 9.]
 [4. 4. 8. 6. 0. 9.]]
'''
# 如果在改变形状操作中一个维度被给做-1,其维度将自动被计算








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值