NumPy is a library for the Python programming language, adding support for large, multi-dimensional arrays and matrices, along with a large collection of high-level mathematical functions to operate on these arrays.
arr = np.arange(10)
print(arr)
import numpy as np
arr = np.arange(100000)
python_list = list(range(100000))
If you have any query, don't forget to comment.
![]() |
Basic Numpy Array in Python | Jupyter Tutorial Part 1 |
What is numpy?
- Numpy (short for 'Numerical Python'): is the fundamental (but not the only) package required for high-performance scientific computing and data analysis
- NumPy introduces and makes extensive use of the ndarray (nth-dimensional array) type, which is a homogeneous multidimensional array: it is a table of elements (usually numbers), all of the same type, indexed by a tuple of positive integers.
- It offers standard mathematical functions for fast operations on entire arrays of data without having to write loops ('array-oriented computing')
- An array object can be of any dimension but most usually we deal with one-, two- or three- dimensional arrays
Installing Numpy
Step 1. Open your Windows PowerShell.
Step 2. Type pip install numpy
Step 3. Numpy will install in your computer
Using Numpy in Jupyter Notebook
**Note: For using Numpy in your python. user this keyword --> import numpy as np. That means the total Numpy module is imported. now, you can use any functions of the Numpy module
Numpy coding on Jupyter:
import numpy as np
arr = np.arange(10)
print(arr)
output : [0,1,2,3,4,5,6,7,8,9]
In this program:
Line 1. I import the numpy module, by using this syntax --> import numpy as np. There np treated as the nick name of numpy. Because, when I call numpy, I don't use the numpy.funtion(). I used just np.funtion(). We use np for save time, but the Python developer community made np universal. So, when you copy code some were like StackOverflow you saw the programmers write code on np. means import numpy as np.
Line 2. arr is a variable where the numpy array is stored. Then np.arange(10) means, for used any numpy functions we need to write numpy.funtion(). Here we use np as nick name of numpy, so we use np.funtion(). The function arange(10) means, the array start from 0 to 9, Total 10 elements. Means, the range is 0 to 9 because of array start from 0.
Line 3. I print the value of arr variable and the output is [0,1,2,3,4,5,6,7,8,9]
Basic Numpy array |
**Note: Numpy array is faster than Python array, and Numpy array also takes less space then Python list. Numpy module is written in C language. C is a middle-level language so it can easily communicate with hardware. That's why Numpy array is fast.
To check that Numpy array is faster than Python list. Write the code in the Jupyter Notebook's cell.
arr = np.arange(100000)
python_list = list(range(100000))
%time for _ in range(10): [item*3 for item in python_list]
Output: Wall time: 95 ms
%time for _ in range(10): arr = arr*3
Output: Wall time: 2 ms
** Here %time is a magic function, which gives us the runtime of the program
Numpy array is faster than Python List |
If you have any query, don't forget to comment.
0 comments:
Post a Comment