## Introduction to NumPy shape

The following article provides an outline for NumPy shape. Python provides different functions to the users.** **To work with arrays, the python library provides a NumPy function. Arrays play a major role in data science where speed matters. NumPy is an acronym for numerical python. Basically, NumPy is an open source project. NumPy performs logical and mathematical operations of arrays. Also, Fourier transforms and operations on linear algebra can be done using NumPy. In python, NumPy is faster than list. This is because NumPy arrays can be stored at continuous places. Therefore, processing and manipulating can be done efficiently.

**Syntax:**

`Import numpy as np`

a = np.array([])

print(a)

**Explanation:**

- In above syntax where first we import numpy class and access a numpy class as np, where a is variable and we assign array value to a then print value of a.

### How does shape Function work in NumPy?

- We must install Python on our system.
- We must install numpy using the pip command.
- We required basic knowledge about Python.
- We required basic knowledge about arrays.
- We can perform different operations using a numpy shape function.

Let’s see how we can implement numpy shape on 2D arrays.

**Example:**

**Code:**

`import numpy as np`

a = np.array ([[2,6],[7,4]])

print(a)

print(a.shape)

**Explanation:**

- In the above example we show 2D array representation, where we import numpy functions and assign them as np objects. We use variable a to store array elements.
- Then we print the given array as well as the shape of that array. Illustrate the end result of the above declaration by using the use of the following snapshot.

**Output:**

Let’s see how we can implement numpy shape on 2D arrays.

**Example:**

**Code:**

`import numpy as np`

a = np.array ([[2,4,6],[7,8,4],[1,2,3]])

print(a)

print(a.shape)

**Explanation:**

- In the above example we show 3D array representation, where we import numpy functions and assign them as np objects. We use variable a to store array elements.
- Then we print the given array as well as the shape of that array. Illustrate the end result of the above declaration by using the use of the following snapshot.

**Output:**

Let’s see how we can implement nested python list using numpy array.

4.5 (6,518 ratings)

View Course

**Example:**

**Code:**

`import numpy as np`

x = np.array([7, 8, 9])

print(type(x))

print(x.shape)

print(x[0], x[1], x[2])

x[0] = 2

print(x)

y = np.array([[2,3,4],[5,6,7]])

print(y.shape)

print(y[0, 0], y[0, 1], y[1, 1])

**Explanation:**

- In the above example we implemented a nested python list using numpy array function, where first we import numpy functions in the project. We have created an array and stored it in variable x. By using type command, we have determined the type of array as numpy. Again, by using shape command, the number of elements in the array can be determined.
- Here, in a numpy array we can replace the elements. In the above example, we have replaced elements with zero index. Also, we have created the array of rank 2. The array shape is (2, 3). Illustrate the end result of the above declaration by using the use of the following snapshot.

**Output:**

NumPy provides different functions to create arrays, let’s see how we can create arrays.

**Example:**

**Code:**

`import numpy as np`

x = np.zeros((3,2))

print(x)

y = np.ones((4,5))

print(y)

z = np.full((3,3), 5)

print(z)

w = np.eye(5)

print(w)

v = np.random.random((5,5))

print(v)

**Explanation:**

- In the above example we use a different numpy function, where we use a zero function. This function is used to create a 3 by 2 matrix with all array elements is zero. Here we use the next function as ones it is used to create a 4 by 5 matrix with all elements is one.
- Next function we use is full, it is used to create a 3 by 3 matrix with all constant array elements. Next function random it used to create a 5 by 5 matrix and it generated random values. Illustrate the end result of the above declaration by using the use of the following snapshot.

**Output:**

**Data Type in NumPy Array:**

In numpy arrays we use the same data type, numpy arrays provide numeric data type to draw an array. But numpy provides a facility to include explicitly data types.

**Example:**

**Code:**

`import numpy as np`

x = np.array([5, 6])

print(x.dtype)

x = np.array([3.0, 4.0])

print(x.dtype)

x = np.array([7, 8], dtype=np.int64)

print(x.dtype)

**Explanation:**

- In the above example, dtype gives the data type of the array as int64. Similarly, it is also giving float 64 as a data type in the next example of numpy. In the next example we forcefully assign data type to array.
- Illustrate the end result of the above declaration by using the use of the following snapshot.

**Output:**

**NumPy Array Math:**

NumPy arrays provide different mathematical functions to users, with the help of mathematical functions we perform different arithmetic operations as follows.

**Example:**

**Code:**

`import numpy as np`

a= np.array([[2,3],[4,5]], dtype=np.int64)

b = np.array([[6,7],[8,9]], dtype=np.int64)

print(a + b)

print(np.add(a, b))

print(a - b)

print(np.subtract(a, b))

print(a * b)

print(np.multiply(a, b))

print(a / b)

print(np.divide(a, b))

print(np.sqrt(a))

**Explanation:**

- In the above example we implement arithmetic operation by using a numpy array math function. In this example we implement add, subtract, multiply, divide and sqrt function with integer data type.
- Illustrate the end result of the above declaration by using the use of the following snapshot.

**Output:**

**NumPy Array Indexing:**

We can access the array element by using NumPy array indexing function.

**Example:**

**Code:**

`import numpy as np`

a = np.array([7, 8, 9, 10])

print(a[3])

**Explanation:**

- In the above example, we can access any element of the given array. The array elements are accessed by using index. Here we access the element of index 3.

**Output:**

Array indexing can be implemented on 2D arrays also.

**Example:**

**Code:**

`import numpy as np`

b = np.array([[6,7,8,9,10],[5,2,4,3,1]])

print(b[0, 2])

**Explanation:**

- In the above example we implement array indexing by using a 2D array. Illustrate the end result of the above declaration by using the use of the following snapshot.
- In a similar way we can implement 3 D array indexing.

**Output:**

### Conclusion

From the above article we saw basic syntax numpy shape arrays. We also saw how we can implement them in python with different examples of each type. From this article we saw how we can handle numpy shape in python.

### Recommended Articles

This is a guide to NumPy shape. Here we discuss the introduction to NumPy shape and how does shape function work with respective examples. You may also have a look at the following articles to learn more –