11-numpy笔记-莫烦基础操作1

代码

import numpy as np
 
array = np.array([[1,2,5],[3,4,6]])
print('-1-')
print('数组维度', array.ndim)
print('-2-')
print('', array.shape)
 
a = np.array([1,2,3])
print('-3-')
print(a)
 
a = np.array([1,2,3], dtype=np.int)
print('-4-')
print(a.dtype)
 
a = np.array([1,2,3], dtype=np.int64)
print('-5-')
print(a.dtype)
 
a = np.array([1,2,3], dtype=np.float32)
print('-6-')
print(a.dtype)
 
a = np.array([1,2,3], dtype=np.float64)
print('-7-')
print(a.dtype)
 
a = np.array([[1,2,3],
              [4,5,6]], dtype=np.float32)
print('-8-')
print(a)
 
# shape
a = np.zeros((3,4))
print('-9-')
print(a)
 
# shape
a = np.ones((3,4), dtype=np.int16)
print('-10-')
print(a)
 
# very close to zero
a = np.empty((3,4), dtype=np.float64)
print('-11-')
print(a)
 
# [)
a = np.arange(10, 20)
print('-12-')
print(a)
 
# [) step 2
a = np.arange(10, 20, 2)
print('-13-')
print(a)
 
a = np.arange(12).reshape((3,4))
print('-14-')
print(a)
 
# linspace,
a = np.linspace(1,10,20)
print('-15-')
print(a)
 
# linspace,
a = np.linspace(1,10,20).reshape((5,4))
print('-16-')
print(a)
 
# linspace,
a = np.arange(2, 14).reshape((3,4))
print('-17-')
print(a)
print('-18-')
print(np.argmin(a))
print('-19-')
print(np.argmax(a))
print('-20-')
print(np.mean(a))
print('-21-')
print(a.mean())
print('-22-')
print(np.average(a))
 
# 之前所有数的累加值
print('-23-')
print(np.cumsum(a))
 
# 之前所有数的累差值
print('-24-')
print(np.diff(a))
 
# 行序号,列序号
print('-25-')
print(np.nonzero(a))
# (array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))
 
print('-26-')
print(np.sort(a))
 
a = np.arange(14, 2, -1).reshape((3,4))
print('-27-')
print(a)
print('-28-')
print(np.sort(a))
 

print('-29-')
print(np.transpose(a))
print('-30-')
print(a.T)
print('-31-')
print((a.T).dot(a))
 
# 小于5等于5,大于9等于9
print('-32-')
print(np.clip(a,5,9))
 
# 列平均
print('-33-')
print(np.mean(a,axis=0))
 
# 行平均
print('-34-')
print(np.mean(a,axis=1))

  

输出

-1-
数组维度 2
-2-
 (2, 3)
-3-
[1 2 3]
-4-
int32
-5-
int64
-6-
float32
-7-
float64
-8-
[[1. 2. 3.]
 [4. 5. 6.]]
-9-
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
-10-
[[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]
-11-
[[0. 0. 0. 0.]
 [0. 0. 0. 0.]
 [0. 0. 0. 0.]]
-12-
[10 11 12 13 14 15 16 17 18 19]
-13-
[10 12 14 16 18]
-14-
[[ 0  1  2  3]
 [ 4  5  6  7]
 [ 8  9 10 11]]
-15-
[ 1.          1.47368421  1.94736842  2.42105263  2.89473684  3.36842105
  3.84210526  4.31578947  4.78947368  5.26315789  5.73684211  6.21052632
  6.68421053  7.15789474  7.63157895  8.10526316  8.57894737  9.05263158
  9.52631579 10.        ]
-16-
[[ 1.          1.47368421  1.94736842  2.42105263]
 [ 2.89473684  3.36842105  3.84210526  4.31578947]
 [ 4.78947368  5.26315789  5.73684211  6.21052632]
 [ 6.68421053  7.15789474  7.63157895  8.10526316]
 [ 8.57894737  9.05263158  9.52631579 10.        ]]
-17-
[[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
-18-
0
-19-
11
-20-
7.5
-21-
7.5
-22-
7.5
-23-
[ 2  5  9 14 20 27 35 44 54 65 77 90]
-24-
[[1 1 1]
 [1 1 1]
 [1 1 1]]
-25-
(array([0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2], dtype=int64), array([0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3], dtype=int64))
-26-
[[ 2  3  4  5]
 [ 6  7  8  9]
 [10 11 12 13]]
-27-
[[14 13 12 11]
 [10  9  8  7]
 [ 6  5  4  3]]
-28-
[[11 12 13 14]
 [ 7  8  9 10]
 [ 3  4  5  6]]
-29-
[[14 10  6]
 [13  9  5]
 [12  8  4]
 [11  7  3]]
-30-
[[14 10  6]
 [13  9  5]
 [12  8  4]
 [11  7  3]]
-31-
[[332 302 272 242]
 [302 275 248 221]
 [272 248 224 200]
 [242 221 200 179]]
-32-
[[9 9 9 9]
 [9 9 8 7]
 [6 5 5 5]]
-33-
[10.  9.  8.  7.]
-34-
[12.5  8.5  4.5]

  

 

posted @ 2018-09-06 15:37  路边的十元钱硬币  阅读(183)  评论(0编辑  收藏  举报