Menu

Workshops

Projects

Blogs

Careers

Hire from us


For Business / Universities

Corporate Training

Academic Up-skilling


All Courses

Choose a category

Loading...

All Courses / undefined

All Courses / undefined / undefined

logo

Loading...

FOR BUSINESSES

Corporate Upskilling

FOR Universities

Academic Training

More

CSE

Uploaded on

23 Jul 2022

The NUMPY Cheat Sheet You Need To Know

logo

Skill-Lync

The Python package NumPy is used to manipulate arrays. Additionally, it has functions for matrices, fourier transform, and working in the area of linear algebra. In the year 2005, Travis Oliphant developed NumPy. You can use it for free because it is an open-source project. Numerical Python is referred to as NumPy for short.

 

Why do you need a Numpy Cheat Sheet

 

Whether you are a professional who has been using Python for a while or a novice who has only recently begun using it, you must be familiar with NumPy, a Python library for numerical operations. Despite the fact that it is widely used, wouldn't you agree that it is almost impossible to memorise all of the commands and operations? Sometimes it's simply necessary to use the internet to research even the most elementary topics. There are no judgments here; we've all done it, so don't worry. This cheat sheet will provide you with all the fundamentals you need to get started using NumPy in Python and has been written assuming that one has a basic understanding of Python.

 

Below are the commands to conduct different actions.

These include:

** np alias numpy**

 

Steps to Import/Export

 

np.loadtxt('file.txt') | From a text file

np.genfromtxt('file.csv',delimiter=',') | From a CSV file np.savetxt('file.txt',array,delimiter=' ') | Writes to a text file np.savetxt('file.csv',array,delimiter=',') | Writes to a CSV file

 

Steps to Create Arrays

 

np.array([1,2,3]) | One dimensional array

np.array([(1,2,3),(4,5,6)]) | Two dimensional array

np.zeros(3) | 1D array of length 3 all values 0 

np.ones((3,4)) | 3x4 array with all values 1 

np.eye(5) | 5x5 array of 0 with 1 on diagonal (Identity matrix)

np.linspace(0,100,6) | Array of 6 evenly divided values from 0 to 100 np.arange(0,10,3) | Array of values from 0 to less than 10 with step 3 (eg [0,3,6,9]) np.full((2,3),8) | 2x3 array with all values 8 

np.random.rand(4,5) | 4x5 array of random floats between 0–1 np.random.rand(6,7)*100 | 6x7 array of random floats between 0–100 np.random.randint(5,size=(2,3)) | 2x3 array with random ints between 0–4

 

Steps to Inspect Properties  

array.size | Returns number of elements in array

array.shape | Returns dimensions of array (rows,columns)

array.dtype | Returns type of elements in array

array.astype(dtype) | Convert array elements to type

array.tolist() | Convert array to a Python list

np.info(np.eye) | View documentation for np.eye

 

** array refers to the name of the numpy array you have defined**

 

Steps to Copy/Sort/Reshape

 

np.copy(array) | Copies array to new memory

array.view(dtype) | Creates view of array elements with type

array.sort() | Sorts array

array.sort(axis=0) | Sorts specific axis of array 

two_d_array.flatten() | Flattens 2D array two_d_array to 1D

array.T | Transposes array (rows become columns and vice versa)

array.reshape(3,4) | Reshapes array to 3 rows, 4 columns without changing data array.resize((5,6)) | Changes array shape to 5x6 and fills new values with 0

 

Steps for Element Additions and Removal

 

np.append(array,values) | Appends values to end of array 

np.insert(array,2,values) | Inserts values into array before index 2 

np.delete(array,3,axis=0) | Deletes row on index 3 of array 

np.delete(array,4,axis=1) | Deletes column on index 4 of array

 

Steps for Joining and Splitting an Array

 

np.concatenate((array1,array2),axis=0) | Adds array2 as rows to the end of array1 np.concatenate((array1,array2),axis=1) | Adds array2 as columns to end of array1 np.split(array,3) | Splits array into 3 sub-array

np.hsplit(array,5) | Splits array horizontally on the 5th index

 

Steps for Indexing/Slicing/Subsetting  

 

array[5] | Returns the element at index 5 

array[2,5] | Returns the 2D array element on index [2][5] 

array[n]=4 | Assigns array element on index n the value 4 

array[1,5]=10 | Assigns array element on index [1][5] the value 10 

array[0:3] | Returns the elements at indices 0,1,2 (On a 2D array: returns rows 0,1,2)

array[0:3,4] | Returns the elements on rows 0,1,2 at column 4 

array[:2] | Returns the elements at indices 0,1 (On a 2D array: returns rows 0,1) array[:,n] | Returns the elements at index n on all rows

array<5 | Returns an array with boolean values

 (array1<3) & (array2>5) | Returns an array with boolean values

 ~array | Inverts a boolean array

array[array<5] | Returns array elements smaller than 5

 

Steps for Scalar Operations

 

np.add(array,1) | Add 1 to each array element

np.subtract(array,2) | Subtract 2 from each array element

np.multiply(array,3) | Multiply each array element by 3 

np.divide(array,4) | Divide each array element by 4 (returns np.nan for division by zero)

np.power(array,5) | Raise each array element to the 5th power

 

Arrays and Vector Algebra

 

np.add(array1,array2) | Elementwise add array2 to array1 

np.subtract(array1,array2) | Elementwise subtract array2 from array1 np.multiply(array1,array2) | Elementwise multiply array1 by array2 

np.divide(array1,array2) | Elementwise divide array1 by array2 

np.power(array1,array2) | Elementwise raise array1 raised to the power of array2 np.array_equal(array1,array2) | Returns True if the arrays have the same elements and shape

np.sqrt(array) | Square root of each element in the array

np.sin(array) | Sine of each element in the array

np.log(array) | Natural log of each element in the array

np.abs(array) | Absolute value of each element in the array

np.ceil(array) | Rounds up to the nearest int

np.floor(array) | Rounds down to the nearest int

np.round(array) | Rounds to the nearest int

 

Statistical Operations that can be performed on an Array 

 

np.mean(array,axis=0) | Returns mean along specific axis

array.sum() | Returns sum of array 

array.min() | Returns minimum value of array 

array.max(axis=0) | Returns maximum value of specific axis

np.var(array) | Returns the variance of array

np.std(array,axis=1) | Returns the standard deviation of specific axis

array.corrcoef() | Returns correlation coefficient of array

Keep the Numpy cheat sheet handy and use it for all your Python-related needs.

 

 


Author

author

Navin Baskar


Author

blogdetails

Skill-Lync

img

Continue Reading

Related Blogs

Christmas Time is Near, Time for Joy and Time for Cheer.

Premium Master’s Program can do so at a discount of 20%. But, Christmas is time for sharing, therefore if you and your friend were to join any Skill-Lync Master’s Program together, both of you will get a discount of 30% on the course fee of your Premium Master’s Program

CSE

24 Dec 2021


Career Prospects For Software Engineers

Increase your career opportunities by becoming a software engineer and make the world a better place. Enroll in upskilling courses and practice the skills you learn.

CSE

27 Dec 2021


Suggested Career Path For A Software Developer

Software development is rated as the best job in the industry. Individuals with the right software development skills, good communication, and an open mind to adapt, learn, and evolve can find success in the field.

CSE

28 Dec 2021


7 best Java Project Ideas To Showcase Your Programming Skills

If you aspire for a career in the software development space, upskilling yourself with the knowledge and practical application of programming languages is mandatory.

CSE

29 Dec 2021


Why choose a career in computer science?

The most fascinating thing about the chosen ways of completing tasks on computers is that we only choose them because we do not have a simpler way yet.

CSE

30 Dec 2021



Author

blogdetails

Skill-Lync

img

Continue Reading

Related Blogs

Christmas Time is Near, Time for Joy and Time for Cheer.

Premium Master’s Program can do so at a discount of 20%. But, Christmas is time for sharing, therefore if you and your friend were to join any Skill-Lync Master’s Program together, both of you will get a discount of 30% on the course fee of your Premium Master’s Program

CSE

24 Dec 2021


Career Prospects For Software Engineers

Increase your career opportunities by becoming a software engineer and make the world a better place. Enroll in upskilling courses and practice the skills you learn.

CSE

27 Dec 2021


Suggested Career Path For A Software Developer

Software development is rated as the best job in the industry. Individuals with the right software development skills, good communication, and an open mind to adapt, learn, and evolve can find success in the field.

CSE

28 Dec 2021


7 best Java Project Ideas To Showcase Your Programming Skills

If you aspire for a career in the software development space, upskilling yourself with the knowledge and practical application of programming languages is mandatory.

CSE

29 Dec 2021


Why choose a career in computer science?

The most fascinating thing about the chosen ways of completing tasks on computers is that we only choose them because we do not have a simpler way yet.

CSE

30 Dec 2021


Related Courses

https://d28ljev2bhqcfz.cloudfront.net/maincourse/thumb/masters-program-data-science-machine-learning_1644325039.jpg
Post Graduate Program in Data Science and Machine Learning
4.7
137 Hours of content
Data science Domain
Know more
Showing 1 of 4 courses
Try our top engineering courses, projects & workshops today!Book a FREE Demo