Home
Posts
Article
Encyclopedia
Garden
Set
English

Introduction to NumPy for Numerical Computation

养花风水
19 Views
When talking about Python programming, one of the most essential libraries for numerical computation is NumPy. Understandably, NumPy is an abbreviation for Numerical Python. It is an important library that is widely used in mathematics and business computing. It is more efficient when it comes to storing and manipulating large quantities of data. In case you work with arrays, matrices, linear algebra, statistical computations, or Fourier-transforms, NumPy is definitely the application that you will turn to.

What is more, thanks to NumPy, numerical calculations in Python can be done much quicker and with better use of memory than what is done using native Python lists. It allows the programming of complex equations without an unreasonable amount of pain to implement the equation manually.

What is NumPy?

The NumPy® stands for Numerical Python that can support fruition of an open-source library that allows the development of multi dimensional arrays along with matrices. But the most interesting fact lies in the fact that a vast range of operations can be performed using mathematical functions on the made arrays. Furthermore, it is interesting to note that, while Python's normal lists can be described as multi-purpose data containing a variety of types, a NumPy array is a single type array. This feature gives NumPy an edge on enhanced performance and reduced memory usage, this now makes it possible for scientific computation and statistical to assist in complex calculations and better prediction models.

Arrays are the core object of NumPy which provides the base and foundation of the library; A good comparison of this is to use Python and its list type but now imagine a list that is much larger where stored lists can perform high-speed operations. Data manipulation also became easier, whereas before, constructs and slices were not allowed for ordinary lists. Every NumPy array can be indexed, sliced and each section can be reshaped with ease.

Creating NumPy Arrays

In order to use NumPy for the first time, it is necessary to prepend the library. After that, there are different modes in which you can create arrays. One of the most common ones is transforming any given list into any given NumPy array with the help of the np.array() function call.

Let's see how to use NumPy by importing it and creating a basic array.

  import numpy as np

arr = np.array([1, 2, 3, 4, 5])

print(arr)
This initializes a one dimensional NumPy array with elements ranging from 1 to 5.

Creating arrays with specific values is also possible in NumPy using their many built in functions. For instance, there are configurations such as: an array of zeros, an array of ones or even an array of a certain range of numbers.

  zeros_array = np.zeros((3, 4))   3x4 Array with zeroes

ones_array = np.ones((2, 2)) 2x2 Array with ones

range_array = np.arange(0, 10, 2) Output: [0 2 4 6 8]
These custom functions are helpful to easily create arrays containing required specific numbers without going through the trouble of inputting every number.

Array Operations

One of the reasons why NumPy has a wide usability is the fact that it allows for vectorized operations. Nowadays, most computers have a large amount of memory which allows for vectorization. In layman's terms, it is when you are able to operate on whole arrays, without needing to iterate through every element.

You can, for example, carry out arithmetic directly with NumPy arrays as such:

  arr1=np.array([1,2,3])

arr2=np.array([4,5,6])

sum_array = arr1 + arr2 Array adding the two arrays element wise

print(sum_array)
Also, NumPy accepts addition and some other operations, e.g., multiplication and division, to each element of an array without the use of loops explicitly. It, hence, increases the efficiency of a task that would have otherwise been executed in Python lists quite dramatically due to inefficiencies in the language.

  product_array = arr1 * arr2  Multiplication of two arrays

print(product_array)
Apart from simple arithmetic we also have their complex equivalents built as sine, cosine and other trigonometric functions aside from logarithms and exponentials, i.e.

  sin_array = np.sin(arr1)  Each element has a sine function applied to it

log_array = np.log(arr1) Apply the natural logarithm
These are highly optimized as they involve millions of such calculations which explains why the use of NumPy is recommended.

Multi-dimensional Arrays

The claim that Python is a one-dimensional language is without merit as one of its salient features is the existence of multi-dimensional arrays. Such arrays have found a useful application in the analytics of data, machine learning, and the computation of scientific calculations.

A 2D array can be considered to be a matrix which consists of rows and columns. You can create a 2D array as follows:

  arr2D = np.array([[1, 2, 3],[4, 5, 6]])

print(arr2D)
Using indexing and slicing, it is possible to reach various elements in a multi-dimensional array. One method to reach a particular element in the 2D array is to define the index of the row and the column. The example below illustrates this:

  element = arr2D[1, 2] Selecting the 2nd row, 3rd column element

print(element)
It is also possible to copy portions of an array, or subarrays through slicing, in the same way as is done with lists:

  subarray = arr2D[0:1, 1:3] Copies elements of first row, 2nd to 3rd column

print(subarray)
NumPy will allow you to create arrays that have more than two dimensions. In addition to arrays of more than two dimensions, indexing and slicing will remain consistent across any array shapes.

Changing the Shape of an Array

Similarly, in NumPy, it is possible to change the shape of an array without in any way altering its data, such as its content and its order. This means that you can transform a 1D array into a 2D array and the converse as well. For example:

  arr1D = np.array([1, 2, 3, 4, 5, 6])

arr2D = arr1D.reshape((2, 3)) Two dimensional array of two rows and three columns

print(arr2D)
In simpler terms, reshaping an array increases the performance of certain algorithms which require data to be in a certain format else the data will be wasted.

Other Significant Features of Numpy

There is a powerful feature called broadcasting in NumPy. As a result, NumPy has an inherent advantage because an operation can be done on two arrays of different sizes without the need to change the size of one of the two dimensions.

Suppose we are given a 2D array and a single number, and we wish to add that single number to all elements of that array. Broadcasting lets us do this – there is no need to loop through the array.

  arr2D = np.array([[1, 2], [3, 4]])

result = arr2D + 5 Add 5 to every element in the array

print(result)
As seen above, NumPy automatically expands the single number across the entire array which makes this operation straightforward and quick to perform.

Conclusion

Numerous functions would benefit from the inclusion of NumPy as it is an important library which needs to be included in the arsenal of anyone who is working with any data which is numerical in nature. It contains useful data structures, sophisticated array control, and many mathematical functions. In the case that you wish to work with one or more arrays or matrices or perform highly complex mathematical calculations, then you will be pleased to know that NumPy addresses the annoyance of using Python unnecessarily for those tasks. Learning this library will prepare students for working on problems related to data science, or machine learning in particular.
0
0
Article
comment
😀 😁 😂 😄 😆 😉 😊 😋 😎 😍 😘 🙂 😐 😏 😣 😯 😪 😫 😌 😜 😒 😔 😖 😤 😭 😱 😳 😵 😠
* Only support image type .JPG .JPEG .PNG .GIF
* Image can't small than 300*300px
Be the first comment
Just Reply
Elite Article
FeedBack

You have any problems or suggestions, please leave us a message.

Please enter content
Set
VIP
Sign out
Share

Share good articles, GFinger floral assistant witness your growth.

Please go to the computer terminal operation

Please go to the computer terminal operation

Forward
Insert topic
Remind friend
Post
/
Submit success Submit fail Picture's max size Success Oops! Something wrong~ Transmit successfully Report Forward Show More Article Help Time line Just Reply Let's chat! Expression Add Picture comment Only support image type .JPG .JPEG .PNG .GIF Image can't small than 300*300px At least one picture Please enter content