Master LLMs with our FREE course in collaboration with Activeloop & Intel Disruptor Initiative. Join now!

# Easiest Numpy Guide For Beginners Part-2

Last Updated on July 21, 2023 by Editorial Team

#### Author(s): Prajakta Mogare

Originally published on Towards AI.

## Programming

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.

## Easiest Numpy Guide For Beginners Part-1

### What is Numpy?

medium.com

1. 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 nparr=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()

1. min() -Finds the minimum value in Numpy array.
2. max()-Finds the maximum value in Numpy array.
3. argmin()-Finds the index of minimum value in Numpy array.
4. 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 Value94 #Max Value1 #Index of Min Value8 #Index of Max Value`

3. Read and Write a File using Numpy

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 nparr=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.])`

.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

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 .npynp.save(‘data2.npy’,arr_2) #creation of .npynp.savez(‘both.npz’,arr_1,arr_2) #creation of .npzd=np.load(‘both.npz’)print(d.files) #Arrays in zip folderprint(d[d.files[0]]) #First Arrayprint(d[d.files[1]]) #Second ArrayOutput:['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

1. 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 npd = np.datetime64(‘2020–12–01 23:34:56’)print(d)Output:2020-12-01T23:34:56Input:print(d+1) #Extra second gets addedOutput:2020-12-01T23:34:57Adding a day into the dateInput:oneday = np.timedelta64(1,'D')print(oneday)print(oneday+d)Output:1 days2020-12-02T23:34:56Adding minute into timeInput:tenminutes = np.timedelta64(10,'m')print(tenminutes)print(tenminutes+d)Output:10 minutes2020-12-01T23:44:56`

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.

Join thousands of data leaders on the AI newsletter. Join over 80,000 subscribers and keep up to date with the latest developments in AI. From research to projects and ideas. If you are building an AI startup, an AI-related product, or a service, we invite you to consider becoming a sponsor.

Published via Towards AI