# 6 ways to initialize NumPy array in Python

In this post, we are going to understand 6 ways to initialize the NumPy array in Python. There are many ways in the NumPy library to initialization of array by using built-in functions np.array(),np.zeros(),np.empty(),np.ones() and initialize numpy array of unknown length.

### 1. initialize NumPy 2D array in python using np.array()

In this Python program, we will learn how to initialize NumPy 2D array in python. The np.array() numpy function takes an argument that can be an array or multi-dimensional list values.

• To install the numpy simply use command “pip install numpy” on window.
• We have imported the NumPy libaray using “import numpy as np“.
• The intializing numpy array with values by using np.array() function passe by passing a list of lists values.
```import numpy as np

nparr = np.array([[3, 6], [9, 15]])

print(nparr)

```

Output

```[[ 3  6]
[ 9 15]]
```

### 2. initialize NumPy array using np.zeros()

In this Python program example, we will discuss how to initialize a numpy array with zeros. The numpy.zeros(shape) function creates a numpy array of a given shape and initializes all values 0.

• We have imported the NumPy libaray using “import numpy as np
• We have intialize numpy array with zeros 0 by using np.zero() function of given shape(3,3).
• We have specified the datatype as int while initializing array if not specified defualt type is float.
```import numpy as np

nparr = np.zeros((3, 3),dtype=int)

print(nparr)

```

Output

```[[0 0 0]
[0 0 0]
[0 0 0]]
```

### 3. initialize NumPy array using np.ones()

In this Python program example, we will learn how to initialize a numpy empty array by using numpy.ones(shape) function.The numpy.ones(shape ) creates an array of given shape size and initializes array all values with ones.

• We have intialize numpy array by using np.ones(shape) function of given shape(3,3).It will intialize an array of 1 ones.
• We have specified the datatype as int while initializing array if not specified the defualt defualt type is float..
```import numpy as np

nparr = np.ones((3, 3),dtype=int)

print(nparr)
```

Output

```[[1 1 1]
[1 1 1]
[1 1 1]]
```

### 4. initialize empty NumPy array using np.empty()

In this Python program example, we will learn how to initialize a numpy empty array by using numpy.empty() function.The numpy.empty(shape ) creates an empty numpy array of given shape size and initializes array values randomly.

• We have intializ numpy array by using np.empty() function of given shape(3,3).It will create an array that will have random value of type int
• We have specified the datatype as int while initializing array if not specified the defualt type is float.
```import numpy as np

nparr = np.empty((2,3),dtype=int)

print(nparr)
```

Output

```[[15490152 15486832 15490480]
[15490920 15490920        0]]
```

### 5. initialize numpy array of unknow length

In this Python example, we are using the np. array() to initialize numpy array of unknown length. We have iterated over the elements in the list y and append elements of y to nparr by using the append() method and finally convert it into a numpy array. This is how we create

```import numpy as np

nparr = []
y = [1,2,3,4,5]
for item  in y:
nparr.append(item)
nparr = np.array(nparr)

print(nparr)
```

Output

```[1 2 3 4 5]
```

### 6. initialize numpy array of without shape

In this Python example, we will understand how to create an empty NumPy array without shape

• We have declared an empty list to an empty array.
• we have np array(nparr) that is created from list of elements.
• for loop to through list and using np.append() function to append to element in list.list is empty,so it create an python numpy empty array.
```lst=[]
nparr = np.array([3,6,9,12])
for item in lst:
nparr = np.append(nparr, item)
print('empty array:'lst)

```

Output

```empty array :[]

```

### Summary

In this post we have learned 6 ways to initialize NumPy array in python by using built-in functions np.array(), np.zeros(), np.empty(),np.ones() and initialize numpy array of unknow length.