Introduction To Numpy Library- Part 1

Numpy is an open-source library licensed under the BSD, and it was developed back in 2005 by Travis Oliphant for working with arrays. The name Numpy stands for “Numerical Python” and people developed this library to even work with multidimensional array objects.

With ease, you can also use logical operations and mathematics operations on arrays and numbers. Developers prefer to use numpy over lists in Python because it is faster at around 30 times which is insane with even less memory resource from your computer. It also supports many scientific functions, such as linear algebra, and is used for random number generation.

1. Install The Package

We’ve mentioned that numpy is developed to work with arrays, and we will see a simple example of creating an array. Before we do that, let’s install it first:

``pip3 install numpy``

Use the command above to install the library on your IDE, but if you are using the Jupyter Notebook cells, then you need to add the “!” in the command:

``!pip3 install numpy``

Both ways will work depending on your IDE, and even some of them, like PyCharm, have an easy way of doing it through the GUI instead of using commands. If you have numpy already installed in your system, it would be better to check its version, so you make sure you use the latest one:

``````import numpy

# Check Numpy Version
print(numpy.__version__)``````

You can check out the latest one on their official website, and if it is not the latest one, I recommend installing it again to update it.

2. Create a Simple Array

Enough talking for now about numpy, and let’s create a simple array to understand how it works:

``````import numpy as np

# Create an Array
array = np.array([10, 20, 30, 40])

# Print The Array
print(array)``````

You can see that creating an array in numpy is very easy. You need to use the .array() function and type the array numbers inside the square brackets. Let’s take a look at the type of this Array:

``````import numpy as np

# Create an Array
array = np.array([10, 20, 30, 40])

# Type of The Array
print(type(array))``````

You see that it shows the type of this Array is “numpy.ndarray” and the type function is a Python built-in function that can give you the type of any variable in Python and not just for arrays or numpy. You can also use the tuple instead of list (Square brackets) to create an array in numpy:

``````import numpy as np

# Create an Array
array = np.array((10, 20, 30, 40))

# Print The Array
print(array)``````

It will work fine, like using the square brackets but with curly brackets. You can also access elements of the Array using indexing:

``````import numpy as np

# Create an Array
array = np.array((10, 20, 30, 40))

# Access Fourth Element
print(array[3])``````

The code will print element number four 40, although we’ve specified 3 inside the square brackets. Indexing in Python starts counting from 0 instead of 1, so the number 3 in indexing means the fourth number.

3. 0-D Array

We’ve said that numpy can work with multidimensional arrays, but we didn’t see these multidimensional arrays. The first type is the 0-D Array which contains only one element:

``````import numpy as np

# Create a 0-D dimension Array
array = np.array(60)

# Print The Array
print(array)``````

You can notice that this Array contains only one element and what discriminate it is that you can’t access its elements since it only contains one element. Even if you try to access the only element which is at index 0, it will raise an error:

``````import numpy as np

# Create a 0-D dimension Array
array = np.array(60)

# Access The First Element
print(array[0])``````

Once you execute the previous code, it will raise an index error message “too many indices for array” since you only have one element, so there is no need to have indexed here or use it.

4. 1-D Arrays

This is the most common Array that is used, which is the same as we’ve used before for the first example:

``````import numpy as np

# Create an Array
array = np.array([10, 20, 30, 40])

# Print The Array
print(array)``````

You can access its elements as we’ve discussed before, and you will use it a lot later when you become a professional developer and build complex programs or work in a field that involves working with data.

5. 2-D Arrays

This is a commonly used array in deep learning, machine learning, artificial intelligence, and more. It is an array that represents a matrix. Let’s see an example:

``````import numpy as np

# Create a 2-D dimension Array
array = np.array([[10, 20, 30, 40], [100, 200, 300, 400]])

# Print The Array
print(array)``````

It doesn’t look very clear, but you can see that we have two lists inside one extensive list since there are two square brackets. You can access its elements using the following syntax:

``````import numpy as np

# Create a 2-D dimension Array
array = np.array([[10, 20, 30, 40], [100, 200, 300, 400]])

# Access The Array Elements
print(array[1][2])``````

It will print the number 300 and do you know why? Because the second square brack is the index two, which mean we are working with the second list. The second square bracket is index 2, which means the third number in the second list.

Conclusion

Thanks for reading! This library is used in almost every program you need to perform calculations. People who work in data science, artificial intelligence, machine learning, data analysis, or any field related to data are used. It will be a massive benefit if you are new to python programming and start learning this library.

Scroll to Top