Jovian
⭐️
Sign In
Learn data science and machine learning by building real-world projects on Jovian

5 Numpy functions to make you a better data analyst

Numpy at it's core is a well optimized C code python library. It sets the standard of numerical and array computing today, as it is fast and powerful. Here we'll take a look at 5 numpy functions that can make you a better data analyst.

  • np.rot90
  • np.place
  • np.ndarray.flatten
  • np.column_stack
  • np.sin

The recommended way to run this notebook is to click the "Run" button at the top of this page, and select "Run on Binder". This will run the notebook on mybinder.org, a free online service for running Jupyter notebooks.

In [2]:
!pip install jovian --upgrade -q
In [3]:
import jovian
In [ ]:
jovian.commit(project='numpy-array-operations')
[jovian] Attempting to save notebook..

Let's begin by importing Numpy and listing out the functions covered in this notebook.

In [4]:
import numpy as np
In [5]:
# List of functions explained 
function1 = np.rot90
function2 = np.place
function3 = np.ndarray.flatten
function4 = np.column_stack
function5 = np.sin

Function 1 - np.rot90

This function allows us to rotate the values of an array by 90 digrees.

In [17]:
# Example 1 - working
# Lets suppose we have the following array
arr1 = [[4, 7], 
        [12, 56]]

np.rot90(arr1)
Out[17]:
array([[ 7, 56],
       [ 4, 12]])

By running the function on the array we see that it rotates the elements by 90 digrees. By default it is a left rotation, but we can set some parameters to define what we need, as shown in the next example.

In [32]:
# Example 2 - working
arr2 = [[5, 6, 7], 
        [8, 9, 10]]

np.rot90(arr2,1,(1,0))
Out[32]:
array([[ 8,  5],
       [ 9,  6],
       [10,  7]])

The number 1 after the array parameter declares how many times we want to rotate the array, and the values in the parenthesis show the direction of the rotation (axes). So (1,0) means rotate clockwise and (0,1) is the oposite.

In [39]:
# Example 3 - breaking (to illustrate when it breaks)
arr3 = [[2, 64, 3], 
        [12, 45, 10]]

np.rot90(arr3,3,(1,3))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-39-1e3563847643> in <module> 3 [12, 45, 10]] 4 ----> 5 np.rot90(arr3,3,(1,3)) <__array_function__ internals> in rot90(*args, **kwargs) /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/lib/function_base.py in rot90(m, k, axes) 119 120 if axes[0] == axes[1] or absolute(axes[0] - axes[1]) == m.ndim: --> 121 raise ValueError("Axes must be different.") 122 123 if (axes[0] >= m.ndim or axes[0] < -m.ndim ValueError: Axes must be different.

Of course the values of each axis must be 1 or 0 and not have the same value at the same time.

This is a useful function as it allows us to easily re-arange and manipulate data according to our needs.

In [12]:
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Please enter your API key ( from https://jovian.ml/ ): API KEY: ········ [jovian] Updating notebook "adrian26mccous/numpy-array-operations" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/adrian26mccous/numpy-array-operations

Function 2 - np.place

This function allows us to change the values of the array according to a mask that we create.

In [19]:
# Example 1 - working
arr4 = np.array([[1,2,3],
                [4,5,6]])

np.place(arr4, arr4>3, [12,13,14])

print(arr4)
[[ 1 2 3] [12 13 14]]

As you can see in the example above, the function takes 3 parameters. The first is the array we want to insert data, the second is our filter and the third the values we want to insert. We created a filter that replaces the values of the array higher than 3. So values like 4,5 and 6 become 12, 13 and 14.

In [10]:
# Example 2 - working
arr5 = np.array([[0,0,4],
                [2,0,5],
                [7,0,0]])

np.place(arr5, arr5==0, [1])
print(arr5)
[[1 1 4] [2 1 5] [7 1 1]]

We can also replace a single value by filtering the false one.

In [17]:
# Example 3 - breaking (to illustrate when it breaks)
arr5 = np.array([[0,0,4],
                [2,0,5],
                [7,0,0]])

np.place(arr5, arr5==1, [1,9,3])
print(arr5)
[[0 0 4] [2 0 5] [7 0 0]]

Obviously if the filter is not correct the array will not be changed

This function is useful on fixing or replacing many values without having to loop over the array.

In [18]:
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "adrian26mccous/numpy-array-operations" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/adrian26mccous/numpy-array-operations

Function 3 - np.ndarray.flatten

Flattens the array into one dimension

In [5]:
# Example 1 - working
arr6 = np.array([[1,2,3],
                 [4,5,6]])

np.ndarray.flatten(arr6)
Out[5]:
array([1, 2, 3, 4, 5, 6])

The 2D array was flattened into a 1D array.

In [9]:
# Example 2 - working
arr7 = np.array([[1,2,3],
                 [4,5,6]])

np.ndarray.flatten(arr7,'F')
Out[9]:
array([1, 4, 2, 5, 3, 6])

We can add a parameter to declare how to flatten the array. In this case 'F' stands for flatten in column-major (Fortran- style).

In [13]:
# Example 3 - breaking (to illustrate when it breaks)
arr7 = np.array([[[1,2,3],[4,5,6]],
                 [[7,8],[10,11]]])

np.ndarray.flatten(arr7,'F')
<ipython-input-13-605eec5a23ac>:2: VisibleDeprecationWarning: Creating an ndarray from ragged nested sequences (which is a list-or-tuple of lists-or-tuples-or ndarrays with different lengths or shapes) is deprecated. If you meant to do this, you must specify 'dtype=object' when creating the ndarray arr7 = np.array([[[1,2,3],[4,5,6]],
Out[13]:
array([list([1, 2, 3]), list([7, 8]), list([4, 5, 6]), list([10, 11])],
      dtype=object)

Array needs to be of constant size.

This function is useful on making a complicated multi-dimensional array more simple.

In [14]:
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "adrian26mccous/numpy-array-operations" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/adrian26mccous/numpy-array-operations

Function 4 - np.column_stack

The column_stack function stacks 1D arrays into columns as a 2D array.

In [20]:
# Example 1 - working
arr8 = np.array([1,2,3])
arr9 = np.array([4,5,6])

np.column_stack((arr8,arr9))
Out[20]:
array([[1, 4],
       [2, 5],
       [3, 6]])

The two arrays merged into one array as two columns.

In [23]:
# Example 2 - working
arr10 = np.array([1,2,3])
arr11 = np.array([4,5,6])
arr12 = np.array([7,8,9])
arr13 = np.array([11,13,12])

np.column_stack((arr10,arr11,arr12,arr13))
Out[23]:
array([[ 1,  4,  7, 11],
       [ 2,  5,  8, 13],
       [ 3,  6,  9, 12]])

Same with multiple arrays

In [21]:
# Example 3 - breaking (to illustrate when it breaks)
arr10 = np.array([1,2])
arr11 = np.array([4,5,6])

np.column_stack((arr10,arr11))
--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-21-ae4bba7369e7> in <module> 3 arr11 = np.array([4,5,6]) 4 ----> 5 np.column_stack((arr10,arr11)) <__array_function__ internals> in column_stack(*args, **kwargs) /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/lib/shape_base.py in column_stack(tup) 654 arr = array(arr, copy=False, subok=True, ndmin=2).T 655 arrays.append(arr) --> 656 return _nx.concatenate(arrays, 1) 657 658 <__array_function__ internals> in concatenate(*args, **kwargs) ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 0, the array at index 0 has size 2 and the array at index 1 has size 3

The two arrays must be of equal size.

Useful to modify how an array or multiple arrays look and function.

In [24]:
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "adrian26mccous/numpy-array-operations" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/adrian26mccous/numpy-array-operations

Function 5 - np.sin

This function calculates the trigonometric sin

In [14]:
# Example 1 - working
np.sin(3*np.pi/2)
Out[14]:
-1.0

Calculates the sin of 270°

In [17]:
# Example 2 - working
arr = np.array([3*np.pi/2,np.pi/2,-np.pi/2])
np.sin(arr)
Out[17]:
array([-1.,  1., -1.])

It also works with arrays

In [18]:
# Example 3 - breaking (to illustrate when it breaks)
np.sin('90 digrees')
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-18-605ce4cf6cd0> in <module> 1 # Example 3 - breaking (to illustrate when it breaks) ----> 2 np.sin('90 digrees') TypeError: ufunc 'sin' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''

The arguement of the function should be a numeric type value.

Really helpful in trigonometry and such appliances.

In [ ]:
jovian.commit()
[jovian] Attempting to save notebook..

Conclusion

We covered 5 functions of numpy but there are many more to discover and experiment with.

Reference Links

Provide links to your references and other interesting articles about Numpy arrays:

In [25]:
jovian.commit()
[jovian] Attempting to save notebook.. [jovian] Updating notebook "adrian26mccous/numpy-array-operations" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/adrian26mccous/numpy-array-operations
In [ ]: