Earlier we have created arrays and generated random samples using numpy. Now, we will follow up NumPy and know more about array and NumPy.
Array Indexing and Iterating
NumPy is a great library if you are working with data. It has support for efficient number crunching and easy to use. NumPy provides indexing and iterating similar to lists. One can use  to subset NumPy arrays, Built-in constructs for iteration, and built-in method to slice NumPy arrays
Indexing and Subsetting
NumPy arrays allow indexing similar to Lists. Index starts with 0 and each element in the array is associated with a unique index.
a = np.array([1,2,3,4,5])
b = np.array([[‘a’,’b’,’c’],
#slicing at index 0
$o/p : 1
#slicing last element using negative index
$o/p : 4
#slicing elements from position 1 to 5
$o/p : array([2,3,4,5])
For 2D array [r,c] where r is row and c is column is used
#slicing the element at (0,1)
$o/p : ‘b’
#slicing first row
$o/p : array([‘a’,’b’,’c’])
#slicing last column
$o/p : array([‘c’,’z’])
NumPy arrays can be indexed with other arrays or lists. Arrays used for indexing are called index arrays. It is a simple array which can subset other arrays. When an array is indexed using another array, a copy of the original data is returned, not a view as one gets for slices.
Arr = np.arange(1,10)
$o/p : array([1,2,3,4,5,6,7,8,9])
#subsetting array ‘Arr’
$o/p : array([3,6,6,2])
Extending this concept, an array can be indexed with itself. Using logical operators, NumPy arrays can be filtered as desired.
a = np.random.randint(1,50,10)
$o/p : array([14,25,39,18,40,10,33,36,29,25])
a[a > 30]
$o/p : array ([39,40,33,36])
Iterating over Arrays
NumPy arrays are iterable objects in python. We can use iter() and next() methods as with any iterables directly.
for element in a:
NumPy defines a new data type called ndarray for array objects. numPy provides flexible and useful array manipulation techniques using broadcasting. Broadcasting describes how NumPy treats arrays of different shapes during arithmetic operations.
NumPy broadcasting rule relaxes constraints of array’s shapes to meet certain constraints.
a = np.array([1,2,3])
b = 3
$o/p : array([3,6,9])
Along with efficient number processing, NumPy also provides a set of methods for array manipulation.
- exp(*args) returns exponential of all elements in the input array.
$o/p : array([2.71828183,7.3890561,20.08553692])
- sqrt(*args) returns positive square-root of an array.
$o/p : array([1.,2.,3.,4.,5.])
- reshape(new_shape) gives new shape to an array without changing its data.
$o/p : array([[0,1],[2,3],[4,5],[6,7]])
- resize(a,new_shape) returns a new array of specified shape. If the new array is larger than the original array, then the new array is filled with copies of a.
$o/p : array([[1,2],[3,4],[1,2],[3,4]])
- round(a,decimals=0) returns a rounded array to given decimals. If decimals are not passed it is 0 by default.
- sort(a,kind=’quicksort’) returns sorted copy of array. Default algorithm is quick sort.
- transpose() permutes the dimensions of an array
a = np.array([[0,1,2,3],[4,5,6,7]])
$o/p : array([[0,4],[1,5],[2,6],[3,7]])
NumPy has more and more features. It is suited for data processing.