Practice
Resources
Contests
Online IDE
New
Free Mock
Scaler
Practice
Improve your coding skills with our resources
Contests
Compete in popular contests with top coders
Scaler
Explore Offerings by SCALER

Go to Problems

# Numpy

Â In Python, we are provided lists whose functionalities are almost similar to arrays (different in some aspects also like they are slower)..

Numpy array is almost 50 times faster than the python lists.

Numpy is faster because it is implemented in c and its objects consist of similar data types, and its tasks are divided and performed parallelly.

The array object in NumPy is called ndarray, and the functions provided in the library for this object make it quite easy to perform various operations more efficiently than python lists.

Arrays can be considered a primitive data structure for storing information and it comes in very handy when complex operations are required to perform on a large number of elements.

For using numpy, create an alias with the as a keyword while importing:

• We can use array() function to create an ndarray object.
`import numpy as nparr = np.array([1, 2, 3, 4, 5])`
• For creating a 2d array we can follow the following code:
`arr = np.array([[1, 2, 3], [4, 5, 6]])Â `
• And 3d with the following one:Â
`arr = np.array([[[1, 2, 3], [4, 5, 6]], [[1, 2, 3], [4, 5, 6]]])`
• And to create an array using ndim as in the following code:
`arr = np.array([1, 2, 3, 4], ndmin=5) #where ndmin will be defining the number of dimensions.`

Â

• We can use a function `numpy.asarray()` which takes a list,list of tuples,tuples, other python sequences as input and converts them into ndarray. For eg.
`x = [1,2,3]Â a = np.asarray(x)`

Â

• `np.arange()` function returns an ndarray object containing evenly spaced values within a given range.
`numpy.arange(start, stop, step, dtype)a=np.arange(1,10,2)Â  # [1 3 5 7 9]`

Â

Few of the Operations on NumPy arrays are illustrated below

One can easily perform various complex mathematical operations on the numpy objects through its inbuilt functions because of vectorization. Some of them are:

1. PowerÂ

`arr = np.array([1,2,3,4,5])Â # for finding an array raised to some powerprint(np.power(arr, 3))Â  #Â  [Â  1 Â  8Â  27Â  64 125]arr1 = np.array([1,2,3,4,5])Â print(np.power(arr, arr1)) #Â  [ Â  1Â  Â  4 Â  27Â  256 3125]`

Â

`# for adding subtractingprint(np.add(arr,[5,4,3,2,1])) #Â  [6 6 6 6 6]print(np.subtract(arr,[5,4,3,2,1]))Â  #Â  [-4 -2Â  0Â  2Â  4]`

Â

3. MultiplicationÂ

`#for multiplyingprint(3*a)Â  #Â  [ 3Â  6Â  9 12 15]`

Â

4. Dot productÂ

`#dot productprint(np.dot(arr,arr1))Â  # 55print(np.dot([[1,2],[3,4]],[5,6]))Â  #Â  [17 39]`

Â

5. Slicing

`#slicingarr = np.array([[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]])print(arr[:, 1:4])Â Â #[[2 3 4]Â [7 8 9]]`

Â

6. Cross product

`#cross productprint(np.cross([1,2,3],[4,5,6]))Â  # [-3Â  6 -3]`

Â

It refers to the manner how Numpy manages arrays with different dimensions during arithmetic operations. If certain constraints are followed then the array with smaller dimensions is â€˜broadcastedâ€™ across the array with larger dimensions, For eg. As shown in the below example when a numpy array of shape (5,) is added to an array of shape (6,1) the resultant we got is of shape of (6,5), the array of shape(5,) is broadcasted to shape (1,5)

`import numpy as npx = np.arange(6)x2 = x.reshape(6,1)y = np.ones(5)y2 = np.ones((3,4))print(x+2)# simplest broadcasting when a scalar is added to a vector# [2 3 4 5 6 7]print(y.shape)# (5,)print(x2 + y)#Â  [[1. 1. 1. 1. 1.]Â [2. 2. 2. 2. 2.]Â [3. 3. 3. 3. 3.]Â [4. 4. 4. 4. 4.]Â [5. 5. 5. 5. 5.]Â [6. 6. 6. 6. 6.]]`

`#print(x2 + y2) -> it will give error as the constraints for broadcasting are not followed.`

Â

Using NumPy, a developer can perform the following operations âˆ’

• Mathematical and logical operations on arrays.
• Used for implementing multi-dimensional arrays and matrices.Â
• Library of high-level mathematical functions to operate on these matrices and arrays.
• Fourier transforms and routines for shape manipulation.
• Operations related to linear algebra. NumPy has in-built functions for linear algebra and random number generation.
• Designed for scientific computation

## Serious about Learning Data Science and Machine Learning ?

Learn this and a lot more with Scaler's Data Science industry vetted curriculum.
Vector analysis (numpy)
Problem Score Companies Time Status
find the one 30
1:58
choose the output 30
3:37
3:51
Duplicates detection 50
34:42
Row-wise unique 50
36:03
Data handling (pandas)
Problem Score Companies Time Status
For 'series' 30
3:38
drop axis 30
1:12
Rename axis 30
1:31
iloc vs loc part I 30
1:00
As a Series 50
22:45
48:50
Basic computer vision (opencv)
Problem Score Companies Time Status
Which library it is? 30
0:46
Image dimensions 30
1:12
Dimension with components 30
0:51
Color interpretation 30
1:15
Image cropping 30
1:24
Data visualization (matplotlib)
Problem Score Companies Time Status
2d graphics 30
0:33
Suitable plot type 30
1:11
Subplot Coordinates 30
3:03
Vertically Stacked Bar Graph 30
2:02
1:11
Web scraping basics
Problem Score Companies Time Status
What does the code do? 30
2:20
Retrieval protocol 30
0:37
2-way communication 30
0:45
Search engine process 30
1:19
What does the code print? 30
1:05
Eda
Problem Score Companies Time Status
PCA's secondary objective 30
1:09
Five number theory 30
1:02
Free Mock Assessment
Fill up the details for personalised experience.
All fields are mandatory
Current Employer *
Enter company name *
Select an option *
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
Phone Number *
OTP will be sent to this number for verification
+1 *
+1
Change Number
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
*Enter the expected year of graduation if you're student
Current Employer *
Company Name *