Numpy and Scipy Library
Numpy and Scipy are the fundamental python packages for scientific computation and programing.If we want to do scientific computation in python we have to use these libraries
import numpy as np
import scipy as sp
Why Numpy and Scipy ?
The Basics scientific programming operations are Arrays, matrices, integration ,differential equation solver , statistics etc. By default python does not have any of these built in except some basic mathematical operations that only deals with variables not with array and matrices , so we are using Numpy and Scipy. Here we are going to see Numpy.
Numpy Arrays
Numpy Arrays are called n dimensional array(nd). It holds the data of same data type. These are some functions used in Numpy .
help()
import numpy as np
help(np.array)
Output
Help on built-in function array in module numpy:
array(...)
array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
Create an array.
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.
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 : {'K', 'A', 'C', 'F'}, optional
Specify the memory layout of the array. If object is not an array, the
newly created array will be in C order (row major) unless 'F' is
specified, in which case it will be in Fortran order (column major).
If object is an array the following holds.
===== ========= ===================================================
order no copy copy=True
===== ========= ===================================================
'K' unchanged F & C order preserved, otherwise most similar order
'A' unchanged F order if input is F and not C, otherwise C order
'C' C order C order
'F' F order F order
===== ========= ===================================================
When ``copy=False`` and a copy is made for other reasons, the result is
the same as if ``copy=True``, with some exceptions for `A`, see the
Notes section. The default order is 'K'.
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.
Returns
-------
out : ndarray
An array object satisfying the specified requirements.
See Also
--------
empty_like : Return an empty array with shape and type of input.
ones_like : Return an array of ones with shape and type of input.
zeros_like : Return an array of zeros with shape and type of input.
full_like : Return a new array with shape of input filled with value.
empty : Return a new uninitialized array.
ones : Return a new array setting values to one.
zeros : Return a new array setting values to zero.
full : Return a new array of given shape filled with value.
Notes
-----
When order is 'A' and `object` is an array in neither 'C' nor 'F' order,
and a copy is forced by a change in dtype, then the order of the result is
not necessarily 'C' as expected. This is likely a bug.
Examples
--------
>>> np.array([1, 2, 3])
array([1, 2, 3])
Upcasting:
>>> np.array([1, 2, 3.0])
array([ 1., 2., 3.])
More than one dimension:
>>> np.array([[1, 2], [3, 4]])
array([[1, 2],
[3, 4]])
Minimum dimensions 2:
>>> np.array([1, 2, 3], ndmin=2)
array([[1, 2, 3]])
Type provided:
>>> np.array([1, 2, 3], dtype=complex)
array([ 1.+0.j, 2.+0.j, 3.+0.j])
Data-type consisting of more than one element:
>>> x = np.array([(1,2),(3,4)],dtype=[('a','<i4'),('b','<i4')])
>>> x['a']
array([1, 3])
Creating an array from sub-classes:
>>> np.array(np.mat('1 2; 3 4'))
array([[1, 2],
[3, 4]])
>>> np.array(np.mat('1 2; 3 4'), subok=True)
matrix([[1, 2],
[3, 4]])
Arange() :- Creates Array of evenly spaced values
Syntax
arange ([start,] stop[, step,], dtype =None)
Input
import numpy as np
np.arange(1,10,2 ,"float")
Output
array([1., 3., 5., 7., 9.])
Linspace ():- creates Array filled evenly spaced values
Syntax:
linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None, axis=0)
Input
np.linspace(1,100,num=10,endpoint=False,retstep=True,dtype="int")
Output
(array([ 1, 10, 20, 30, 40, 50, 60, 70, 80, 90]), 9.9)
Eye ():- Returns array filled with zeros except in the k-th diagonal
import numpy as np
np.eye(4,k=-1)
Output
array([[0., 0., 0., 0.],
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.]])
Numpy Random Module
1) rand()
np.random.rand(4)
Output
array([0.90600296, 0.84742763, 0.21140014, 0.71782607])
2) randn()
np.random.randn(4)
Output
array([-1.36980338, 1.42326917, 2.14822216, -0.69217009])
3) ranf()
np.random.ranf(4)
Output
array([0.28127124, 0.41282905, 0.48456846, 0.25986008])
4) randint()
np.random.randint(4,size=(2,3))
Output
array([[0, 1, 3],
[0, 3, 1]])
conclusion:
Â
These are the different ways to create Numpy Array in python.I hope it will be useful for the beginners.
Comments