 # Numpy Tutorial : A Guide for Beginners (Creation, Conversion ,Indexing )

Array in Python is the often use topics by the developers. These arrays are normal Python arrays and are not able to do faster mathematical operations on it. Then a module came know as Numpy. Numpy is also known as Numerical Python. It is generally use to perform faster mathematical value. You will be happy to know that Numpy performs faster in solving the mathematical operations than the normal Python arrays. In this article of Numpy Tutorial, you will learn every topic that is generally used in doing any projects in the Python Programming language. You will learn the following topics:-

• How to create arrays in Numpy?
• How to Convert Existing Lists and Tuples into Numpy Array?
• The indexing of the Numpy Array.

## Lets us begin the Numpy Tutorial ### How to create arrays in Numpy?

You are using Numpy for creating arrays. Therefore, first of all, you have to import Numpy module for performing all the tasks related to Numpy.

`import numpy as np #importing Numpy module as np`

#### One Dimensional Array

The syntax for creating the one-dimensional array is np.array([valu1,value2,valu3,…] ). The np is Python Numpy module and values can be of any datatypes and enclose by square brackets.

```# File name: Numpy1DArray.py
# Author: Data Science Learner
# Begin code

import numpy as np # importing Numpy Module as np
one_d = np.array([10,20,30]) # creating one dimensional array
print(one_d) # print the values of array```

You can see the above code np.array([10,20,0]) created one dimensional array and [10,20,30] are assign to the one_d variable. The print(one_d) will print the array.

Output

#### Multi-Dimensional Array

In the single dimensional array, You were taking values inside square bracket and was considering as single array values. In addition, You will pass multiples square brackets arrays to create the multi-dimensional arrays.Then all the arrays are enclosed by square brackets.

The syntax for the multi dimensiona array is

np.array([[value1,value2,…], [value1,value2,…],[value1,value2,…],…..[value1,value2,…]])

`multi_d = np.array([[10,20,30],[40,50,60],[70,80,90]]) # creating multi dimensional array`

In the above statement multi dimensional arrays are assign to the multi_d variable.

The print(multi_d) will display the multi-dimensional values.

`print(multi_d) # print the values of array`

Printing the shape of Array

You can also know the size of the array using the method variable_name.shape. Like the multi_d.shape will let you know the shape of the array. It means how much rows and columns are in the multi-dimensional array.

`print(multi_d.shape) # Display the shape of the array`

Find The data type of the array

The multidimensional array can store various types of data types. You can know the data types of the values using variable_name.dtype.

`print(multi_d.dtype)# print the data type of the multi dimensional array`

The print(multi_d_type) will print the data type of the values inside the array. In this case, the datatype is an integer so it will print int32.

Full Code

```# File name: NumpyMD.py
# Author: Data Science Learner
# Begin code

import numpy as np # importing Numpy Module as np
multi_d = np.array([[10,20,30],[40,50,60],[70,80,90]]) # creating multi dimensional array
print(multi_d) # print the values of array
print(multi_d.shape)
print(multi_d.dtype)# print the data type of the multi dimensional array```

Output

#### How to create zero matrix?

Zero Matrix are those matrix having all values of column and rows are zero. Numpy allows you to create zero matrix using the method zero(no_columns,no_rows).  Let’s take one example, zero([3,2]) will create 3×2 zero matrix . It means the number of rows with zero values will be and the number of columns with zero values is 2.

Full Code

```# File name: ZeroMatrix.py
# Author: Data Science Learner
# Begin code

import numpy as np  # importing Numpy Module as np

multi_d = np.zeros()  # creating zero matrix with columns 3
print("3x1 matrix")
print(multi_d)  # print the values of array
print("3x2 matrix \n")
multi_d = np.zeros([3, 4]) # creating zero matrix with columns 3 and 2 rows
print(multi_d)
```

You can see the multi_d = np.zeros() will create zero matrix with three columns and one rows. In the same way, multi_d = np.zeros([3, 4]) will create zero matrix with three number of columns and four number of columns.

Output

#### How to create diagonal matrix?

The diagonal matrix is a square matrix whose diagonal values are 1. In fact, values outside the main diagonal are zero. You will use the eye(IntegerValue) to create a diagonal matrix. For example, eye(4) will create a diagonal matrix with both four rows and columns.

Full Code

```# File name: DiagonalMatrix.py
# Author: Data Science Learner
# Begin code

import numpy as np  # importing Numpy Module as np

multi_d = np.eye(4)  # creating diagonal  matrix with columns 3 columns and  rows
print(multi_d)
```

Output

## How to Convert Existing Lists and Tuples into Numpy Array?

List and Tuples are part of the data structures. The list contains values enclosed in the square brackets whereas tuples enclose values by brackets. The syntax for both list and tuples are below.

List

list = {value1,valu2,value3,…}

Tuples

tuples = (value1,valu2,value3,…)

Both the list and tuples Values can be anything integer,string,float or other data types.

You can easily convert the existing list or tuples to Numpy Array using the function asarray(list_name or tuple_name). For example, you have the list of integers.

list= {10,20,30}

You want to convert it into Numpy array then use the list as an argument to asarray() method, that is asarray(list).

Full Code

```# File name: NumpyConversion.py
# Author: Data Science Learner
# Begin code

import numpy as np  # importing Numpy Module as np

list = [10,20,30] # list
list_con = np.asarray(list) # coversion of list to Numpy Array
print(list_con)```

You can see the above code list_con = np.asarray(list) converted the list into Numpy Array.

Output

Now, Let’s take an example of Tuple. Suppose you have a tuple of strings

The method asarray(tuple) will convert the above tuple into a numpy array.

Full Code

```# File name: NumpyConversion.py
# Author: Data Science Learner
# Begin code

import numpy as np  # importing Numpy Module as np

#list = [10,20,30] # list
tuple_con = np.asarray(tuple) # coversion of list to Numpy Array
print(tuple_con)```

The np.asarray(tuple) converted the above tuple into the numpy array and assigned it to the variable tuple_con.

Output

You can also convert the datatype of the list values using asarray() method. You have to just add an argument dtype=float or int e.t.c.

Let’s take the example of the above list. It has integer values. You can convert it into the float by passing the dtype= float into the asarray() method.

```list_con = np.asarray(list,dtype= float) # coversion of list to Numpy Array as a Float data type
```

You can see the np.asarray(list,dtype= float) as accepted the two arguments. The first one is the list or the second one is dtype = float.

Full Code

```# File name: NumpyConversion.py
# Author: Data Science Learner
# Begin code

import numpy as np  # importing Numpy Module as np
list = [10,20,30]
list_con = np.asarray(list,dtype= float) # coversion of list to Numpy Array as a Float data type
print(list_con)```

Output

## How to do indexing of the Numpy Array?

Indexing of the array is to access the value of the data of it. You already know that index of the data inside the array lies from 0 to n-1. Thus, you have to just write off the index value inside the ‘[]’ square bracket.

You will understand more through the examples.

#### One Dimensional Indexing

You have one-dimensional array one_d = np.array([10,20,30]). You want to access the values first and last values. Then one_d and one_d will access the value at the index position 0 and 2.

Full Code

```# File name: Numpy1DArray.py
# Author: Data Science Learner
# Begin code

import numpy as np # importing Numpy Module as np
one_d = np.array([10,20,30]) # creating one dimensional array
print(one_d)
print(one_d)```

Output

It will give the output as 10 and 30.

#### Two Dimensional Indexing

Suppose you have a two-dimensional list of array  multi_d = np.array([[10,20,30],[40,50,60],[70,80,90]]). The indexing of the two dimension is done with two square brackets ‘[][]’. The first bracket will tell the compiler the that you have to index the row and second bracket to index the column. For example, you have to access the first row and first column values.Then the following statement will access the values

multi_d: It will access the first row and first column value that is 10.

multi_d: This will access the value second row and first column value 40.

Full Code

```# File name: NumpyMD.py
# Author: Data Science Learner
# Begin code

import numpy as np # importing Numpy Module as np
multi_d = np.array([[10,20,30],[40,50,60],[70,80,90]]) # creating multi dimensional array

print(multi_d) # print the value of  first row and first column value
print(multi_d) # print the value of second row and first column value```

Output

## Conclusion

Normal arrays are best for short mathematical operations. If you try to do a mathematical calculation on the large arrays, then it will take more time than Numpy Array. Therefore it’s better to use Numpy Module for faster calculations.

I hope you learned the method to create Numpy arrays and conversion from existing lists and tuples. You have also learned how to do indexing in the Numpy array. Keep visiting this page as we will keep adding new topics on Numpy. If anything you want to include related to Numpy then you can Contact Us. or Message us on Offical Data Science Learner Page

Data Science Learner Team 