numpy_7

import numpy as np
#交换矩阵的其中两行
a = np.arange(25).reshape(5,5)
print (a)
a[[0,1]] = a[[1,0]]
print (a)
[[ 0  1  2  3  4]
 [ 5  6  7  8  9]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]
[[ 5  6  7  8  9]
 [ 0  1  2  3  4]
 [10 11 12 13 14]
 [15 16 17 18 19]
 [20 21 22 23 24]]
#找出数组中与给定值最接近的数
z = np.array([[0,1,2,3],[4,5,6,7]])
print(z)
a = 5.1
b=np.abs(z-a)
print(b)
print (np.argmin(b,axis=1))
print("---")
print (np.argmin(b,axis=0))
[[0 1 2 3]
 [4 5 6 7]]
[[5.1 4.1 3.1 2.1]
 [1.1 0.1 0.9 1.9]]
[3 1]
---
[1 1 1 1]
#判断二维矩阵中有没有一整列数为0?
z = np.random.randint(0,3,(2,10))
print (z)
print (z.any(axis=0))
print(z.any(axis=1))
[[0 1 2 2 0 2 2 1 1 1]
 [0 0 1 2 2 0 2 0 2 2]]
[False  True  True  True  True  True  True  True  True  True]
[ True  True]
#生成二维的高斯矩阵
help(np.random.randint)
Help on built-in function randint:

randint(...)
    randint(low, high=None, size=None)
    
    Return random integers from `low` (inclusive) to `high` (exclusive).
    
    Return random integers from the "discrete uniform" distribution in the
    "half-open" interval [`low`, `high`). If `high` is None (the default),
    then results are from [0, `low`).
    
    Parameters
    ----------
    low : int
        Lowest (signed) integer to be drawn from the distribution (unless
        ``high=None``, in which case this parameter is the *highest* such
        integer).
    high : int, optional
        If provided, one above the largest (signed) integer to be drawn
        from the distribution (see above for behavior if ``high=None``).
    size : int or tuple of ints, optional
        Output shape.  If the given shape is, e.g., ``(m, n, k)``, then
        ``m * n * k`` samples are drawn.  Default is None, in which case a
        single value is returned.
    
    Returns
    -------
    out : int or ndarray of ints
        `size`-shaped array of random integers from the appropriate
        distribution, or a single such random int if `size` not provided.
    
    See Also
    --------
    random.random_integers : similar to `randint`, only for the closed
        interval [`low`, `high`], and 1 is the lowest value if `high` is
        omitted. In particular, this other one is the one to use to generate
        uniformly distributed discrete non-integers.
    
    Examples
    --------
    >>> np.random.randint(2, size=10)
    array([1, 0, 0, 0, 1, 1, 0, 0, 1, 0])
    >>> np.random.randint(1, size=10)
    array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
    
    Generate a 2 x 4 array of ints between 0 and 4, inclusive:
    
    >>> np.random.randint(5, size=(2, 4))
    array([[4, 0, 2, 1],
           [3, 2, 2, 0]])
x,y = np.meshgrid(np.linspace(-1,1,10),np.linspace(-2,2,10))
print (x)
print(30*"@")
print (y)
print(30*"@")
D = np.sqrt(x**2+y**2)
print (D)
print(30*"@")
sigma,mu = 1,0
a = np.exp(-(D-mu)**2/(2*sigma**2))
print (a)
[[-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111
   0.33333333  0.55555556  0.77777778  1.        ]
 [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111
   0.33333333  0.55555556  0.77777778  1.        ]
 [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111
   0.33333333  0.55555556  0.77777778  1.        ]
 [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111
   0.33333333  0.55555556  0.77777778  1.        ]
 [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111
   0.33333333  0.55555556  0.77777778  1.        ]
 [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111
   0.33333333  0.55555556  0.77777778  1.        ]
 [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111
   0.33333333  0.55555556  0.77777778  1.        ]
 [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111
   0.33333333  0.55555556  0.77777778  1.        ]
 [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111
   0.33333333  0.55555556  0.77777778  1.        ]
 [-1.         -0.77777778 -0.55555556 -0.33333333 -0.11111111  0.11111111
   0.33333333  0.55555556  0.77777778  1.        ]]
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
[[-2.         -2.         -2.         -2.         -2.         -2.
  -2.         -2.         -2.         -2.        ]
 [-1.55555556 -1.55555556 -1.55555556 -1.55555556 -1.55555556 -1.55555556
  -1.55555556 -1.55555556 -1.55555556 -1.55555556]
 [-1.11111111 -1.11111111 -1.11111111 -1.11111111 -1.11111111 -1.11111111
  -1.11111111 -1.11111111 -1.11111111 -1.11111111]
 [-0.66666667 -0.66666667 -0.66666667 -0.66666667 -0.66666667 -0.66666667
  -0.66666667 -0.66666667 -0.66666667 -0.66666667]
 [-0.22222222 -0.22222222 -0.22222222 -0.22222222 -0.22222222 -0.22222222
  -0.22222222 -0.22222222 -0.22222222 -0.22222222]
 [ 0.22222222  0.22222222  0.22222222  0.22222222  0.22222222  0.22222222
   0.22222222  0.22222222  0.22222222  0.22222222]
 [ 0.66666667  0.66666667  0.66666667  0.66666667  0.66666667  0.66666667
   0.66666667  0.66666667  0.66666667  0.66666667]
 [ 1.11111111  1.11111111  1.11111111  1.11111111  1.11111111  1.11111111
   1.11111111  1.11111111  1.11111111  1.11111111]
 [ 1.55555556  1.55555556  1.55555556  1.55555556  1.55555556  1.55555556
   1.55555556  1.55555556  1.55555556  1.55555556]
 [ 2.          2.          2.          2.          2.          2.
   2.          2.          2.          2.        ]]
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
[[2.23606798 2.14591199 2.07572685 2.02758751 2.00308404 2.00308404
  2.02758751 2.07572685 2.14591199 2.23606798]
 [1.84925744 1.73916398 1.65178542 1.59086901 1.55951876 1.55951876
  1.59086901 1.65178542 1.73916398 1.84925744]
 [1.49484712 1.35628396 1.24225999 1.16003406 1.11665285 1.11665285
  1.16003406 1.24225999 1.35628396 1.49484712]
 [1.20185043 1.02439383 0.86780552 0.74535599 0.6758625  0.6758625
  0.74535599 0.86780552 1.02439383 1.20185043]
 [1.02439383 0.8089011  0.59835165 0.40061681 0.248452   0.248452
  0.40061681 0.59835165 0.8089011  1.02439383]
 [1.02439383 0.8089011  0.59835165 0.40061681 0.248452   0.248452
  0.40061681 0.59835165 0.8089011  1.02439383]
 [1.20185043 1.02439383 0.86780552 0.74535599 0.6758625  0.6758625
  0.74535599 0.86780552 1.02439383 1.20185043]
 [1.49484712 1.35628396 1.24225999 1.16003406 1.11665285 1.11665285
  1.16003406 1.24225999 1.35628396 1.49484712]
 [1.84925744 1.73916398 1.65178542 1.59086901 1.55951876 1.55951876
  1.59086901 1.65178542 1.73916398 1.84925744]
 [2.23606798 2.14591199 2.07572685 2.02758751 2.00308404 2.00308404
  2.02758751 2.07572685 2.14591199 2.23606798]]
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
[[0.082085   0.1000116  0.11598192 0.12802169 0.13450245 0.13450245
  0.12802169 0.11598192 0.1000116  0.082085  ]
 [0.18088812 0.2203924  0.25558569 0.28211737 0.29639882 0.29639882
  0.28211737 0.25558569 0.2203924  0.18088812]
 [0.32716719 0.39861745 0.46227056 0.51025764 0.53608809 0.53608809
  0.51025764 0.46227056 0.39861745 0.32716719]
 [0.48567179 0.59173797 0.68622947 0.75746513 0.7958098  0.7958098
  0.75746513 0.68622947 0.59173797 0.48567179]
 [0.59173797 0.72096802 0.83609558 0.92288844 0.96960724 0.96960724
  0.92288844 0.83609558 0.72096802 0.59173797]
 [0.59173797 0.72096802 0.83609558 0.92288844 0.96960724 0.96960724
  0.92288844 0.83609558 0.72096802 0.59173797]
 [0.48567179 0.59173797 0.68622947 0.75746513 0.7958098  0.7958098
  0.75746513 0.68622947 0.59173797 0.48567179]
 [0.32716719 0.39861745 0.46227056 0.51025764 0.53608809 0.53608809
  0.51025764 0.46227056 0.39861745 0.32716719]
 [0.18088812 0.2203924  0.25558569 0.28211737 0.29639882 0.29639882
  0.28211737 0.25558569 0.2203924  0.18088812]
 [0.082085   0.1000116  0.11598192 0.12802169 0.13450245 0.13450245
  0.12802169 0.11598192 0.1000116  0.082085  ]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值