Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Read by thought-leaders and decision-makers around the world. Phone Number: +1-650-246-9381 Email: [email protected]
228 Park Avenue South New York, NY 10003 United States
Website: Publisher: https://towardsai.net/#publisher Diversity Policy: https://towardsai.net/about Ethics Policy: https://towardsai.net/about Masthead: https://towardsai.net/about
Name: Towards AI Legal Name: Towards AI, Inc. Description: Towards AI is the world's leading artificial intelligence (AI) and technology publication. Founders: Roberto Iriondo, , Job Title: Co-founder and Advisor Works for: Towards AI, Inc. Follow Roberto: X, LinkedIn, GitHub, Google Scholar, Towards AI Profile, Medium, ML@CMU, FreeCodeCamp, Crunchbase, Bloomberg, Roberto Iriondo, Generative AI Lab, Generative AI Lab Denis Piffaretti, Job Title: Co-founder Works for: Towards AI, Inc. Louie Peters, Job Title: Co-founder Works for: Towards AI, Inc. Louis-François Bouchard, Job Title: Co-founder Works for: Towards AI, Inc. Cover:
Towards AI Cover
Logo:
Towards AI Logo
Areas Served: Worldwide Alternate Name: Towards AI, Inc. Alternate Name: Towards AI Co. Alternate Name: towards ai Alternate Name: towardsai Alternate Name: towards.ai Alternate Name: tai Alternate Name: toward ai Alternate Name: toward.ai Alternate Name: Towards AI, Inc. Alternate Name: towardsai.net Alternate Name: pub.towardsai.net
5 stars – based on 497 reviews

Frequently Used, Contextual References

TODO: Remember to copy unique IDs whenever it needs used. i.e., URL: 304b2e42315e

Resources

Take our 85+ lesson From Beginner to Advanced LLM Developer Certification: From choosing a project to deploying a working product this is the most comprehensive and practical LLM course out there!

Publication

Easiest Numpy Guide For Beginners Part-2
Latest   Machine Learning

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 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()

  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 Value
94 #Max Value
1 #Index of Min Value
8 #Index of Max Value

3. Read and Write a File using Numpy

  1. 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

  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 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 dateInput:
oneday = np.timedelta64(1,'D')
print(oneday)
print(oneday+d)
Output:
1 days
2020-12-02T23:34:56
Adding minute into timeInput:
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.

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

Feedback ↓