Search notes:
Python library: numpy
numpy
provides many function that operate on or manipulate n-dimensional array objects.
>>> import numpy as np
>>> a = np.array([1,2,3])
>>> a.sum()
6
>>> a**2
array([1, 4, 9])
shape
determines the dimensions:
>>> x = np.array([ [1,2,3],
[4,5,6] ])
>>> x.shape
(2, 3)
T
transposes:
>>> y = x.T
>>> y
array([[1, 4],
[2, 5],
[3, 6]])
>>> y.shape
(3, 2)
Create arrays of zeros and ones:
>>> np.zeros(4)
array([0., 0., 0., 0.])
>>> np.ones(3)
array([1., 1., 1.])
linspace(s, e, n
) create an array with n
evenly spread out values between s
and e
:
>>> np.linspace(-1, 1, 5)
array([-1. , -0.5, 0. , 0.5, 1. ])
Stacking arrays
>>> x = np.array([ [0, 1, 2],
[3, 4, 5] ])
>>> y = np.array([ [6, 7 ],
[8, 9 ] ])
>>> np.hstack( (x,y) )
array([[0, 1, 2, 6, 7],
[3, 4, 5, 8, 9]])
linspace
import numpy
values = numpy.linspace(-1, 1, num=20, endpoint=True)
# <type 'numpy.ndarray'>
print type(values)
# <type 'numpy.ndarray'>
print values
# [-1. -0.89473684 -0.78947368 -0.68421053 -0.57894737 -0.47368421
# -0.36842105 -0.26315789 -0.15789474 -0.05263158 0.05263158 0.15789474
# 0.26315789 0.36842105 0.47368421 0.57894737 0.68421053 0.78947368
# 0.89473684 1. ]
mean
import numpy
import pandas
print numpy.mean( [ 10, 20, 30, 40, 1000] )
# 220.0
df = pandas.DataFrame(
{ 'col_1': pandas.Series( [ 10 , 20 , 30 , 40 ] ),
'col_2': pandas.Series( ['foo','bar','baz','qux'] ),
'col_3': pandas.Series( [ 'X', 'Y', 'Z', 'Q'] )
})
print numpy.mean(df.col_1)
# 25.0
average
import numpy
print numpy.average( [ 10, 20, 30, 40 ] )
# 25.0
Members of numpy
abs | ? |
absolute | ? |
add | ? |
add_docstring() | Built-in function |
add_newdoc() | |
_add_newdoc_ufunc() | Built-in function |
add_newdoc_ufunc() | Built-in function |
all() | |
allclose() | Compare isclose() |
ALLOW_THREADS | int object |
alltrue() | |
amax() | |
amin() | |
angle() | |
any() | |
append() | |
apply_along_axis() | |
apply_over_axes() | |
arange() | Built-in function |
arccos | ? |
arccosh | ? |
arcsin | ? |
arcsinh | ? |
arctan | ? |
arctan2 | ? |
arctanh | ? |
argmax() | |
argmin() | |
argpartition() | |
argsort() | |
argwhere() | |
around() | |
array() | Creates an ndarray object |
array2string() | |
array_equal() | |
array_equiv() | |
array_repr() | |
array_split() | |
array_str() | |
asanyarray() | Built-in function |
asarray() | Built-in function |
asarray_chkfinite() | |
ascontiguousarray() | Built-in function |
asfarray() | |
asfortranarray() | Built-in function |
asmatrix() | |
atleast_1d() | |
atleast_2d() | |
atleast_3d() | |
average() | |
AxisError | numpy.AxisError class |
bartlett() | |
base_repr() | |
binary_repr() | |
bincount() | |
bitwise_and | ? |
bitwise_not | ? |
bitwise_or | ? |
bitwise_xor | ? |
blackman() | |
block() | |
bmat() | |
bool_ | numpy.bool_ class |
broadcast | numpy.broadcast class |
broadcast_arrays() | |
broadcast_shapes() | |
broadcast_to() | |
BUFSIZE | int object |
busdaycalendar | numpy.busdaycalendar class |
busday_count() | |
busday_offset() | |
byte | numpy.int8 class |
byte_bounds() | |
bytes_ | numpy.bytes_ class |
c_ | ? |
can_cast() | |
cast | ? |
cbrt | ? |
cdouble | numpy.complex128 class |
ceil | ? |
cfloat | numpy.complex128 class |
char | Module |
character | numpy.character class |
chararray | numpy.chararray class |
choose() | |
CLIP | int object |
clip() | |
clongdouble | numpy.complex256 class |
clongfloat | numpy.complex256 class |
column_stack() | |
common_type() | |
compare_chararrays() | Built-in function |
compat | Module |
complex_ | numpy.complex128 class |
complex128 | numpy.complex128 class |
complex256 | numpy.complex256 class |
complex64 | numpy.complex64 class |
complexfloating | numpy.complexfloating class |
ComplexWarning | numpy.ComplexWarning class |
compress() | |
concatenate() | |
conj | ? |
conjugate | ? |
convolve() | |
copy() | |
_CopyMode | ? |
copysign | ? |
copyto() | |
corrcoef() | |
correlate() | |
cos | ? |
cosh | ? |
count_nonzero() | |
cov() | |
cross() | |
csingle | numpy.complex64 class |
ctypeslib | Module |
cumprod() | |
cumproduct() | |
cumsum() | |
DataSource | numpy.DataSource class |
datetime64 | numpy.datetime64 class |
datetime_as_string() | |
datetime_data() | Built-in function |
deg2rad | ? |
degrees | ? |
delete() | |
deprecate() | |
deprecate_with_doc() | |
diag() | |
diagflat() | |
diag_indices() | |
diag_indices_from() | |
diagonal() | |
diff() | |
digitize() | |
disp() | |
_distributor_init | Module |
divide | ? |
divmod | ? |
dot() | |
double | numpy.float64 class |
dsplit() | |
dstack() | |
dtype | ? |
e | float object |
ediff1d() | |
einsum() | |
einsum_path() | |
emath | Module |
empty() | Creates an ndarray object.j |
empty_like() | |
equal | ? |
ERR_CALL | int object |
ERR_DEFAULT | int object |
ERR_IGNORE | int object |
ERR_LOG | int object |
ERR_PRINT | int object |
ERR_RAISE | int object |
errstate | numpy.errstate class |
ERR_WARN | int object |
euler_gamma | float object |
exp | ? |
exp2 | ? |
expand_dims() | |
expm1 | ? |
extract() | |
eye() | |
fabs | ? |
False_ | ? |
fastCopyAndTranspose() | Built-in function |
fft | Module |
fill_diagonal() | |
_financial_names | list object |
find_common_type() | |
finfo | numpy.finfo class |
fix() | |
flatiter | numpy.flatiter class |
flatnonzero() | |
flexible | numpy.flexible class |
flip() | |
fliplr() | |
flipud() | |
float_ | numpy.float64 class |
float128 | numpy.float128 class |
float16 | numpy.float16 class |
float32 | numpy.float32 class |
float64 | numpy.float64 class |
floating | numpy.floating class |
FLOATING_POINT_SUPPORT | int object |
float_power | ? |
floor | ? |
floor_divide | ? |
fmax | ? |
fmin | ? |
fmod | ? |
format_float_positional() | |
format_float_scientific() | |
format_parser | numpy.format_parser class |
FPE_DIVIDEBYZERO | int object |
FPE_INVALID | int object |
FPE_OVERFLOW | int object |
FPE_UNDERFLOW | int object |
frexp | ? |
frombuffer() | Built-in function |
from_dlpack() | Built-in function |
fromfile() | Built-in function |
fromfunction() | |
fromiter() | Built-in function |
frompyfunc() | Built-in function |
fromregex() | |
fromstring() | Built-in function |
full() | |
full_like() | |
gcd | ? |
generic | numpy.generic class |
genfromtxt() | |
geomspace() | |
get_array_wrap() | |
getbufsize() | |
geterr() | |
geterrcall() | |
geterrobj() | Built-in function |
get_include() | |
get_printoptions() | |
_get_promotion_state() | Built-in function |
_globals | Module |
gradient() | |
greater | ? |
greater_equal | ? |
half | numpy.float16 class |
hamming() | |
hanning() | |
heaviside | ? |
histogram() | |
histogram2d() | |
histogram_bin_edges() | |
histogramdd() | |
hsplit() | |
hstack() | |
hypot | ? |
i0() | |
identity() | |
iinfo | numpy.iinfo class |
imag() | |
in1d() | |
index_exp | ? |
indices() | |
inexact | numpy.inexact class |
Inf | float object |
inf | float object |
Infinity | float object |
info() | |
infty | float object |
inner() | |
insert() | |
int_ | numpy.int64 class |
int16 | numpy.int16 class |
int32 | numpy.int32 class |
int64 | numpy.int64 class |
int8 | numpy.int8 class |
intc | numpy.int32 class |
integer | numpy.integer class |
interp() | |
intersect1d() | |
intp | numpy.int64 class |
invert | ? |
is_busday() | |
isclose() | Compare allclose() |
iscomplex() | |
iscomplexobj() | |
isfinite | ? |
isfortran() | |
isin() | |
isinf | ? |
isnan | ? |
isnat | ? |
isneginf() | |
isposinf() | |
isreal() | |
isrealobj() | |
isscalar() | |
issctype() | |
issubclass_() | |
issubdtype() | |
issubsctype() | |
iterable() | |
ix_() | |
kaiser() | |
kernel_version | tuple object |
kron() | |
lcm | ? |
ldexp | ? |
left_shift | ? |
less | ? |
less_equal | ? |
lexsort() | |
lib | Module |
linalg | Module |
linspace() | |
little_endian | bool object |
load() | |
loadtxt() | |
log | ? |
log10 | ? |
log1p | ? |
log2 | ? |
logaddexp | ? |
logaddexp2 | ? |
logical_and | ? |
logical_not | ? |
logical_or | ? |
logical_xor | ? |
logspace() | |
longcomplex | numpy.complex256 class |
longdouble | numpy.float128 class |
longfloat | numpy.float128 class |
longlong | numpy.longlong class |
lookfor() | |
ma | Module |
mask_indices() | |
_mat | Module |
mat() | |
math | Module |
matmul | ? |
matrix | numpy.matrix class |
max() | |
MAXDIMS | int object |
maximum | ? |
maximum_sctype() | |
MAY_SHARE_BOUNDS | int object |
MAY_SHARE_EXACT | int object |
may_share_memory() | |
mean() | |
median() | |
memmap | numpy.memmap class |
meshgrid() | useful to evaluate functions on a grid. |
mgrid | ? |
min() | |
minimum | ? |
min_scalar_type() | |
mintypecode() | |
mod | ? |
modf | ? |
ModuleDeprecationWarning | numpy.ModuleDeprecationWarning class |
moveaxis() | |
msort() | |
multiply | ? |
NAN | float object |
NaN | float object |
nan | float object |
nanargmax() | |
nanargmin() | |
nancumprod() | |
nancumsum() | |
nanmax() | |
nanmean() | |
nanmedian() | |
nanmin() | |
nanpercentile() | |
nanprod() | |
nanquantile() | |
nanstd() | |
nansum() | |
nan_to_num() | |
nanvar() | |
nbytes | ? |
ndarray | A class that represents a multidimensional array of items of the same data type. |
ndenumerate | numpy.ndenumerate class |
ndim() | |
ndindex | numpy.ndindex class |
nditer | numpy.nditer class |
negative | ? |
nested_iters() | Built-in function |
newaxis | NoneType object |
nextafter | ? |
NINF | float object |
_no_nep50_warning() | |
nonzero() | |
not_equal | ? |
_NoValue | ? |
numarray | str object |
number | numpy.number class |
NZERO | float object |
obj2sctype() | |
object_ | numpy.object_ class |
ogrid | ? |
oldnumeric | str object |
ones() | |
ones_like() | |
outer() | |
packbits() | |
pad() | |
partition() | |
percentile() | |
pi | float object |
piecewise() | |
PINF | float object |
place() | |
poly() | |
poly1d | numpy.poly1d class |
polyadd() | |
polyder() | |
polydiv() | |
polyfit() | |
polyint() | |
polymul() | |
polynomial | Module |
polysub() | |
polyval() | |
positive | ? |
power | ? |
printoptions() | |
prod() | |
product() | |
promote_types() | Built-in function |
ptp() | |
put() | |
put_along_axis() | |
putmask() | |
_pyinstaller_hooks_dir() | |
_pytesttester | Module |
PZERO | float object |
quantile() | |
r_ | ? |
rad2deg | ? |
radians | ? |
RAISE | int object |
random | Module |
RankWarning | numpy.RankWarning class |
ravel() | |
ravel_multi_index() | |
real() | |
real_if_close() | |
rec | Module |
recarray | numpy.recarray class |
recfromcsv() | |
recfromtxt() | |
reciprocal | ? |
record | numpy.record class |
remainder | ? |
repeat() | |
require() | |
reshape() | Change an array's shape and reuse the array's data. Can also be used to reduce the dimensions of a dataframe. |
resize() | |
result_type() | |
right_shift | ? |
rint | ? |
roll() | |
rollaxis() | |
roots() | |
rot90() | |
round() | |
round_() | |
row_stack() | |
s_ | ? |
safe_eval() | |
save() | |
savetxt() | |
savez() | |
savez_compressed() | |
ScalarType | tuple object |
sctype2char() | |
sctypeDict | dict object |
sctypes | dict object |
searchsorted() | |
select() | |
setbufsize() | |
setdiff1d() | |
seterr() | |
seterrcall() | |
seterrobj() | Built-in function |
set_numeric_ops() | Built-in function |
set_printoptions() | |
_set_promotion_state() | Built-in function |
set_string_function() | |
setxor1d() | |
shape() | |
shares_memory() | |
SHIFT_DIVIDEBYZERO | int object |
SHIFT_INVALID | int object |
SHIFT_OVERFLOW | int object |
SHIFT_UNDERFLOW | int object |
short | numpy.int16 class |
show_config() | |
show_runtime() | |
sign | ? |
signbit | ? |
signedinteger | numpy.signedinteger class |
sin | ? |
sinc() | |
single | numpy.float32 class |
singlecomplex | numpy.complex64 class |
sinh | ? |
size() | |
sometrue() | |
sort() | |
sort_complex() | |
source() | |
spacing | ? |
split() | |
sqrt | ? |
square | ? |
squeeze() | |
stack() | |
std() | |
str_ | numpy.str_ class |
string_ | numpy.bytes_ class |
subtract | ? |
sum() | |
swapaxes() | |
take() | |
take_along_axis() | |
tan | ? |
tanh | ? |
tensordot() | |
test | ? |
Tester | numpy.testing._private.nosetester.NoseTester class |
testing | Module |
tile() | |
timedelta64 | numpy.timedelta64 class |
TooHardError | numpy.TooHardError class |
trace() | |
tracemalloc_domain | int object |
transpose() | |
trapz() | |
tri() | |
tril() | |
tril_indices() | |
tril_indices_from() | |
trim_zeros() | |
triu() | |
triu_indices() | |
triu_indices_from() | |
True_ | ? |
true_divide | ? |
trunc | ? |
typecodes | dict object |
typename() | |
ubyte | numpy.uint8 class |
ufunc | numpy.ufunc class |
_UFUNC_API | PyCapsule object |
UFUNC_BUFSIZE_DEFAULT | int object |
UFUNC_PYVALS_NAME | str object |
uint | numpy.uint64 class |
uint16 | numpy.uint16 class |
uint32 | numpy.uint32 class |
uint64 | numpy.uint64 class |
uint8 | numpy.uint8 class |
uintc | numpy.uint32 class |
uintp | numpy.uint64 class |
ulonglong | numpy.ulonglong class |
unicode_ | numpy.str_ class |
union1d() | |
unique() | |
unpackbits() | |
unravel_index() | |
unsignedinteger | numpy.unsignedinteger class |
unwrap() | |
use_hugepage | int object |
ushort | numpy.uint16 class |
vander() | |
var() | |
vdot() | |
vectorize | numpy.vectorize class |
_version | Module |
version | Module |
VisibleDeprecationWarning | numpy.VisibleDeprecationWarning class |
void | numpy.void class |
vsplit() | |
vstack() | |
where() | |
who() | |
WRAP | int object |
zeros() | Creates an ndarray whose elements are filled with zeros. |
zeros_like() | |
Misc
Numpy indexes elements in the form (row, column), which is the convention of linear algebra.
See also
numpy is a prerequisite for nanoGPT
The
Hierarchical Data Format (HDF5) is useful to store huge amounts of numerical data and then manipulate it with NumPy.