# Array Creation in NumPy

Here we will learn how to create array using NumPy in Python with some easy examples.

Numerical Python, often abbreviated as NumPy and is a very powerful high-level package with offers very powerful tools for scientific computing and data science. The main feature is its capability of creating multi-dimensional arrays in Python. It is also the basis for understanding other modules such as pandas in detail.

Before proceeding further, make sure that you have Python3 installed in your system. We also need to ensure to have the NumPy module installed.

## Installation of NumPy

To install NumPy package, follow the given steps:

1. For Windows OS:

- Make sure you have pip installed in your system.
- In case pip is not installed you can also go for anaconda
- Go to Powershell >>Run as administrator and then type the following command:

pip install numpy

2. For Linux-based systems (Ubuntu and Debian):

Open the terminal and type:

sudo apt-get install python-numpy

3. For Mac OS systems:

Open the terminal and type:

brew install numpy

## How to create an array using NumPy

Once the NumPy module has been installed, we need to understand how to create an array using the same.

NumPy gives you the liberty to create arrays of any dimension. For example, consider the following code snippet:

import numpy as np info = [1,2,3,4,5,6,7,8] #one dimensional array data = np.array(info) #converts into numpy array print(data)

Output:

[1 2 3 4 5 6 7 8]

We can adopt a similar procedure for a two-dimensional array also. For example:

import numpy as np info = [[1,2,3,4,5],[6,7,8,9,10],[11,12,13,14,15]] #a two dimensional array data = np.array(info) #converts into numpy array print(data)

Output:

[[ 1 2 3 4 5]

[ 6 7 8 9 10]

[11 12 13 14 15]]

## How to initialize every element of array with zero

An array consisting of all elements initialized to zero can be created (both in 1-D and 2-D arrays) by :

import numpy as np data1 = np.zeros(5) #creates 1-D array of 5 elements all initialized to 0 data2 = np.zeros((4,7)) #creates 2-D array of 4x7 elements all initialized to 0 print(data1) print("\n",data2)

Output:

[0. 0. 0. 0. 0.]

[[0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0.]

[0. 0. 0. 0. 0. 0. 0.]]

An empty array can also be created but it is important to note that while creating such an array, all the values are by default initialized with some dummy values. For instance consider the following piece of code:

import numpy as np data = np.empty((2)) #creates 1-D array of 5 elements all initialized to garbage values print(data)

Output:

[-1.09132710e+300 -3.62675172e-108]

## Leave a Reply