In [6]:
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]]
In [7]:
#找出数组中与给定值最接近的数
z = np.array([[0,1,2,3],[4,5,6,7]])
a = 5.1
print np.abs(z-a).argmin()
5
In [9]:
#判断二维矩阵中有没有一整列数为0?
z = np.random.randint(0,3,(2,10))
print z
print z.any(axis=0)
[[1 1 2 0 0 1 1 0 2 2]
[0 0 2 1 0 2 1 0 1 0]]
[ True True True True False True True False True True]
In [15]:
#生成二维的高斯矩阵
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]])
In [14]:
x,y = np.meshgrid(np.linspace(-1,1,10),np.linspace(-1,1,10))
print x
print y
D = np.sqrt(x**2+y**2)
print D
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. ]]
[[-1. -1. -1. -1. -1. -1. -1.
-1. -1. -1. ]
[-0.77777778 -0.77777778 -0.77777778 -0.77777778 -0.77777778 -0.77777778
-0.77777778 -0.77777778 -0.77777778 -0.77777778]
[-0.55555556 -0.55555556 -0.55555556 -0.55555556 -0.55555556 -0.55555556
-0.55555556 -0.55555556 -0.55555556 -0.55555556]
[-0.33333333 -0.33333333 -0.33333333 -0.33333333 -0.33333333 -0.33333333
-0.33333333 -0.33333333 -0.33333333 -0.33333333]
[-0.11111111 -0.11111111 -0.11111111 -0.11111111 -0.11111111 -0.11111111
-0.11111111 -0.11111111 -0.11111111 -0.11111111]
[ 0.11111111 0.11111111 0.11111111 0.11111111 0.11111111 0.11111111
0.11111111 0.11111111 0.11111111 0.11111111]
[ 0.33333333 0.33333333 0.33333333 0.33333333 0.33333333 0.33333333
0.33333333 0.33333333 0.33333333 0.33333333]
[ 0.55555556 0.55555556 0.55555556 0.55555556 0.55555556 0.55555556
0.55555556 0.55555556 0.55555556 0.55555556]
[ 0.77777778 0.77777778 0.77777778 0.77777778 0.77777778 0.77777778
0.77777778 0.77777778 0.77777778 0.77777778]
[ 1. 1. 1. 1. 1. 1. 1.
1. 1. 1. ]]
[[ 1.41421356 1.26686158 1.1439589 1.05409255 1.0061539 1.0061539
1.05409255 1.1439589 1.26686158 1.41421356]
[ 1.26686158 1.09994388 0.95581392 0.84619701 0.7856742 0.7856742
0.84619701 0.95581392 1.09994388 1.26686158]
[ 1.1439589 0.95581392 0.7856742 0.64788354 0.56655772 0.56655772
0.64788354 0.7856742 0.95581392 1.1439589 ]
[ 1.05409255 0.84619701 0.64788354 0.47140452 0.35136418 0.35136418
0.47140452 0.64788354 0.84619701 1.05409255]
[ 1.0061539 0.7856742 0.56655772 0.35136418 0.15713484 0.15713484
0.35136418 0.56655772 0.7856742 1.0061539 ]
[ 1.0061539 0.7856742 0.56655772 0.35136418 0.15713484 0.15713484
0.35136418 0.56655772 0.7856742 1.0061539 ]
[ 1.05409255 0.84619701 0.64788354 0.47140452 0.35136418 0.35136418
0.47140452 0.64788354 0.84619701 1.05409255]
[ 1.1439589 0.95581392 0.7856742 0.64788354 0.56655772 0.56655772
0.64788354 0.7856742 0.95581392 1.1439589 ]
[ 1.26686158 1.09994388 0.95581392 0.84619701 0.7856742 0.7856742
0.84619701 0.95581392 1.09994388 1.26686158]
[ 1.41421356 1.26686158 1.1439589 1.05409255 1.0061539 1.0061539
1.05409255 1.1439589 1.26686158 1.41421356]]
[[ 0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818
0.57375342 0.51979489 0.44822088 0.36787944]
[ 0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.73444367
0.69905581 0.63331324 0.54610814 0.44822088]
[ 0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.85172308
0.81068432 0.73444367 0.63331324 0.51979489]
[ 0.57375342 0.69905581 0.81068432 0.89483932 0.9401382 0.9401382
0.89483932 0.81068432 0.69905581 0.57375342]
[ 0.60279818 0.73444367 0.85172308 0.9401382 0.98773022 0.98773022
0.9401382 0.85172308 0.73444367 0.60279818]
[ 0.60279818 0.73444367 0.85172308 0.9401382 0.98773022 0.98773022
0.9401382 0.85172308 0.73444367 0.60279818]
[ 0.57375342 0.69905581 0.81068432 0.89483932 0.9401382 0.9401382
0.89483932 0.81068432 0.69905581 0.57375342]
[ 0.51979489 0.63331324 0.73444367 0.81068432 0.85172308 0.85172308
0.81068432 0.73444367 0.63331324 0.51979489]
[ 0.44822088 0.54610814 0.63331324 0.69905581 0.73444367 0.73444367
0.69905581 0.63331324 0.54610814 0.44822088]
[ 0.36787944 0.44822088 0.51979489 0.57375342 0.60279818 0.60279818
0.57375342 0.51979489 0.44822088 0.36787944]]
In [ ]:
Content source: JarkJiao/Python_ML
Similar notebooks: