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

Sign up to execute **numpy-array-operations** and 150,000+ data science projects. Build your own projects and share them online!

Updated a year ago

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 [2]:

`!pip install jovian --upgrade -q`

In [3]:

`import jovian`

In [4]:

`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
```

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

In [5]:

`import numpy as np`

In [9]:

```
# List of functions explained
function1 = np.histogram # (change this)
function2 = np.corrcoef
function3 = np.count_nonzero
function4 = np.ravel
function5 = np.nanstd
```

In [10]:

`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
```

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

In [14]:

```
arr = np.random.randint(11, size = 50)
np.histogram(arr)
```

Out[14]:

```
(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 [21]:

```
# 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[21]:

```
(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 [23]:

```
# 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 [24]:

`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
```

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

In [25]:

```
# 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[25]:

```
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 [30]:

```
# 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[30]:

```
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 [31]:

```
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[0] != 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 [32]:

`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
```

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

In [41]:

```
# Example 1 - working
arr1 = np.random.randint(5 , size = (4,4))
arr1
```

Out[41]:

```
array([[1, 1, 3, 2],
[2, 2, 0, 0],
[0, 2, 2, 2],
[0, 2, 4, 4]])
```

In [43]:

`np.count_nonzero(arr1)`

Out[43]:

`12`

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

In [46]:

```
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[46]:

`(7, 7, 7, 7)`

In [47]:

`np.corrcoef(height,weight)`

Out[47]:

```
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 [49]:

```
# 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[49]:

```
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 [25]:

`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
```

Add some explanations

In [ ]:

```
# Example 1 - working
???
```

Explanation about example

In [ ]:

```
# Example 2 - working
???
```

Explanation about example

In [ ]:

```
# Example 3 - breaking (to illustrate when it breaks)
???
```

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

Some closing comments about when to use this function.

In [26]:

`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
```

Add some explanations

In [ ]:

```
# Example 1 - working
???
```

Explanation about example

In [ ]:

```
# Example 2 - working
???
```

Explanation about example

In [ ]:

```
# Example 3 - breaking (to illustrate when it breaks)
???
```

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

Some closing comments about when to use this function.

In [27]:

`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
```

Add some explanations

In [ ]:

```
# Example 1 - working
???
```

Explanation about example

In [ ]:

```
# Example 2 - working
???
```

Explanation about example

In [ ]:

```
# Example 3 - breaking (to illustrate when it breaks)
???
```

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

Some closing comments about when to use this function.

In [28]:

`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
```

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

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

- Numpy official tutorial : https://numpy.org/doc/stable/user/quickstart.html
- ...

In [30]:

`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
```

In [ ]:

` `