Method
Description
Example
append(data1,
data2, dtype)
Add one or more values on
the end of an array to make a
longer array. Unless an axis
is specified the result will be
a = array([[1, 2, 3],
[4, 5,
6]])
b = append(a, [7,8,9])
a flattened, 1D array. If an
axis is specified the array
added must have the same
number of dimensions, and
naturally must match the
shape along that axis. Note it
will generally be quicker to
use empty() and then fill
array values by index rather
than call append() multiple
times.
# Makes flat 1 x 9 array
c = append(a, [[7,8,9]],
axis=0)
# Makes 3 x 3 array
d = append(a, [[4],[7]],
axis=1)
# Makes 2 x 4 array
arange(start,
end, step, dtype)
Generates an array of equally
spaced numbers, from a start
point, up to (but not
including) an end point using
a given step size. The start
defaults to zero and the step
to 1. The data type will be
inferred from the arguments
unless specifically stated.
Similar to range/xrange in
standard Python, but for
more data types.
arange(3.0)
# Floats: array([0.0, 1.0,
2.0])
arange(7, 3, -1)
# Ints: array([7, 6, 5,
4])
arange(0, 6 ,2, float)
# Floats: array([0.0, 2.0,
4.0])
arange(3.2, 3.5, 0.1)
# Floats: array([3.2, 3.3,
3.4])
argmax(arry,
axis)
or
a.argmax(axis)
Gets an array of indices that
specify the position of the
maximum value in an array,
either along a specified axis
or in the flattened 1D version
of the array if no axis is
specified.
a = array([[1,2,8],
[9,1,1]])
argmax(a) # 3
# Position of max in 1D
argmax(a, axis=0)
# array([1, 0, 0])
# Which row has max for
each col
a.argmax(axis=1)
# array([2, 0])
# Which col has max for
each row
argmin(arry,
axis)
or
a.argmax(axis)
As above, but finds the
indices for the minimum
value. Also an inbuilt
method of numpy.array.
a = array([[1,2,8],
[9,1,1]])
a.argmin(0) # axis=0
# array([0, 1, 1])
# Which row has min for
each col
argsort(arry,
axis)
or
a.argsort(axis)
Compares the values in an
array to generate an array of
their indices in value-sorted
order. If the axis is not
specified the result refers to a
flattened 1D version of the
array.
a = array([5,9,3,4,1,8])
sortIndices = a.argsort()
print(sortIndices)
# [4,2,3,0,5,1] – min val
at a[4]
print(a[sortIndices])
# [1,3,4,5,8,9]
argwhere(arry)
Finds the indices of the non-
zero (or true) elements of an
array. Unlike nonzero(), the
returned indices are grouped
by element, rather than by
axis.
a1 = array([[8, 0, 0],
[0,
0, 9]])
indices = argwhere(a1)
# array([[0, 0], [1, 2]])
# Positions of non-zero
elements
cov(data1,
data2)
Makes an array representing
the sample covariance matrix
for given array or array-like
data. Each data row is taken
to be a different variable, and
each data column represents
a separate data point/vector.
The covariance matrix
represents the correlations
between different pairs of
variables (data dimensions).
x = [0.0, 1.0, 2.0, 3.0]
y = [0.4, 2.2, 3.9, 5.8]
cov(x) # 1.666667
cov(y) # 5.342500
cov([x, y])
# array([[1.666667,
2.983333],
[2.983333, 5.342500]])
cross(arry1,
arry2)
The cross-product between
two vector arrays. The arrays
must be 2 or 3 dimensional.
For 3D arrays the result is a
vector array that is
perpendicular to both input
vectors (where possible). For
2D arrays the result is the
determinant of the associated
2×2 matrix.
cross([0,1,0], [0,0,1])
# array([1, 0, 0])
cross((3,1),(1,2))
# 5
dot(arry1,
arry2)
or
a.dot(arry)
The dot product or scalar
product between two arrays.
For 2D matrices the result is
matrix multiplication. For
1D vectors the result is a
scalar, equivalent to the
m1 = array([[ 1, 2],
[-2,
0]])
m2 = array([[-1, 1],
[2,
3]])
magnitude of the projection
of one vector on to the other.
m3 = dot(m1, m2)
# array([[ 3, 7],
# [ 2, -2]])
v1 = array([1,2,3,4])
v1.dot(array([4,3,2,1])) #
20
dstack(arrays)
Combines a sequence (e.g.
list) of arrays into a larger
array by stacking them
depth-wise, along their third
axis. For example, combines
separate 2D matrices into a
3D tensor.
m1 = array([[1,2,3],
[4,5,6]])
m2 = array([[7,8,9],
[0,1,2]])
m3 = dstack([m1, m2])
# array([[[1, 7],[2, 8],
[3, 9]],
[[4,
0],[5, 1],[6, 2]]])
print(m3.shape)
# (2, 3, 2)
empty(sizes,
dtype)
Generates a new empty
array, of specified size. The
array will actually be filled
with arbitrary (but small)
numbers, rather than zero.
The data type may be
specified, but otherwise
defaults to floating point.
The size can be an integer or
a tuple of integers, one for
each axis, specifying number
of rows, columns etc.
a = empty((2,1))
# 2 x 1 array,
arbitrarily:
# array([[ 1.34634549e-
316],
# [
1.81895505e-317]])
eye(nRows,
nCols)
Creates an identity matrix of
a specified size; a 2D array
with ones on the diagonal
and zeros elsewhere. If the
number of columns is not
specified the matrix is
square.
eye(3)
# array([[ 1.0, 0.0, 0.0],
# [ 0.0,
1.0, 0.0],
# [ 0.0,
0.0, 1.0]])
histogram(arry,
Do'stlaringiz bilan baham: |