我是靠谱客的博主 忧郁云朵,这篇文章主要介绍Numpy Learning一些numpy学习要注意的地方,现在分享给大家,希望可以做个参考。

一些numpy学习要注意的地方

  • resize和reshape的不同
复制代码
1
2
3
4
5
6
7
8
import numpy as np a = np.array([[ 2., 8., 0., 6.], [ 4., 5., 1., 1.], [ 8., 9., 3., 6.]]) a.reshape(1,1) # 输出失败,行列和原来不相等 a.reshape(1, -1) # 自动计算行列 a.resize((1,1)) # 没有强制要求,无所谓,数列变成(1,1) print(a)
  • 显示所有被省略的数据
复制代码
1
2
3
4
5
6
7
8
9
10
import numpy as np print(np.arange(10000)) # 这里讲所有的数据都显示了出来 # 但是注意,使用了‘np.nan’作为参数如果不指定import的numpy为np的话,结果如下 np.set_printoptions(threshold=np.nan) # 不指定module from numpy import * print(arange(10000)) set_printoptions(threshold=nan)
  • 横向一条一条堆叠
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from numpy import newaxis import numpy as np a = np.floor(10*np.random.random((2,2))) b = np.floor(10*np.random.random((2,2))) print(a ,'nn', b, 'nn') print (np.column_stack((a,b))) #[[ 0. 8.] #[ 2. 4.]] #[[ 8. 1.] #[ 7. 5.]] #[[ 0. 8. 8. 1.] # [ 2. 4. 7. 5.]] # 将原来的数组进行更高维度的装换,之后拼接 c = np.column_stack((a[:,newaxis],b[:,newaxis])) print (c) c.shape # (2, 2, 2) #[[ 3. 1.] # [ 8. 5.]] # [[ 6. 3.] # [ 0. 7.]] #[[[ 3. 1.] # [ 6. 3.]] # [[ 8. 5.] # [ 0. 7.]]] # For arrays of with more than two dimensions, hstack stacks along their second axes, vstack stacks along their first axes, and concatenate allows for an optional arguments giving the number of the axis along which the concatenation should happen.
  • 数组拆分
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
from numpy import newaxis import numpy as np a = np.floor(10*np.random.random((2,12))) print(a) a.shape print(100* '*') # 原始的数组竖直切断,分成原始数组的3份的数组 c = np.hsplit(a,3) # Split a into 3 print(c) print(len(c)) print(c[0].shape) print(100* '*') new = np.floor(10*np.random.random((2,12))) print(new) print(100* '*') d = np.hsplit(new,(3,4)) # Split a after the third and the fourth column print(d) print(len(d)) i = 0 while i < len(d): print(d[i].shape) i = i + 1 print(100* '*')
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
[[ 9. 7. 3. 5. 6. 8. 5. 0. 3. 8. 4. 2.] [ 6. 9. 4. 5. 8. 0. 9. 7. 7. 5. 4. 9.]] **************************************************************************************************** [array([[ 9., 7., 3., 5.], [ 6., 9., 4., 5.]]), array([[ 6., 8., 5., 0.], [ 8., 0., 9., 7.]]), array([[ 3., 8., 4., 2.], [ 7., 5., 4., 9.]])] 3 # 分成组数 (2, 4) # 每个拆分的size **************************************************************************************************** [[ 9. 6. 2. 7. 3. 5. 3. 0. 1. 4. 3. 5.] [ 2. 9. 1. 7. 9. 0. 8. 7. 4. 5. 1. 8.]] **************************************************************************************************** [array([[ 9., 6., 2.], [ 2., 9., 1.]]), array([[ 7.], [ 7.]]), array([[ 3., 5., 3., 0., 1., 4., 3., 5.], [ 9., 0., 8., 7., 4., 5., 1., 8.]])] 3 # 分成的组数 (2, 3) # 每个拆分的size (2, 1) (2, 8) ***************************************************************************************************
  • 创建一个array的view,这个根据数据类型不同可能有不同拷贝,如果需要一个完全新的不受影响的数据,就使用copy()
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from numpy import newaxis import numpy as np a = np.arange(12).reshape(2,6) c = a.view() print(a, type(a)) print(c is a) print(c.base is a) # c is a view of the data owned by a print(c.flags.owndata) c.resize(3, 4) # a's shape doesn't change print(a.shape) c[0,2] = 1234 # a's data changes print(a) print(100 * '-') print(c)
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 原始的a生成的数组,(2, 6) [[ 0 1 2 3 4 5] [ 6 7 8 9 10 11]] <class 'numpy.ndarray'> # c is a False # c.base is a False False # a原始数组的大小 (2, 6) # a [[ 0 1 1234 3 4 5] [ 6 7 8 9 10 11]] ---------------------------------------------------------------------------------------------------- # c, 改变了c的大小 [[ 0 1 1234 3] [ 4 5 6 7] [ 8 9 10 11]]
  • 索引
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
from numpy import newaxis import numpy as np a = np.arange(12)**2 # the first 12 square numbers i = np.array( [ 1,1,3,8,5 ] ) # an array of indices print(a) # [ 0 1 4 9 16 25 36 49 64 81 100 121] j = np.array( [ [ 3, 4], [ 9, 7 ] ] ) # a bidimensional array of indices # 这里支持交叉组合索引 print(a[j]) # [[ 9 16] # [81 49]]

最后

以上就是忧郁云朵最近收集整理的关于Numpy Learning一些numpy学习要注意的地方的全部内容,更多相关Numpy内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(63)

评论列表共有 0 条评论

立即
投稿
返回
顶部