numpy is a powerful set of tools to perform mathematical operations of on lists of numbers. It works faster than normal python lists operations and can manupilate high dimentional arrays too.
Finding Help:
SciPy (pronounced “Sigh Pie”) is a Python-based ecosystem of open-source software for mathematics, science, and engineering.
So NumPy is a part of a bigger ecosystem of libraries that build on the optimized performance of NumPy NDArray.
It contain these core packages:
NumPyBase N-dimensional array package |
SciPyFundamental library for scientific computing |
MatplotlibComprehensive 2D Plotting |
|||
IPythonEnhanced Interactive Console |
SymPySymbolic mathematics |
PandasData structures & analysis |
In [18]:
import numpy as np
In [19]:
np.arange(10)
Out[19]:
In [20]:
np.arange(1,10)
Out[20]:
In [21]:
np.arange(1,10, 0.5)
Out[21]:
In [22]:
np.arange(1,10, 3)
Out[22]:
In [23]:
np.arange(1,10, 2, dtype=np.float64)
Out[23]:
In [24]:
ds = np.arange(1,10,2)
ds.ndim
Out[24]:
In [25]:
ds.shape
Out[25]:
In [26]:
ds.size
Out[26]:
In [27]:
ds.dtype
Out[27]:
In [28]:
ds.itemsize
Out[28]:
In [29]:
x=ds.data
list(x)
Out[29]:
In [30]:
ds
Out[30]:
In [31]:
# Memory Usage
ds.size * ds.itemsize
Out[31]:
We will compare the time it takes to create two lists and do some basic operations on them.
In [32]:
%%capture timeit_results
# Regular Python
%timeit python_list_1 = range(1,1000)
python_list_1 = range(1,1000)
python_list_2 = range(1,1000)
#Numpy
%timeit numpy_list_1 = np.arange(1,1000)
numpy_list_1 = np.arange(1,1000)
numpy_list_2 = np.arange(1,1000)
In [33]:
print timeit_results
In [34]:
# Function to calculate time in seconds
def return_time(timeit_result):
temp_time = float(timeit_result.split(" ")[5])
temp_unit = timeit_result.split(" ")[6]
if temp_unit == "ms":
temp_time = temp_time * 1e-3
elif temp_unit == "us":
temp_time = temp_time * 1e-6
elif temp_unit == "ns":
temp_time = temp_time * 1e-9
return temp_time
In [35]:
python_time = return_time(timeit_results.stdout.split("\n")[0])
numpy_time = return_time(timeit_results.stdout.split("\n")[1])
print "Python/NumPy: %.1f" % (python_time/numpy_time)
In [ ]:
%%capture timeit_python
%%timeit
# Regular Python
[(x + y) for x, y in zip(python_list_1, python_list_2)]
[(x - y) for x, y in zip(python_list_1, python_list_2)]
[(x * y) for x, y in zip(python_list_1, python_list_2)]
[(x / y) for x, y in zip(python_list_1, python_list_2)];
In [ ]:
print timeit_python
In [ ]:
%%capture timeit_numpy
%%timeit
#Numpy
numpy_list_1 + numpy_list_2
numpy_list_1 - numpy_list_2
numpy_list_1 * numpy_list_2
numpy_list_1 / numpy_list_2;
In [ ]:
print timeit_numpy
In [ ]:
python_time = return_time(timeit_python.stdout)
numpy_time = return_time(timeit_numpy.stdout)
print "Python/NumPy: %.1f" % (python_time/numpy_time)
Parameters
----------
object : array_like
An array, any object exposing the array interface, an
object whose __array__ method returns an array, or any
(nested) sequence.
dtype : data-type, optional
The desired data-type for the array. If not given, then
the type will be determined as the minimum type required
to hold the objects in the sequence. This argument can only
be used to 'upcast' the array. For downcasting, use the
.astype(t) method.
copy : bool, optional
If true (default), then the object is copied. Otherwise, a copy
will only be made if __array__ returns a copy, if obj is a
nested sequence, or if a copy is needed to satisfy any of the other
requirements (`dtype`, `order`, etc.).
order : {'C', 'F', 'A'}, optional
Specify the order of the array. If order is 'C' (default), then the
array will be in C-contiguous order (last-index varies the
fastest). If order is 'F', then the returned array
will be in Fortran-contiguous order (first-index varies the
fastest). If order is 'A', then the returned array may
be in any order (either C-, Fortran-contiguous, or even
discontiguous).
subok : bool, optional
If True, then sub-classes will be passed-through, otherwise
the returned array will be forced to be a base-class array (default).
ndmin : int, optional
Specifies the minimum number of dimensions that the resulting
array should have. Ones will be pre-pended to the shape as
needed to meet this requirement.
In [ ]:
np.array([1,2,3,4,5])
In [ ]:
np.array([[1,2],[3,4],[5,6]])
Parameters
----------
shape : int or sequence of ints
Shape of the new array, e.g., ``(2, 3)`` or ``2``.
dtype : data-type, optional
The desired data-type for the array, e.g., `numpy.int8`. Default is
`numpy.float64`.
order : {'C', 'F'}, optional
Whether to store multidimensional data in C- or Fortran-contiguous
(row- or column-wise) order in memory.
In [ ]:
np.zeros((3,4))
In [ ]:
np.zeros((3,4), dtype=np.int64)
In [ ]:
np.ones((3,4))
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.
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.
In [ ]:
np.linspace(1,5)
In [ ]:
np.linspace(0,2,num=4)
In [ ]:
np.linspace(0,2,num=4,endpoint=False)
Parameters
----------
size : int or tuple of ints, optional
Defines the shape of the returned array of random floats. If None
(the default), returns a single float.
In [ ]:
np.random.random((2,3))
In [ ]:
np.random.random_sample((2,3))
In [ ]:
data_set = np.random.random((2,3))
data_set
Parameters
----------
a : array_like
Input data.
axis : int, optional
Axis along which to operate. By default, flattened input is used.
out : ndarray, optional
Alternative output array in which to place the result. Must
be of the same shape and buffer length as the expected output.
See `doc.ufuncs` (Section "Output arguments") for more details.
keepdims : bool, optional
If this is set to True, the axes which are reduced are left
in the result as dimensions with size one. With this option,
the result will broadcast correctly against the original `arr`.
In [ ]:
np.max(data_set)
In [ ]:
np.max(data_set, axis=0)
In [ ]:
np.max(data_set, axis=1)
In [ ]:
np.min(data_set)
In [ ]:
np.mean(data_set)
In [ ]:
np.median(data_set)
In [ ]:
np.std(data_set)
In [ ]:
np.sum(data_set)
In [ ]:
np.reshape(data_set, (3,2))
In [ ]:
np.reshape(data_set, (6,1))
In [ ]:
np.reshape(data_set, (6))
In [ ]:
np.ravel(data_set)
In [ ]:
data_set = np.random.random((5,10))
data_set
In [ ]:
data_set[1]
In [ ]:
data_set[1][0]
In [ ]:
data_set[1,0]
In [ ]:
data_set[2:4]
In [ ]:
data_set[2:4,0]
In [ ]:
data_set[2:4,0:2]
In [ ]:
data_set[:,0]
In [ ]:
data_set[2:4:1]
In [ ]:
data_set[::]
In [ ]:
data_set[::2]
In [ ]:
data_set[2:4]
In [ ]:
data_set[2:4,::2]
In [1]:
import numpy as np
# Matrix A
A = np.array([[1,2],[3,4]])
# Matrix B
B = np.array([[3,4],[5,6]])
In [ ]:
A+B
In [ ]:
A-B
In [ ]:
A*B
In [2]:
A.dot(B)
Out[2]:
In [ ]:
A/B
In [ ]:
np.square(A)
In [ ]:
np.power(A,3) #cube of matrix
In [3]:
A.transpose()
Out[3]:
In [5]:
np.linalg.inv(A)
Out[5]: