Easiest Numpy Guide For Beginners Part-2
By: Prajakta Mogare
Tags: Numpy, Data Science, Deep Learning, Beginners Guide, Python

This part is the continuation of the part-1 of my blog. I appeal to you to please go through part-1 for a better understanding of this blog.

Where function in Numpy Array

Where function in NumPy array is used for finding a group of values that satisfy the mentioned condition. This method returns the index of the values and not the values themselves.

Input:

import numpy as np

arr=np.array([8,94,8,56,7,3,2,1,0])

print(np.where(arr>10)) Output: #Returns the index not values

(array([1, 3], dtype=int64),) Input:

val1= np.where(arr>10)

print(arr[val1]) Output: #After passing index to array we can get values

[94 56]

2. min(),max(),argmin(),argmax()

min() -Finds the minimum value in Numpy array. max()-Finds the maximum value in Numpy array. argmin()-Finds the index of minimum value in Numpy array. argmax()-Finds the index of maximum value in Numpy array.

Input:

print(arr.min())

print(arr.max())

print(arr.argmax())

print(arr.argmin()) Output:

0 #Min Value

94 #Max Value

1 #Index of Min Value

8 #Index of Max Value

3. Read and Write a File using Numpy

savetxt() and loadtxt()

savetxt()- Used for writing some text content into a file. We can use the savetxt() for the creation and writing of text files or CSV files.

CSV file is a file that has comma-separated values in it. This type of file can be made by using the delimiter keyword in savetxt().

Input:

import numpy as np

arr=np.array([8,94,8,56,7,3,2,1,0])

np.savetxt('arr_file.csv',arr,delimiter=',') Output:

Input:

np.loadtxt('arr_file.csv',delimiter=',') Output:

array([ 8., 94., 8., 56., 7., 3., 2., 1., 0.])

2. save(),load(),savez()

.npy - It is a binary file in NumPy array which can hold only a single array in it.

.npz -It works like a zip file that contains the multiple numbers of .npy files in it.

save()- Saves file with .npy extension

load()- Loads file with .npy and .npz extension

savez()- Saves file with .npz extension

Input:

arr_1=np.array([8,94,8,56,7,3,2,1,0])

arr_2=np.array([2,3,4,5,6,7,8,9])

np.save(‘data1.npy’,arr_1) #creation of .npy

np.save(‘data2.npy’,arr_2) #creation of .npy

np.savez(‘both.npz’,arr_1,arr_2) #creation of .npz

d=np.load(‘both.npz’)

print(d.files) #Arrays in zip folder

print(d[d.files[0]]) #First Array

print(d[d.files[1]]) #Second Array Output:

['arr_0', 'arr_1']

[ 8 94 8 56 7 3 2 1 0]

[2 3 4 5 6 7 8 9]

4. Concatenation and Sorting in Numpy Array

Concatenate method

axis=0 -Concatenation row-wise

axis=1 -Concatenation column-wise

Input:

arr_3=np.array([[1,2],[5,6],[7,8]])

np.concatenate([arr_3,arr_3],axis=0) Output:

array([[1, 2],

[5, 6],

[7, 8],

[1, 2],

[5, 6],

[7, 8]]) Input:

np.concatenate([arr_3,arr_3],axis=1) Output:

array([[1, 2, 1, 2],

[5, 6, 5, 6],

[7, 8, 7, 8]]) For concatenation the dimensions of two arrays should be exactly the same otherwise it will give valueError. Input:

np.concatenate([arr_1,arr_3],axis=0) Output: ValueError Traceback (most recent call last)

<ipython-input-17-c9ceb2b572e7> in <module>

----> 1 np.concatenate([arr_1,arr_3],axis=0)



<__array_function__ internals> in concatenate(*args, **kwargs)



ValueError: all the input arrays must have same number of dimensions, but the array at index 0 has 1 dimension(s) and the array at index 1 has 2 dimension(s)

2. vstack() and hstack() methods

vstack() — Stacks the elements vertically.

hstack() — Stacks the elements horizontally.

Input:

np.vstack([arr_3,arr_3]) Output:

array([[1, 2],

[5, 6],

[7, 8],

[1, 2],

[5, 6],

[7, 8]]) Input:

np.hstack([arr_3,arr_3]) Output:

array([[1, 2, 1, 2],

[5, 6, 5, 6],

[7, 8, 7, 8]])

Sorting-

Sorting is by default done row-wise

Input:

npa=np.hstack([arr_3,arr_3])

print(npa)

npa.sort()

print(npa) Output:

[[1 2 1 2] #Before sorting

[5 6 5 6]

[7 8 7 8]] [[1 1 2 2] #Sorted Rowwise

[5 5 6 6]

[7 7 8 8]]

5. Date and time in Numpy

Creation of a particular date and time

Input:

import numpy as np

d = np.datetime64(‘2020–12–01 23:34:56’)

print(d) Output:

2020-12-01T23:34:56 Input:

print(d+1) #Extra second gets added Output:

2020-12-01T23:34:57 Adding a day into the date Input:

oneday = np.timedelta64(1,'D')

print(oneday)

print(oneday+d) Output:

1 days

2020-12-02T23:34:56 Adding minute into time Input:

tenminutes = np.timedelta64(10,'m')

print(tenminutes)

print(tenminutes+d) Output:

10 minutes

2020-12-01T23:44:56

6. Broadcasting in Numpy

Many of the times when we are performing arithmetic operations we face a problem with dimensionality but with the special property of NumPy, it is possible to have an automatic solution to this problem.

In broadcasting what it does is expands the array of smaller dimensions into the similar dimension of a larger array. So that now it can perform arithmetic operations easily as both arrays have a similar size.

For broadcasting the array there is one condition that we need to follow.

At least one of the dimensions of the smaller array should be the same as the larger array.

The array b expands itself for getting a size similar to array a.

Input:

a = np.array([[10.0,10.0,10.0],[20.0,20.0,20.0],[20.0,20.0,20.0],[40.0,40.0,40.0]])

b = np.array([1.0,2.0,3.0])

print(a)

print(b)

print(a+b) Output:

[[10. 10. 10.]

[20. 20. 20.]

[20. 20. 20.]

[40. 40. 40.]] [1. 2. 3.] [[11. 12. 13.] #10+1,10+2,10+3

[21. 22. 23.] #20+1,20+2,20+3

[21. 22. 23.] #20+1,20+2,20+3

[41. 42. 43.]] #40+1,40+2,40+3

If you really like this blog please give it a clap and Follow.

Thank You.