交换矩阵的其中两行
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]])
a = 5.1
print np.abs(z-a).argmin()
输出 : 5
判断二维矩阵中有没有一整列数为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]
生成二维的高斯矩阵
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(-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]]
1:8*8棋盘矩阵,其中1、3、5、7行&&0、2、4、6列的元素置为1 1 ,3,5,7列&&0,2,4,6行也是1
import numpy as np
z = np.zeros((8,8),dtype=int)
z[1::2,::2] = 1
z[::2,1::2] = 1
print z
输出 :
[[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]
[0 1 0 1 0 1 0 1]
[1 0 1 0 1 0 1 0]]
2:min()、max()函数
z = np.random.random((10,10))
zmin,zmax = z.min(),z.max()
归一化,将矩阵规格化到0~1,即最小的变成0,最大的变成1,最小与最大之间的等比缩放
z = 10*np.random.random((5,5))
print z
zmin,zmax = z.min(),z.max()
z = (z-zmin)/(zmax-zmin)
print z
输出 :
[[ 5.64509768 0.68146094 8.23694681 0.07100285 7.37379186]
[ 0.0752762 9.7764589 7.80421002 6.11552908 2.6865688 ]
[ 9.8869914 1.44285425 0.89132992 2.59310328 0.56864919]
[ 6.55186281 8.26647432 6.15930931 4.22051594 7.03349304]
[ 4.74498093 0.58582793 3.09918065 2.54389279 7.28846041]]
[[ 5.67858734e-01 6.21901795e-02 8.31902351e-01 0.00000000e+00
7.43968778e-01]
[ 4.35345679e-04 9.88739545e-01 7.87817461e-01 6.15783749e-01
2.66459759e-01]
[ 1.00000000e+00 1.39756825e-01 8.35704993e-02 2.56937996e-01
5.06975256e-02]
[ 6.60235078e-01 8.34910455e-01 6.20243842e-01 4.22730025e-01
7.09300970e-01]
[ 4.76159692e-01 5.24476039e-02 3.08494430e-01 2.51924697e-01
7.35275670e-01]]
矩阵相加
z = np.zeros((5,5))
z += np.arange(5)
print np.arange(5)
print z
输出 :
[0 1 2 3 4]
[[ 0. 1. 2. 3. 4.]
[ 0. 1. 2. 3. 4.]
[ 0. 1. 2. 3. 4.]
[ 0. 1. 2. 3. 4.]
[ 0. 1. 2. 3. 4.]]
生成0~10之间均匀分布的11个数,包括0和10
z = np.linspace(0,10,11,endpoint=True,retstep=True)
print z
输出 :
(array([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.]), 1.0)
help(np.linspace)
输出 :
Help on function linspace in module numpy.core.function_base:
linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
Return evenly spaced numbers over a specified interval.
Returns `num` evenly spaced samples, calculated over the
interval [`start`, `stop`].
The endpoint of the interval can optionally be excluded.
Parameters
----------
start : scalar
The starting value of the sequence.
stop : scalar
The end value of the sequence, unless `endpoint` is set to False.
In that case, the sequence consists of all but the last of ``num + 1``
evenly spaced samples, so that `stop` is excluded. Note that the step
size changes when `endpoint` is False.
num : int, optional
Number of samples to generate. Default is 50. Must be non-negative.
endpoint : bool, optional
If True, `stop` is the last sample. Otherwise, it is not included.
Default is True.
retstep : bool, optional
If True, return (`samples`, `step`), where `step` is the spacing
between samples.
dtype : dtype, optional
The type of the output array. If `dtype` is not given, infer the data
type from the other input arguments.
.. versionadded:: 1.9.0
Returns
-------
samples : ndarray
There are `num` equally spaced samples in the closed interval
``[start, stop]`` or the half-open interval ``[start, stop)``
(depending on whether `endpoint` is True or False).
step : float
Only returned if `retstep` is True
Size of spacing between samples.
See Also
--------
arange : Similar to `linspace`, but uses a step size (instead of the
number of samples).
logspace : Samples uniformly distributed in log space.
Examples
--------
>>> np.linspace(2.0, 3.0, num=5)
array([ 2. , 2.25, 2.5 , 2.75, 3. ])
>>> np.linspace(2.0, 3.0, num=5, endpoint=False)
array([ 2. , 2.2, 2.4, 2.6, 2.8])
>>> np.linspace(2.0, 3.0, num=5, retstep=True)
(array([ 2. , 2.25, 2.5 , 2.75, 3. ]), 0.25)
Graphical illustration:
>>> import matplotlib.pyplot as plt
>>> N = 8
>>> y = np.zeros(N)
>>> x1 = np.linspace(0, 10, N, endpoint=True)
>>> x2 = np.linspace(0, 10, N, endpoint=False)
>>> plt.plot(x1, y, 'o')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.plot(x2, y + 0.5, 'o')
[<matplotlib.lines.Line2D object at 0x...>]
>>> plt.ylim([-0.5, 1])
(-0.5, 1)
>>> plt.show()
import numpy
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",")
print(type(world_alcohol))
输出 : <class 'numpy.ndarray'>
#The numpy.array() function can take a list or list of lists as input. When we input a list, we get a one-dimensional array as a result:
vector = numpy.array([5, 10, 15, 20])
#When we input a list of lists, we get a matrix as a result:
matrix = numpy.array([[5, 10, 15], [20, 25, 30], [35, 40, 45]])
print vector
print matrix
输出 :
[ 5 10 15 20]
[[ 5 10 15]
[20 25 30]
[35 40 45]]
#We can use the ndarray.shape property to figure out how many elements are in the array
vector = numpy.array([1, 2, 3, 4])
print(vector.shape)
#For matrices, the shape property contains a tuple with 2 elements.
matrix = numpy.array([[5, 10, 15], [20, 25, 30]])
print(matrix.shape)
输出 :
(4,)
(2, 3)
#Each value in a NumPy array has to have the same data type
#NumPy will automatically figure out an appropriate data type when reading in data or converting lists to arrays.
#You can check the data type of a NumPy array using the dtype property.
numbers = numpy.array([1, 2, 3, 4])
numbers.dtype
输出 : dtype('int32')
#When NumPy can't convert a value to a numeric data type like float or integer, it uses a special nan value that stands for Not a Number
#nan is the missing data
#1.98600000e+03 is actually 1.986 * 10 ^ 3
world_alcohol
输出 :
array([[ nan, nan, nan,
nan, nan],
[ 1.98600000e+03, nan, nan,
nan, 0.00000000e+00],
[ 1.98600000e+03, nan, nan,
nan, 5.00000000e-01],
...,
[ 1.98700000e+03, nan, nan,
nan, 7.50000000e-01],
[ 1.98900000e+03, nan, nan,
nan, 1.50000000e+00],
[ 1.98500000e+03, nan, nan,
nan, 3.10000000e-01]])
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",", dtype="U75", skip_header=1)
print(world_alcohol)
输出 :
[[u'1986' u'Western Pacific' u'Viet Nam' u'Wine' u'0']
[u'1986' u'Americas' u'Uruguay' u'Other' u'0.5']
[u'1985' u'Africa' u"Cte d'Ivoire" u'Wine' u'1.62']
...,
[u'1987' u'Africa' u'Malawi' u'Other' u'0.75']
[u'1989' u'Americas' u'Bahamas' u'Wine' u'1.5']
[u'1985' u'Africa' u'Malawi' u'Spirits' u'0.31']]
uruguay_other_1986 = world_alcohol[1,4]
third_country = world_alcohol[2,2]
print uruguay_other_1986
print third_country
输出 :
0.5
Cte d'Ivoire
vector = numpy.array([5, 10, 15, 20])
print(vector[0:3])
输出 : [ 5 10 15]
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix[:,1])
输出 : [10 25 40]
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix[:,0:2])
输出 :
[[ 5 10]
[20 25]
[35 40]]
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
print(matrix[1:3,0:2])
输出 :
[[20 25]
[35 40]]
import numpy
#it will compare the second value to each element in the vector
# If the values are equal, the Python interpreter returns True; otherwise, it returns False
vector = numpy.array([5, 10, 15, 20])
vector == 10
输出 : array([False, True, False, False], dtype=bool)
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
matrix == 25
输出 :
array([[False, False, False],
[False, True, False],
[False, False, False]], dtype=bool)
#Compares vector to the value 10, which generates a new Boolean vector [False, True, False, False]. It assigns this result to equal_to_ten
vector = numpy.array([5, 10, 15, 20])
equal_to_ten = (vector == 10)
print equal_to_ten
print(vector[equal_to_ten])
输出 :
[False True False False]
[10]
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
second_column_25 = (matrix[:,1] == 25)
print second_column_25
print(matrix[second_column_25, :])
输出 :
[False True False]
[[20 25 30]]
#We can also perform comparisons with multiple conditions
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_and_five = (vector == 10) & (vector == 5)
print equal_to_ten_and_five
输出 : [False False False False]
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5)
print equal_to_ten_or_five
输出 : [ True True False False]
vector = numpy.array([5, 10, 15, 20])
equal_to_ten_or_five = (vector == 10) | (vector == 5)
vector[equal_to_ten_or_five] = 50
print(vector)
输出 : [50 50 15 20]
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
second_column_25 = matrix[:,1] == 25
print second_column_25
matrix[second_column_25, 1] = 10
print matrix
输出 :
[False True False]
[[ 5 10 15]
[20 10 30]
[35 40 45]]
#We can convert the data type of an array with the ndarray.astype() method.
vector = numpy.array(["1", "2", "3"])
print vector.dtype
print vector
vector = vector.astype(float)
print vector.dtype
print vector
输出 :
|S1
['1' '2' '3']
float64
[ 1. 2. 3.]
vector = numpy.array([5, 10, 15, 20])
vector.sum()
输出 : 50
# The axis dictates which dimension we perform the operation on
#1 means that we want to perform the operation on each row, and 0 means on each column
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
matrix.sum(axis=1)
输出 : array([ 30, 75, 120])
matrix = numpy.array([
[5, 10, 15],
[20, 25, 30],
[35, 40, 45]
])
matrix.sum(axis=0)
输出 : array([60, 75, 90])
#replace nan value with 0
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",")
#print world_alcohol
is_value_empty = numpy.isnan(world_alcohol[:,4])
#print is_value_empty
world_alcohol[is_value_empty, 4] = '0'
alcohol_consumption = world_alcohol[:,4]
alcohol_consumption = alcohol_consumption.astype(float)
total_alcohol = alcohol_consumption.sum()
average_alcohol = alcohol_consumption.mean()
print total_alcohol
print average_alcohol
输出 :
1137.78
1.14006012024
import numpy as np
a = np.arange(15).reshape(3, 5)
a
输出 :
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
a.shape
输出 : (3, 5)
#the number of axes (dimensions) of the array
a.ndim
输出 : 2
a.dtype.name
输出 : 'int32'
#the total number of elements of the array
a.size
输出 : 15
np.zeros ((3,4))
输出 :
array([[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]])
np.ones( (2,3,4), dtype=np.int32 )
输出 :
array([[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]],
[[1, 1, 1, 1],
[1, 1, 1, 1],
[1, 1, 1, 1]]])
#To create sequences of numbers
np.arange( 10, 30, 5 )
输出 : array([10, 15, 20, 25])
np.arange( 0, 2, 0.3 )
输出 : array([ 0. , 0.3, 0.6, 0.9, 1.2, 1.5, 1.8])
np.arange(12).reshape(4,3)
输出 :
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
np.random.random((2,3))
输出 :
array([[ 0.40130659, 0.45452825, 0.79776512],
[ 0.63220592, 0.74591134, 0.64130737]])
from numpy import pi
np.linspace( 0, 2*pi, 100 )
输出 :
array([ 0. , 0.06346652, 0.12693304, 0.19039955, 0.25386607,
0.31733259, 0.38079911, 0.44426563, 0.50773215, 0.57119866,
0.63466518, 0.6981317 , 0.76159822, 0.82506474, 0.88853126,
0.95199777, 1.01546429, 1.07893081, 1.14239733, 1.20586385,
1.26933037, 1.33279688, 1.3962634 , 1.45972992, 1.52319644,
1.58666296, 1.65012947, 1.71359599, 1.77706251, 1.84052903,
1.90399555, 1.96746207, 2.03092858, 2.0943951 , 2.15786162,
2.22132814, 2.28479466, 2.34826118, 2.41172769, 2.47519421,
2.53866073, 2.60212725, 2.66559377, 2.72906028, 2.7925268 ,
2.85599332, 2.91945984, 2.98292636, 3.04639288, 3.10985939,
3.17332591, 3.23679243, 3.30025895, 3.36372547, 3.42719199,
3.4906585 , 3.55412502, 3.61759154, 3.68105806, 3.74452458,
3.8079911 , 3.87145761, 3.93492413, 3.99839065, 4.06185717,
4.12532369, 4.1887902 , 4.25225672, 4.31572324, 4.37918976,
4.44265628, 4.5061228 , 4.56958931, 4.63305583, 4.69652235,
4.75998887, 4.82345539, 4.88692191, 4.95038842, 5.01385494,
5.07732146, 5.14078798, 5.2042545 , 5.26772102, 5.33118753,
5.39465405, 5.45812057, 5.52158709, 5.58505361, 5.64852012,
5.71198664, 5.77545316, 5.83891968, 5.9023862 , 5.96585272,
6.02931923, 6.09278575, 6.15625227, 6.21971879, 6.28318531])
np.sin(np.linspace( 0, 2*pi, 100 ))
输出 :
array([ 0.00000000e+00, 6.34239197e-02, 1.26592454e-01,
1.89251244e-01, 2.51147987e-01, 3.12033446e-01,
3.71662456e-01, 4.29794912e-01, 4.86196736e-01,
5.40640817e-01, 5.92907929e-01, 6.42787610e-01,
6.90079011e-01, 7.34591709e-01, 7.76146464e-01,
8.14575952e-01, 8.49725430e-01, 8.81453363e-01,
9.09631995e-01, 9.34147860e-01, 9.54902241e-01,
9.71811568e-01, 9.84807753e-01, 9.93838464e-01,
9.98867339e-01, 9.99874128e-01, 9.96854776e-01,
9.89821442e-01, 9.78802446e-01, 9.63842159e-01,
9.45000819e-01, 9.22354294e-01, 8.95993774e-01,
8.66025404e-01, 8.32569855e-01, 7.95761841e-01,
7.55749574e-01, 7.12694171e-01, 6.66769001e-01,
6.18158986e-01, 5.67059864e-01, 5.13677392e-01,
4.58226522e-01, 4.00930535e-01, 3.42020143e-01,
2.81732557e-01, 2.20310533e-01, 1.58001396e-01,
9.50560433e-02, 3.17279335e-02, -3.17279335e-02,
-9.50560433e-02, -1.58001396e-01, -2.20310533e-01,
-2.81732557e-01, -3.42020143e-01, -4.00930535e-01,
-4.58226522e-01, -5.13677392e-01, -5.67059864e-01,
-6.18158986e-01, -6.66769001e-01, -7.12694171e-01,
-7.55749574e-01, -7.95761841e-01, -8.32569855e-01,
-8.66025404e-01, -8.95993774e-01, -9.22354294e-01,
-9.45000819e-01, -9.63842159e-01, -9.78802446e-01,
-9.89821442e-01, -9.96854776e-01, -9.99874128e-01,
-9.98867339e-01, -9.93838464e-01, -9.84807753e-01,
-9.71811568e-01, -9.54902241e-01, -9.34147860e-01,
-9.09631995e-01, -8.81453363e-01, -8.49725430e-01,
-8.14575952e-01, -7.76146464e-01, -7.34591709e-01,
-6.90079011e-01, -6.42787610e-01, -5.92907929e-01,
-5.40640817e-01, -4.86196736e-01, -4.29794912e-01,
-3.71662456e-01, -3.12033446e-01, -2.51147987e-01,
-1.89251244e-01, -1.26592454e-01, -6.34239197e-02,
-2.44929360e-16])
#the product operator * operates elementwise in NumPy arrays
a = np.array( [20,30,40,50] )
b = np.arange( 4 )
#print a
#print b
#b
c = a-b
#print c
b**2
#print b**2
print a<35
输出 : [ True True False False]
#The matrix product can be performed using the dot function or method
A = np.array( [[1,1],
[0,1]] )
B = np.array( [[2,0],
[3,4]] )
print A
print B
#print A*B
print A.dot(B)
print np.dot(A, B)
输出 :
[[1 1]
[0 1]]
[[2 0]
[3 4]]
[[5 4]
[3 4]]
[[5 4]
[3 4]]
import numpy as np
B = np.arange(3)
print B
#print np.exp(B)
print np.sqrt(B)
输出 :
[0 1 2]
[ 0. 1. 1.41421356]
#Return the floor of the input
a = np.floor(10*np.random.random((3,4)))
#print a
#a.shape
## flatten the array
#print a.ravel()
#a.shape = (6, 2)
#print a
#print a.T
print a.resize((2,6))
print a
#If a dimension is given as -1 in a reshaping operation, the other dimensions are automatically calculated:
#a.reshape(3,-1)
输出 :
None
[[ 9. 7. 6. 4. 9. 0.]
[ 2. 9. 1. 3. 4. 0.]]
a = np.floor(10*np.random.random((2,2)))
b = np.floor(10*np.random.random((2,2)))
print a
print '---'
print b
print '---'
print np.hstack((a,b))
#np.hstack((a,b))
输出 :
[[ 5. 6.]
[ 1. 5.]]
---
[[ 8. 6.]
[ 9. 0.]]
---
[[ 5. 6. 8. 6.]
[ 1. 5. 9. 0.]]
a = np.floor(10*np.random.random((2,12)))
#print a
#print np.hsplit(a,3)
#print np.hsplit(a,(3,4)) # Split a after the third and the fourth column
a = np.floor(10*np.random.random((12,2)))
print a
np.vsplit(a,3)
输出 :
[[ 5. 2.]
[ 1. 3.]
[ 9. 6.]
[ 2. 2.]
[ 7. 2.]
[ 8. 2.]
[ 1. 7.]
[ 2. 8.]
[ 4. 4.]
[ 8. 5.]
[ 4. 3.]
[ 2. 3.]]
Out[37]:
[array([[ 5., 2.],
[ 1., 3.],
[ 9., 6.],
[ 2., 2.]]), array([[ 7., 2.],
[ 8., 2.],
[ 1., 7.],
[ 2., 8.]]), array([[ 4., 4.],
[ 8., 5.],
[ 4., 3.],
[ 2., 3.]])]
#Simple assignments make no copy of array objects or of their data.
a = np.arange(12)
b = a
# a and b are two names for the same ndarray object
b is a
b.shape = 3,4
print a.shape
print id(a)
print id(b)
输出 :
(3, 4)
82691200
82691200
#The view method creates a new array object that looks at the same data.
c = a.view()
c is a
c.shape = 2,6
#print a.shape
c[0,4] = 1234
a
输出 :
array([[ 0, 1, 2, 3],
[1234, 5, 6, 7],
[ 8, 9, 10, 11]])
#The copy method makes a complete copy of the array and its data.
d = a.copy()
d is a
d[0,0] = 9999
print d
print a
输出 :
[[9999 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
[[ 0 1 2 3]
[1234 5 6 7]
[ 8 9 10 11]]
import numpy as np
#data = np.sin(np.arange(20)).reshape(5,4)
#print data
#ind = data.argmax(axis=0)
#print ind
#data_max = data[ind, xrange(data.shape[1])]
#print data_max
all(data_max == data.max(axis=0))
输出 : True
a = np.arange(0, 40, 10)
b = np.tile(a, (3, 5))
print b
输出 :
[[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]
[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]]
a = np.arange(0, 40, 10) print a print '---' b = np.tile(a, (1, 4)) print b
#print a
#print b
a = np.array([[4, 3, 5], [1, 2, 1]])
#print a
#b = np.sort(a, axis=1)
#print b
#b
#a.sort(axis=1)
#print a
a = np.array([4, 3, 1, 2])
j = np.argsort(a)
print j
print a[j]
输出 :
[2 3 1 0]
[1 2 3 4]
网友评论