Skip to main content

Creating Arrays with Numpy module

Creating Arrays with Numpy module
Hi guys. Welcome to this amazing article. If you are new here, please do check out on our previous articles on python to get a brief understanding of what we have done so far. Also check out our video tutorials to the full tutorial.

Despite its simplistic name, Numpy as we have seen is a powerful python package that is mainly for working with arrays and matrices. And before working with Numpy, you have to know how to create arrays. They are several ways in creating arrays in a Numpy python module. 

Let’s start by creating a NumPy ndarray Object.

We used the array function to create ndarray object (array functions is group of numbers of the same type for example a list)

Example

import numpy as np
array = np.array([2,4,6,8,10])
print(array)
print(type(array))

The code above creates even numbers using Numpy while the type functions tells Numpy the type of code being passed to it, after which the code is run.

OUTPUT

Creating Arrays with Numpy module

Now we are going to see how to create Numpy arrays using Numpy functions.

Just like a normal python function, Numpy has built-in functions for creating arrays. We are going to see some of them in this article:

Creating one dimensional array

A one dimensional array is also known as array of rank 1. We used the arrange function to create arrays.

import numpy as np
array = np.arange(10)
print(array)

We want to create an array of the number10. The value is passed on to the arrange function. Using Numpy, the array function creates a range of values from 0 to 9.

OUTPUT

Creating Arrays with Numpy module

A Two Dimensional Array.

We are going to create this type of array by using the reshape function. The reshape function changes its arrangement without altering its data.

Example

import numpy as np
array = np.arange(20).reshape(4,5)
print(array)

The array created is converted to a 2-dimension array with 4 rows and 5 columns using the reshape the function.

OUTPUT

Creating Arrays with Numpy module

Creating three dimension 

To create a three dimension, we specify three parameters

Example

import numpy as np
array = np.arange(27).reshape(3,3,3)
print(array)

OUTPUT

Creating Arrays with Numpy module

Conversion of python data types.

We can also create Numpy arrays from data types such as list. Here we are going to use the array function. Below is syntax to use when creating arrays with a list

import numpy as np
array = np.array(list)
print(array)

Example

import numpy as np
array = np.array((1, 2, 3))
print(array)

OUTPUT

Creating Arrays with Numpy module

We can also use a list to create dimensional arrays.

For example: creating a 2 dimensional array.

import numpy as np
array = np.array([[1, 2, 3], [10, 8, 6]])
print(array)

OUTPUT

Creating Arrays with Numpy module

Knowing the number of dimensions

We can use the attribute ndim to know the number of dimensions in a code. This attribute is found in the Numpy function.

Example 

b = np.array([1, 2, 3,])
c = np.array([[1, 2, 3], [10, 8, 6]])
d = np.array([[[1, 2, 3], [10, 8, 6]], [[1, 2, 3], [10, 8, 6]]])
print(b.ndim)
print(c.ndim)
print(d.ndim)

OUTPUT

Creating Arrays with Numpy module

Higher dimensional arrays can be created by using ndim argument. This argument is assigned to the code. It tells the code the number of dimensions of arrays to create.

Example

import numpy as np
arr = np.array([1, 2, 3,], ndmin=10)
print(arr)
print('number of dimensions :', arr.ndim)

OUTPUT

Creating Arrays with Numpy module

Creating Numpy arrays is very important in performing numeric array computations. With these tools, you are now ready to create basic array operations.

We have come to the end of this session guys. Have nice coding guys.


Comments

Popular posts from this blog

Introduction to flask

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session because we are entering into one of python’s web-based application called flask. In this article, we are going to see the following What is flask Prequistes Installation of flask in python Some of flask template engine. What is flask? Flask is one of python-based framework which is used to create web applications. Flight is a very light web framework. During installation, flask comes with pre-installed modules and functions which are used to backup your own web applications. Flask is very easy to understand and perfect go-to for beginners and with flask, a web server can run in less than 3 lines of code. Prequistes Before learning flask, we recommend the reader to have a basic mastery of python concepts. Installation of flask  Before installing flask, we have to checked if python has been installed or. If n...

How to generate random numbers using NumP1

Hello. Welcome to another edition on this platform. For more better understanding, do checkout our YouTube channel to get the video tutorial. In this article of today, we are going to see how to generate random numbers using any of the following methods: Generating a random number Generating a random float and integer Generating random number arrays Generating random number from an array What is a random number? This is a number which cannot be predicted before its occurrence. This number might not different every time. Programmatically, they are two categories of random numbers:     Pseudo-Random numbers       True Random numbers. Just as programs which are written by programmers are a set of instructions, we must follow an algorithm to generate random numbers. Random numbers which are generated using an algorithm are called Pseudo-Random numbers. To generate a true random number, it is important to get the data from sources such as the keyboards, mou...

Introduction to Django

Hello. Welcome to another session on this platform. If you are new here, please do checkout our previous articles on python programming language and stay excited on this session. we are entering into one of python’s application called Django. In this article, we are going to discuss the following: What is Django Why must we use Django  A brief history of Django Installation of Django Popularity of Django What is Django? Python has so many framework application and Django happen to be one of them. Being a python-based-framework, it is used to quickly create web applications.  When building websites, django provides similar ready-made components to handle user authentication, forms, a way to upload components. Why use django? Django is very fast. It takes applications from concept to applications very quickly Django has thousand available packages in it when it is installed. With django, we can launch web applications is a matter of hours. Django is a highly is secured and helps...