栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

numpy基本操作例子,cmd操作,有需要看正确的示例

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

numpy基本操作例子,cmd操作,有需要看正确的示例

Microsoft Windows [版本 10.0.19043.1645]
(c) Microsoft Corporation。保留所有权利。

C:Usersymd>python
Python 3.8.5 (default, Sep  3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32

Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated.  Libraries may fail to load.  To activate this environment
please see https://conda.io/activation

Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import numpy as  np
>>>
>>> x = np.array([[1,2,3],[4,5,6]])
>>> x
array([[1, 2, 3],
       [4, 5, 6]])
>>> x.size
6
>>> x.shape
(2, 3)
>>> x.ndim
2
>>> x = np.array([[1,2,3],[4,5,6]],dtype = np.float32)
>>> x
array([[1., 2., 3.],
       [4., 5., 6.]], dtype=float32)
>>>
>>>
>>>
>>> y = np.zeros((2,3))
>>> y
array([[0., 0., 0.],
       [0., 0., 0.]])
>>> y = np.empty((2,3))
>>> y
array([[0., 0., 0.],
       [0., 0., 0.]])
>>> y
array([[0., 0., 0.],
       [0., 0., 0.]])
>>> y = np.empty((2,3))
>>> y = np.empty((2,3))
>>> y
array([[1.33733641e+160, 6.19319847e-071, 1.73158239e-056],
       [3.50772676e+174, 2.93494585e+222, 7.92680158e-061]])
>>>
>>>
>>>
>>> a = np.arange(12)
>>> a
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11])
>>> a.reshape(2,3)
Traceback (most recent call last):
  File "", line 1, in 
ValueError: cannot reshape array of size 12 into shape (2,3)
>>> a.reshape(2,-1)
array([[ 0,  1,  2,  3,  4,  5],
       [ 6,  7,  8,  9, 10, 11]])
>>>
>>>
>>>
>>>
>>>
>>> b = np.linspace(1,10,20)
>>> b
array([ 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.        ])
>>>
>>>
>>>
>>>
>>>
>>> c = np.array([1,2,3,6])
>>>
>>>
>>> d = np.arange(1,5)
>>>
>>>
>>>
>>> c+d
array([ 2,  4,  6, 10])
>>>
>>>
>>>
>>>
>>>
>>> np.sin(c)
array([ 0.84147098,  0.90929743,  0.14112001, -0.2794155 ])
>>>
>>>
>>>
>>>
>>>
>>> c
array([1, 2, 3, 6])
>>> c.reshape(2,2)
array([[1, 2],
       [3, 6]])
>>> d.reshape((2,2))
array([[1, 2],
       [3, 4]])
>>> c*d
array([ 1,  4,  9, 24])
>>> c= c.reshape(2,2)
>>> d = d.reshape((2,2))
>>> c*d
array([[ 1,  4],
       [ 9, 24]])
>>>
>>>
>>>
>>> np.dot(c*d)
Traceback (most recent call last):
  File "", line 1, in 
  File "<__array_function__ internals>", line 4, in dot
TypeError: dot() missing 1 required positional argument: 'b'
>>> np.dot(c,d)
array([[ 7, 10],
       [21, 30]])
>>>
>>>
>>>
>>> c.dot(d)
array([[ 7, 10],
       [21, 30]])
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> f = np.random.random((2,3))
>>> F
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'F' is not defined
>>> f
array([[0.53193023, 0.04059496, 0.92837355],
       [0.94249984, 0.44669889, 0.47781203]])
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> np.max(f)
0.9424998373816006
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> np.sum(f)
3.36790949454025
>>> np.sum(f,axis = 0)
array([1.47443006, 0.48729385, 1.40618558])
>>> np.sum(f,axis = 1)
array([1.50089873, 1.86701076])
>>>
>>>
>>>
>>>
>>>
>>>
>>> f
array([[0.53193023, 0.04059496, 0.92837355],
       [0.94249984, 0.44669889, 0.47781203]])
>>>
>>>
>>>
>>>
>>>
>>> np.argmax(f)
3
>>> np.argmin(f)
1
>>> np.mean(f)
0.5613182490900417
>>> np.average(f)
0.5613182490900417
>>>
>>>
>>>
>>> np.median(f)
0.5048711292210916
>>> np.cumsum(f)
array([0.53193023, 0.57252518, 1.50089873, 2.44339857, 2.89009746,
       3.36790949])
>>> np.diff(f)
array([[-0.49133527,  0.88777859],
       [-0.49580095,  0.03111314]])
>>> np.nonzero(f)
(array([0, 0, 0, 1, 1, 1], dtype=int64), array([0, 1, 2, 0, 1, 2], dtype=int64))
>>> f
array([[0.53193023, 0.04059496, 0.92837355],
       [0.94249984, 0.44669889, 0.47781203]])
>>>
>>>
>>>
>>>
>>> g = np.array([[1,0,1],[0,1,0]])
>>> g
array([[1, 0, 1],
       [0, 1, 0]])
>>>
>>>
>>>
>>> np.nonzero(g)
(array([0, 0, 1], dtype=int64), array([0, 2, 1], dtype=int64))
>>> f
array([[0.53193023, 0.04059496, 0.92837355],
       [0.94249984, 0.44669889, 0.47781203]])
>>>
>>>
>>>
>>>
>>> np.sort(f)
array([[0.04059496, 0.53193023, 0.92837355],
       [0.44669889, 0.47781203, 0.94249984]])
>>>
>>>
>>>
>>>
>>>
>>>
>>> f.T
array([[0.53193023, 0.94249984],
       [0.04059496, 0.44669889],
       [0.92837355, 0.47781203]])
>>>
>>>
>>>
>>> np.transpose(f)
array([[0.53193023, 0.94249984],
       [0.04059496, 0.44669889],
       [0.92837355, 0.47781203]])
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> f
array([[0.53193023, 0.04059496, 0.92837355],
       [0.94249984, 0.44669889, 0.47781203]])
>>>
>>>
>>>
>>>
>>>
>>>
>>> np.clip(f , 0.5,0.9)
array([[0.53193023, 0.5       , 0.9       ],
       [0.9       , 0.5       , 0.5       ]])
>>> f
array([[0.53193023, 0.04059496, 0.92837355],
       [0.94249984, 0.44669889, 0.47781203]])
>>>
>>>
>>>
>>>
>>> np.mean(f , axis = 0)
array([0.73721503, 0.24364692, 0.70309279])
>>> 
...
>>>
>>>
>>>
>>>
>>> np.range(14)
Traceback (most recent call last):
  File "", line 1, in 
  File "E:pythonpycharmanacodacodalibsite-packagesnumpy__init__.py", line 313, in __getattr__
    raise AttributeError("module {!r} has no attribute "
AttributeError: module 'numpy' has no attribute 'range'
>>>
>>>
>>>
>>>
>>> h  = np.arange(14)
>>> h
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13])
>>> h = h.reshape(2,7)
>>> h
array([[ 0,  1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12, 13]])
>>>
>>>
>>>
>>>
>>> h
array([[ 0,  1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12, 13]])
>>>
>>>
>>>
>>>
>>>
>>> h[0]
array([0, 1, 2, 3, 4, 5, 6])
>>> h[0][1]
1
>>>
>>>
>>>
>>>
>>> h[0,1]
1
>>> h[0,:]
array([0, 1, 2, 3, 4, 5, 6])
>>> h[0,2:]
array([2, 3, 4, 5, 6])
>>>
>>>
>>>
>>>
>>>
>>> for  i in h:
...     print(i)
...
[0 1 2 3 4 5 6]
[ 7  8  9 10 11 12 13]
>>>
>>>
>>> for  i in h:
...  print(i)
...
[0 1 2 3 4 5 6]
[ 7  8  9 10 11 12 13]
>>>
>>>
>>>
>>>
>>>
>>>
>>> for  j in h.T:
...  print(j)
...
[0 7]
[1 8]
[2 9]
[ 3 10]
[ 4 11]
[ 5 12]
[ 6 13]
>>>
>>>
>>>
>>>
>>>
>>> h.flat

>>> h.flatten()
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13])
>>> for i in h.flat:
...  print(i)
...
0
1
2
3
4
5
6
7
8
9
10
11
12
13
>>> h
array([[ 0,  1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12, 13]])
>>>
>>>
>>>
>>>
>>> h = h.flatten()
>>> h
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13])
>>>
>>>
>>>
>>>
>>>
>>>
>>> i = np.arange(3,14)
>>> i
array([ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13])
>>> i = np.arange(3,17)
>>> i
array([ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])
>>>
>>>
>>>
>>> i.size
14
>>>
>>>
>>> h.size
14
>>>
>>>
>>>
>>>
>>> np.vstack((h,i))
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13],
       [ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16]])
>>> print(np.vstack((h,i)).ndim)
2
>>> print(np.hstack((h,i))
...
...
...
...
...
...
...
... h.size
  File "", line 9
    h.size
    ^
SyntaxError: invalid syntax
>>> print(np.hstack((h,i))
...
...
...
...
...
...
...
... g
  File "", line 9
    g
    ^
SyntaxError: invalid syntax
>>>
>>> np.hstack((h,i))
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13,  3,  4,  5,
        6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])
>>>
>>>
>>> h,j
(array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13]), array([ 6, 13]))
>>> h,i
(array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13]), array([ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16]))
>>>
>>>
>>>
>>>
>>>
>>> h = h.reshape((2,7))
>>> i = i.reshape((2,7))
>>>
>>>
>>> h,i
(array([[ 0,  1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12, 13]]), array([[ 3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16]]))
>>>
>>>
>>>
>>>
>>> np.hstack((h,i))
array([[ 0,  1,  2,  3,  4,  5,  6,  3,  4,  5,  6,  7,  8,  9],
       [ 7,  8,  9, 10, 11, 12, 13, 10, 11, 12, 13, 14, 15, 16]])
>>> np.vstack((h,i))
array([[ 0,  1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12, 13],
       [ 3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16]])
>>>
>>>
>>>
>>>
>>>
>>> h
array([[ 0,  1,  2,  3,  4,  5,  6],
       [ 7,  8,  9, 10, 11, 12, 13]])
>>>
>>>
>>>
>>> h = h.flatten()
>>>
>>>
>>>
>>> h
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13])
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> h[newaxis, :]
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'newaxis' is not defined
>>> h[np.newaxis, :]
array([[ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13]])
>>> h:[,np.newaxis]
  File "", line 1
    h:[,np.newaxis]
       ^
SyntaxError: invalid syntax
>>> h[:,np.newaxis]
array([[ 0],
       [ 1],
       [ 2],
       [ 3],
       [ 4],
       [ 5],
       [ 6],
       [ 7],
       [ 8],
       [ 9],
       [10],
       [11],
       [12],
       [13]])
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> h,i
(array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13]), array([[ 3,  4,  5,  6,  7,  8,  9],
       [10, 11, 12, 13, 14, 15, 16]]))
>>>
>>>
>>>
>>>
>>>
>>> np.concatenate((h,i))
Traceback (most recent call last):
  File "", line 1, in 
  File "<__array_function__ internals>", line 5, in concatenate
ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)
>>> i.flatten()
array([ 3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])
>>> i= i.flatten()
>>>
>>>
>>>
>>> np.concatenate((h,i))
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13,  3,  4,  5,
        6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])
>>> np.concatenate((h,i,i))
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13,  3,  4,  5,
        6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,  3,  4,  5,  6,  7,  8,
        9, 10, 11, 12, 13, 14, 15, 16])
>>> np.concatenate((h,i),axis = 0)
array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13,  3,  4,  5,
        6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16])
>>> np.concatenate((h,i),axis = 1)
Traceback (most recent call last):
  File "", line 1, in 
  File "<__array_function__ internals>", line 5, in concatenate
numpy.AxisError: axis 1 is out of bounds for array of dimension 1
>>> h = h.reshape((2,7))
>>>
>>>
>>>
>>>
>>> i = i .reshape((2,7))
>>>
>>>
>>>
>>>
>>> np.concatenate((h,i),axis = 1)
array([[ 0,  1,  2,  3,  4,  5,  6,  3,  4,  5,  6,  7,  8,  9],
       [ 7,  8,  9, 10, 11, 12, 13, 10, 11, 12, 13, 14, 15, 16]])
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> j = np.arange(12).reshape(3,4)
>>>
>>>
>>>
>>> j
array([[ 0,  1,  2,  3],
       [ 4,  5,  6,  7],
       [ 8,  9, 10, 11]])
>>>
>>>
>>>
>>>
>>>
>>> np.split(j,3,axis = 1)
Traceback (most recent call last):
  File "", line 1, in 
  File "<__array_function__ internals>", line 5, in split
  File "E:pythonpycharmanacodacodalibsite-packagesnumpylibshape_base.py", line 872, in split
    raise ValueError(
ValueError: array split does not result in an equal division
>>> np.split(j,2,axis = 1)
[array([[0, 1],
       [4, 5],
       [8, 9]]), array([[ 2,  3],
       [ 6,  7],
       [10, 11]])]
>>> np.split(j,2,axis = 0)
Traceback (most recent call last):
  File "", line 1, in 
  File "<__array_function__ internals>", line 5, in split
  File "E:pythonpycharmanacodacodalibsite-packagesnumpylibshape_base.py", line 872, in split
    raise ValueError(
ValueError: array split does not result in an equal division
>>> np.split(j,3,axis = 0)
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]
>>>
>>>
>>>
>>>
>>>
>>> np.array_split(j,3,axis = 0)
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]
>>> np.array_split(j,2,axis = 0)
[array([[0, 1, 2, 3],
       [4, 5, 6, 7]]), array([[ 8,  9, 10, 11]])]
>>>
>>>
>>>
>>> np.vsplit(j,2,axis = 0)
Traceback (most recent call last):
  File "", line 1, in 
  File "<__array_function__ internals>", line 4, in vsplit
TypeError: _hvdsplit_dispatcher() got an unexpected keyword argument 'axis'
>>> np.vsplit(j,2,axis = 1)
Traceback (most recent call last):
  File "", line 1, in 
  File "<__array_function__ internals>", line 4, in vsplit
TypeError: _hvdsplit_dispatcher() got an unexpected keyword argument 'axis'
>>> np.vsplit(jaxis = 1)
Traceback (most recent call last):
  File "", line 1, in 
  File "<__array_function__ internals>", line 4, in vsplit
TypeError: _hvdsplit_dispatcher() got an unexpected keyword argument 'jaxis'
>>> np.vsplit(j,axis = 1)
Traceback (most recent call last):
  File "", line 1, in 
  File "<__array_function__ internals>", line 4, in vsplit
TypeError: _hvdsplit_dispatcher() got an unexpected keyword argument 'axis'
>>> np.vsplit(j)
Traceback (most recent call last):
  File "", line 1, in 
  File "<__array_function__ internals>", line 4, in vsplit
TypeError: _hvdsplit_dispatcher() missing 1 required positional argument: 'indices_or_sections'
>>>
>>>
>>>
>>>
>>>
>>>
>>> k = np.arange(6)
>>> k
array([0, 1, 2, 3, 4, 5])
>>>
>>>
>>>
>>>
>>> l = k
>>> l
array([0, 1, 2, 3, 4, 5])
>>>
>>>
>>>
>>> k[1] = 333
>>> k
array([  0, 333,   2,   3,   4,   5])
>>> l
array([  0, 333,   2,   3,   4,   5])
>>> l = k.copy()
>>> l
array([  0, 333,   2,   3,   4,   5])
>>> k[3]= 3333333
>>> k
array([      0,     333,       2, 3333333,       4,       5])
>>>
>>>
>>>
>>>
>>> l
array([  0, 333,   2,   3,   4,   5])
>>>
>>>
>>>
>>> np.vsplit(l3,)
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'l3' is not defined
>>> np.vsplit(l,3)
Traceback (most recent call last):
  File "", line 1, in 
  File "<__array_function__ internals>", line 5, in vsplit
  File "E:pythonpycharmanacodacodalibsite-packagesnumpylibshape_base.py", line 990, in vsplit
    raise ValueError('vsplit only works on arrays of 2 or more dimensions')
ValueError: vsplit only works on arrays of 2 or more dimensions
>>> l = l[newaxis,:]
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'newaxis' is not defined
>>> l = l[mp.newaxis,:]
Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'mp' is not defined
>>> l = l[np.newaxis,:]
>>> l
array([[  0, 333,   2,   3,   4,   5]])
>>> np.vsplit(l,3)
Traceback (most recent call last):
  File "", line 1, in 
  File "<__array_function__ internals>", line 5, in vsplit
  File "E:pythonpycharmanacodacodalibsite-packagesnumpylibshape_base.py", line 991, in vsplit
    return split(ary, indices_or_sections, 0)
  File "<__array_function__ internals>", line 5, in split
  File "E:pythonpycharmanacodacodalibsite-packagesnumpylibshape_base.py", line 872, in split
    raise ValueError(
ValueError: array split does not result in an equal division
>>> np.vsplit(l,2)
Traceback (most recent call last):
  File "", line 1, in 
  File "<__array_function__ internals>", line 5, in vsplit
  File "E:pythonpycharmanacodacodalibsite-packagesnumpylibshape_base.py", line 991, in vsplit
    return split(ary, indices_or_sections, 0)
  File "<__array_function__ internals>", line 5, in split
  File "E:pythonpycharmanacodacodalibsite-packagesnumpylibshape_base.py", line 872, in split
    raise ValueError(
ValueError: array split does not result in an equal division
>>>
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/857687.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号