Chandan Rajpurohit

An Artist With Technical Skills

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. 

#initialization

a = np.array([1,2,3,4,5])

b = np.array([[‘a’,’b’,’c’],

                     [‘x’,’y’,’z’]])

#slicing at index 0

a[0]

$o/p : 1

#slicing last element using negative index

a[-1]

$o/p : 4

#slicing elements from position 1 to 5

a[1: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)

b[0,1]

$o/p : ‘b’

#slicing first row

b[0,]

$o/p : array([‘a’,’b’,’c’])

#slicing last column

b[:,2]

$o/p : array([‘c’,’z’])

Boolean indexing 

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.

#initialization

Arr = np.arange(1,10)

$o/p : array([1,2,3,4,5,6,7,8,9])

#subsetting array ‘Arr’ 

Arr[np.array([2,5,5,1])]

$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])

#filtering

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:

print(element)

$o/p : 

14

25

39

18

40

10

33

36

29

25

Array manipulation

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

a*b

$o/p : array([3,6,9])

Along with efficient number processing, NumPy also provides a set of methods for array manipulation.

  1. exp(*args) returns exponential of all elements in the input array.

np.exp(a)

$o/p : array([2.71828183,7.3890561,20.08553692])

  1. sqrt(*args) returns positive square-root of an array.

np.sqrt([1,4,9,16,25])

$o/p : array([1.,2.,3.,4.,5.])

  1. reshape(new_shape) gives new shape to an array without changing its data.

np.reshape([0,1,2,3,4,5,6,7],(4,2))

$o/p : array([[0,1],[2,3],[4,5],[6,7]])

  1. 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.

np.resize([1,2,3,4],(4,2))

$o/p : array([[1,2],[3,4],[1,2],[3,4]])

  1. round(a,decimals=0) returns a rounded array to given decimals. If decimals are not passed it is 0 by default.
  1. sort(a,kind=’quicksort’) returns sorted copy of array. Default algorithm is quick sort.
  1. transpose() permutes the dimensions of an array

a = np.array([[0,1,2,3],[4,5,6,7]])

a.transpose()

$o/p : array([[0,4],[1,5],[2,6],[3,7]])

NumPy has more and more features. It is suited for data processing.

Happy learning!

%d bloggers like this: