Learn data science and machine learning by building real-world projects on Jovian

## The Not So Famous Five of Numpy

#### Subtitle Here

Write a short introduction about Numpy and list the chosen functions.

• histogram - Computes the histogram of a set of data
• corrcoef - Return Pearson product-moment correlation coefficients
• count_nonzero - Counts the number of non-zero values in the array a.
• ravel - Return a contiguous flattened array.
• nanstd - Compute the standard deviation along the specified axis, while ignoring NaNs.

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 :
``!pip install jovian --upgrade -q``
In :
``import jovian``
In :
``jovian.commit(project='numpy-array-operations')``
```[jovian] Attempting to save notebook.. [jovian] Please enter your API key ( from https://jovian.ml/ ): API KEY: ········ [jovian] Updating notebook "abhinavyadav7/numpy-array-operations" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/abhinavyadav7/numpy-array-operations ```
Out:
``'https://jovian.ml/abhinavyadav7/numpy-array-operations'``

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

In :
``import numpy as np``
In :
``````# List of functions explained
function1 = np.histogram  # (change this)
function2 = np.corrcoef
function3 = np.count_nonzero
function4 = np.ravel
function5 = np.nanstd
``````
In :
``jovian.commit(project='numpy-array-operations')``
```[jovian] Attempting to save notebook.. [jovian] Updating notebook "abhinavyadav7/numpy-array-operations" on https://jovian.ml/ [jovian] Uploading notebook.. [jovian] Capturing environment.. [jovian] Committed successfully! https://jovian.ml/abhinavyadav7/numpy-array-operations ```
Out:
``'https://jovian.ml/abhinavyadav7/numpy-array-operations'``

### Function 1 - np.histogram()

Computes the histogram of a set of data i.e. gives the frequency of data occuring in the data set

In :
``````arr = np.random.randint(11, size = 50)
np.histogram(arr)
``````
Out:
``````(array([ 4,  2,  2,  4, 12,  4,  5,  5,  4,  8]),
array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10.]))``````

The example defines arr as a series with numbers bw 1 and 10 at 50 data set points. The frquency of occurence of each of those numbers is given by the np.histogram function. The function is extremely helpful in creating histograms to get an idea about the data

In :
``````# Example 2 - working
arr = np.random.randint(100, size = 50)
np.histogram(arr, bins = [0,10,20,30,40,50,60,70,80,90,100])``````
Out:
``````(array([ 7, 12,  4,  8,  3,  1,  2,  4,  4,  5]),
array([  0,  10,  20,  30,  40,  50,  60,  70,  80,  90, 100]))``````

The example defines arr as a series with numbers bw 1 and 10 at 50 data set points. The frquency of occurence of each of those numbers is given by the np.histogram function. The function is extremely helpful in creating histograms to get an idea about the data

In :
``````# Example 3 - breaking (to illustrate when it breaks)
arr = np.random.randint(100, size = 50)
np.histogram(arr, bin = [0,10,20,30,40,50,60,70,80,90,100])``````
```--------------------------------------------------------------------------- TypeError Traceback (most recent call last) <ipython-input-23-5b080845ad2c> in <module> 1 # Example 3 - breaking (to illustrate when it breaks) 2 arr = np.random.randint(100, size = 50) ----> 3 np.histogram(arr, bin = [0,10,20,30,40,50,60,70,80,90,100]) <__array_function__ internals> in histogram(*args, **kwargs) TypeError: _histogram_dispatcher() got an unexpected keyword argument 'bin'```

the parameters in the histrogram function should be written carefully as the keyword to be used is bins not bin

the function should be used when freuqency of occurence is to be calculated , moreso in a graphical manner

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

### Function 2 - np.corrcoef

Return Pearson product-moment correlation coefficients in the form of a matrix.

In :
``````# Example 1 - calcaulating the co relation between height in inches and weight in kgs
height = np.array([72,75,76,78,82,73,85])
weight = np.array([68,69,69,73,75,65,78])
np.corrcoef(height,weight)
``````
Out:
``````array([[1.       , 0.9569077],
[0.9569077, 1.       ]])``````

The matrix gives the correlations between height and weight as matrix . It shows that height and weight are co related that as height increases so does weight in almost a linear fashion.

In :
``````# Example 1 - calcaulating the co relation between  weight in kgs and number of hours excercised
weight = np.array([68,69,69,73,75,65,78])
hours = np.array([3,2,1,0,0,1,0])
np.corrcoef(weight, hours)
``````
Out:
``````array([[ 1.        , -0.67219362],
[-0.67219362,  1.        ]])``````

The matrix gives the correlations between height and weight as matrix . It shows that height and number of hours excercised are co related lightly .

In :
``````weight = np.array([68,69,69,73,75,65,78])
hours = np.array([3,2,1,0,0,1])
np.corrcoef(weight, hours)
``````
```--------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-31-f8d8c518cd10> in <module> 1 weight = np.array([68,69,69,73,75,65,78]) 2 hours = np.array([3,2,1,0,0,1]) ----> 3 np.corrcoef(weight, hours) <__array_function__ internals> in corrcoef(*args, **kwargs) /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/lib/function_base.py in corrcoef(x, y, rowvar, bias, ddof) 2549 warnings.warn('bias and ddof have no effect and are deprecated', 2550 DeprecationWarning, stacklevel=3) -> 2551 c = cov(x, y, rowvar) 2552 try: 2553 d = diag(c) <__array_function__ internals> in cov(*args, **kwargs) /srv/conda/envs/notebook/lib/python3.8/site-packages/numpy/lib/function_base.py in cov(m, y, rowvar, bias, ddof, fweights, aweights) 2413 if not rowvar and y.shape != 1: 2414 y = y.T -> 2415 X = np.concatenate((X, y), axis=0) 2416 2417 if ddof is None: <__array_function__ internals> in concatenate(*args, **kwargs) ValueError: all the input array dimensions for the concatenation axis must match exactly, but along dimension 1, the array at index 0 has size 7 and the array at index 1 has size 6```

The example breaks as array size should be the same . care should be taken to make sure array sizes are the same.

The function is to be used when trying to establish relationships between datasets. This gives a geneeral direction on what to expect from the data .

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

### Function 3 - count_nonzero

Counts the number of non-zero values in the array a.

In :
``````# Example 1 - working
arr1 = np.random.randint(5 , size = (4,4))
arr1
``````
Out:
``````array([[1, 1, 3, 2],
[2, 2, 0, 0],
[0, 2, 2, 2],
[0, 2, 4, 4]])``````
In :
``np.count_nonzero(arr1)``
Out:
``12``

np.count_nonzero(arr1) gives the number of non zero elements in a series which in this matrix is 4

In :
``````height = np.array([72,75,76,78,82,73,85])
weight = np.array([68,69,69,73,75,65,78])
len(height),np.count_nonzero(height),len(weight),np.count_nonzero(weight)

``````
Out:
``(7, 7, 7, 7)``
In :
``np.corrcoef(height,weight)``
Out:
``````array([[1.       , 0.9569077],
[0.9569077, 1.       ]])``````

For example in ssome surveys NaN or questions people choose not to answer are erroneously coded as 0's this leads to bias in the data set and throws off the calculations. here we check before finding the co relation coef whther both the data contains all no zero elements as neither height or weight can be zero

In :
``````# Example 3 - breaking (to illustrate when it breaks)
height = np.array([72,75,76,78,82,73,85])
weight = np.array([68,69,69,73,0,65,0])
np.corrcoef(height,weight)
``````
Out:
``````array([[ 1.        , -0.86761515],
[-0.86761515,  1.        ]])``````

No count_nozero was used to weed out the 0's and the co relation coef dropped as compared with the previous example. Hence it is necessary to weed out the non zeros from data that are not expected to be zero like height weight or age

This function should be used in exploratory data analysis to sanitise the data so that is necessary to weed out the non zeros from data that are not expected to be zero like height weight or age

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

### Function 3 - ???

In [ ]:
``````# Example 1 - working
???
``````

In [ ]:
``````# Example 2 - working
???
``````

In [ ]:
``````# Example 3 - breaking (to illustrate when it breaks)
???
``````

Explanation about example (why it breaks and how to fix it)

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

### Function 4 - ???

In [ ]:
``````# Example 1 - working
???
``````

In [ ]:
``````# Example 2 - working
???
``````

In [ ]:
``````# Example 3 - breaking (to illustrate when it breaks)
???
``````

Explanation about example (why it breaks and how to fix it)

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

### Function 5 - ???

In [ ]:
``````# Example 1 - working
???
``````

In [ ]:
``````# Example 2 - working
???
``````

In [ ]:
``````# Example 3 - breaking (to illustrate when it breaks)
???
``````

Explanation about example (why it breaks and how to fix it)

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

### Conclusion

Summarize what was covered in this notebook, and where to go next

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