numpy与list的区别 定义多维数组,取数组元素 numpy数值类型 数据类型对象 dtype('int32') e.dtype.type 所占字节数 e.dtype.itemsize 字符码 e.dtype.char 数组切片 处理数组形状 数组遍历

Python 3.6.2 (v3.6.2:5fd33b5, Jul  8 2017, 04:57:36) [MSC v.1900 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> import numpy as np
>>> a=list(range(10,15))
>>> a
[10, 11, 12, 13, 14]
>>> b=np.arange(5)
>>> b
array([0, 1, 2, 3, 4])
>>> type(a)
<class 'list'>
>>> type(b)
<class 'numpy.ndarray'>
>>> a[1]
11
>>> b[1]
1
>>> a*2
[10, 11, 12, 13, 14, 10, 11, 12, 13, 14]
>>> b*2
array([0, 2, 4, 6, 8])
>>> a**2
Traceback (most recent call last):
  File "<pyshell#11>", line 1, in <module>
    a**2
TypeError: unsupported operand type(s) for ** or pow(): 'list' and 'int'
>>> b**2 //b的二次方
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    b**2 //b的二次方
NameError: name 'b的二次方' is not defined
>>> #b的二次方
>>> b**2
array([ 0,  1,  4,  9, 16], dtype=int32)
>>> b**2
array([ 0,  1,  4,  9, 16], dtype=int32)
>>> b.dtype
dtype('int32')
>>> #b元组都是整型
>>> b.shape
(5,)
>>> #多维数组
>>> c=np.array([a,b])
>>> c
array([[10, 11, 12, 13, 14],
       [ 0,  1,  2,  3,  4]])
>>> d=[a,b]
>>> d
[[10, 11, 12, 13, 14], array([0, 1, 2, 3, 4])]
>>> c.shape
(2, 5)
>>> c.size
10
>>> e=np.array([c,c*2])
>>> e
array([[[10, 11, 12, 13, 14],
        [ 0,  1,  2,  3,  4]],

       [[20, 22, 24, 26, 28],
        [ 0,  2,  4,  6,  8]]])
>>> e.shape
(2, 2, 5)
>>> e[1]
array([[20, 22, 24, 26, 28],
       [ 0,  2,  4,  6,  8]])
>>> e[1,0]
array([20, 22, 24, 26, 28])
>>> e[1,0,3]
26
>>> #numpy数值类型
>>> type(d)
<class 'list'>
>>> type(d[1])
<class 'numpy.ndarray'>
>>> e.dtype
dtype('int32')
>>> e[1].dtype
dtype('int32')
>>> b.np.arange(5,dtype=np.int64)
Traceback (most recent call last):
  File "<pyshell#37>", line 1, in <module>
    b.np.arange(5,dtype=np.int64)
AttributeError: 'numpy.ndarray' object has no attribute 'np'
>>> b=np.arange(5,dtype=np.float16)
>>> b
array([ 0.,  1.,  2.,  3.,  4.], dtype=float16)
>>> b=np.arange(5,dtype=np.int64)
>>> b
array([0, 1, 2, 3, 4], dtype=int64)
>>> #所占字节数
>>> e.dtype.itemsize
4
>>> #字符码
>>> e.dtype.char
'l'
>>> #数组切片
>>> b[2:5]
array([2, 3, 4], dtype=int64)
>>> e[0,0,2:5]
array([12, 13, 14])
>>> e[0,0,0:5:2]
array([10, 12, 14])
>>> #数组反转
>>> e[::-1]
array([[[20, 22, 24, 26, 28],
        [ 0,  2,  4,  6,  8]],

       [[10, 11, 12, 13, 14],
        [ 0,  1,  2,  3,  4]]])
>>> e[::1]
array([[[10, 11, 12, 13, 14],
        [ 0,  1,  2,  3,  4]],

       [[20, 22, 24, 26, 28],
        [ 0,  2,  4,  6,  8]]])
>>> #处理数组形状
>>> m=np.arange(24)
>>> m
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])
>>> m.shape
(24,)
>>> #视图
>>> n=m.reshape(3,8)
>>> #一维转多维
>>> n.shape
(3, 8)
>>> n
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23]])
>>> o=m.reshape(3,2,4)
>>> o
array([[[ 0,  1,  2,  3],
        [ 4,  5,  6,  7]],

       [[ 8,  9, 10, 11],
        [12, 13, 14, 15]],

       [[16, 17, 18, 19],
        [20, 21, 22, 23]]])
>>> #直接改变形状
>>> m.shape=(2,3,1,4)
>>> m
array([[[[ 0,  1,  2,  3]],

        [[ 4,  5,  6,  7]],

        [[ 8,  9, 10, 11]]],


       [[[12, 13, 14, 15]],

        [[16, 17, 18, 19]],

        [[20, 21, 22, 23]]]])
>>> #直接改变形状
>>> m.resize(3,8)
>>> m
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23]])
>>> e.ravel()
array([10, 11, 12, 13, 14,  0,  1,  2,  3,  4, 20, 22, 24, 26, 28,  0,  2,
        4,  6,  8])
>>> n.flatten()
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])
>>> #n.flatten()为重新分配内在
>>> #e.ravel()是多维转一维
>>> #视图
>>> n.reshape()
Traceback (most recent call last):
  File "<pyshell#75>", line 1, in <module>
    n.reshape()
TypeError: reshape() takes exactly 1 argument (0 given)
>>> n.reshape(24,)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])
>>> n
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23]])
>>> n.shape
(3, 8)
>>> n.shape=(24,)
>>> n
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])
>>> n.resize(24,)
>>> n
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])
>>> n=m.reshape(3,8)
>>> n
array([[ 0,  1,  2,  3,  4,  5,  6,  7],
       [ 8,  9, 10, 11, 12, 13, 14, 15],
       [16, 17, 18, 19, 20, 21, 22, 23]])
>>> n.resize(24,)
>>> n
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 20, 21, 22, 23])
>>> 

 

>>> #数组遍历
>>> for x in np.nditer(m)
SyntaxError: invalid syntax
>>> for x in np.nditer(m):
    print(x)

    
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
>>> #F为纵向遍历
>>> for x in np.nditer(m,order="F"):
    print(x)

    
0
8
16
1
9
17
2
10
18
3
11
19
4
12
20
5
13
21
6
14
22
7
15
23
>>> for x in np.nditer(m,order="C"):
    print(x)

    
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

 

posted @ 2018-10-11 09:08  zhongwolin  阅读(724)  评论(0编辑  收藏  举报