- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
To work with SciPy, do I need to import the NumPy functions explicitly?
When SciPy is imported, you do not need to explicitly import the NumPy functions because by default all the NumPy functions are available through SciPy namespace. But as SciPy is built upon the NumPy arrays, we must need to know the basics of NumPy.
As most parts of linear algebra deals with vectors and matrices only, let us understand the basic functionalities of NumPy vectors and matrices.
Creating NumPy vectors by converting Python array-like objects
Let us understand this with the help of following example−
import numpy as np list_objects = [10,20,30,40,50,60,70,80,90] array_new = np.array(list_objects) print (array_new)
[10 20 30 40 50 60 70 80 90]
NumPy array creation
NumPy has various functions to create arrays from scratch. Let us understand them along with examples −
This function will create an array filled with 0 values with the specified shape.
import numpy as np print (np.zeros((3, 4)))
[[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]]
This function will create an array filled with 1 values with the specified shape.
import numpy as np print (np.ones((3, 4)))
[[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]]
This function will create an array with regularly incrementing values.
import numpy as np print (np.arange(15))
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14]
This function will create arrays spaced equally between the specified start and end values.
import numpy as np print (np.linspace(1.0, 3.0, num=5))
[1. 1.5 2. 2.5 3. ]
Creating matrices and applying some functions over matrices using NumPy
A matrix is a specialized two-dimensional (2D) array. Matrices retains its nature through all the operations applied over it.
Creating a matrix
Let us understand this with the help of following example −
import numpy as np matrix = np.matrix('10 20; 30 40') print(matrix)
[[10 20] [30 40]]
Transpose a Matrix
This function will return the transpose of a matrix. The below example will transpose of the above created matrix −
matrix([[10, 30], [20, 40]])
Multiplication of two Matrices
We can use the dot() function of NumPy for doing multiplications of two matrices. Let us understand this with the help of following example −
import numpy as np matrix1 = np.array([[1,6,9],[3,4,10]]) matrix2 = np.array([[2,5],[4,7],[7,8]]) matrix3 = np.dot(matrix1,matrix2) print(matrix3)
[[ 89 119] [ 92 123]]
You can learn in detail about NumPy at https://www.tutorialspoint.com/numpy/index.htm .
- Do I need to import the Java.lang package anytime during running a program?
- How do I install Python SciPy?
- Why do I need Babel JS?
- How do nested functions work in Python?
- How do I work with array fields in MongoDB to match all?
- When do I need to call the main loop in a Tkinter application?
- What do you need to join the metaverse?
- Why do we need a Valuation Approach in performing Finance Functions?
- What permission do I need to access Internet from an Android application?
- How do I import all the submodules of a Python namespace package?
- What is the difference between SciPy and NumPy?
- What do we need to do to join Indian Army?
- Do I need to set up SAP user for every user of module?
- Why do I lose focus when I try to work while putting on headphones?