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 [1]:
import numpy as np
In [2]:
np.arange(10)
Out[2]:
In [3]:
np.arange(1,10)
Out[3]:
In [4]:
np.arange(1,10, 0.5)
Out[4]:
In [5]:
np.arange(1,10, 3)
Out[5]:
In [6]:
np.arange(1,10, 2, dtype=np.float64)
Out[6]:
In [7]:
ds = np.arange(1,10,2)
ds.ndim
Out[7]:
In [8]:
ds.shape
Out[8]:
In [9]:
ds.size
Out[9]:
In [10]:
ds.dtype
Out[10]:
In [11]:
ds.itemsize
Out[11]:
In [12]:
x=ds.data
list(x)
Out[12]:
In [13]:
ds
Out[13]:
In [14]:
# Memory Usage
ds.size * ds.itemsize
Out[14]:
We will compare the time it takes to create two lists and do some basic operations on them.
In [15]:
%%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 [16]:
print timeit_results
In [17]:
# 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 [18]:
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 [19]:
%%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 [20]:
print timeit_python
In [21]:
%%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 [22]:
print timeit_numpy
In [23]:
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 [24]:
np.array([1,2,3,4,5])
Out[24]:
In [25]:
np.array([[1,2],[3,4],[5,6]])
Out[25]:
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 [26]:
np.zeros((3,4))
Out[26]:
In [27]:
np.zeros((3,4), dtype=np.int64)
Out[27]:
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 [28]:
np.linspace(1,5)
Out[28]:
In [29]:
np.linspace(0,2,num=4)
Out[29]:
In [30]:
np.linspace(0,2,num=4,endpoint=False)
Out[30]:
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 [31]:
np.random.random((2,3))
Out[31]:
In [32]:
np.random.random_sample((2,3))
Out[32]:
In [33]:
data_set = np.random.random((2,3))
data_set
Out[33]:
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 [34]:
np.max(data_set)
Out[34]:
In [35]:
np.max(data_set, axis=0)
Out[35]:
In [36]:
np.max(data_set, axis=1)
Out[36]:
In [37]:
np.min(data_set)
Out[37]:
In [38]:
np.mean(data_set)
Out[38]:
In [39]:
np.median(data_set)
Out[39]:
In [40]:
np.std(data_set)
Out[40]:
In [41]:
np.sum(data_set)
Out[41]:
In [42]:
np.reshape(data_set, (3,2))
Out[42]:
In [43]:
np.reshape(data_set, (6,1))
Out[43]:
In [44]:
np.reshape(data_set, (6))
Out[44]:
In [45]:
np.ravel(data_set)
Out[45]:
In [46]:
data_set = np.random.random((5,10))
data_set
Out[46]:
In [47]:
data_set[1]
Out[47]:
In [48]:
data_set[1][0]
Out[48]:
In [49]:
data_set[1,0]
Out[49]:
In [50]:
data_set[2:4]
Out[50]:
In [51]:
data_set[2:4,0]
Out[51]:
In [52]:
data_set[2:4,0:2]
Out[52]:
In [53]:
data_set[:,0]
Out[53]:
In [54]:
data_set[2:4:1]
Out[54]:
In [55]:
data_set[::]
Out[55]:
In [56]:
data_set[::2]
Out[56]:
In [57]:
data_set[2:4]
Out[57]:
In [58]:
data_set[2:4,::2]
Out[58]: