Articles

what is numpy? | creating numpy array in python | 1d & 2d array examples

what is numpy? | creating numpy array in python | 1d & 2d array examples


In this numpy tutorial, we will discuss about:

  • what is numpy?
  • how to create numpy Array in Python?
  • numpy array in python example,
  • creating numpy array from list,
  • create numpy array from tuple,
  • create numpy 1d array,
  • create numpy 2d array

What is numpy?

numpy stands for numeric python which is used to perform mathematical operations on arrays.

 

numpy is a module which we have to import from the python. 

Syntax to import numpy:

import numpy

We can also use alias for the module 

For example,

import numpy as np

Now, we can directly use np to call the numpy module. We will see examples for the same as we proceed.


Numpy Array in python

An array is an one dimensional data structure used to store single data type data, i.e, it will only store all integer data or all string type data.or all float type data.

 

We can create numpy array in python by using array() function.

Syntax numpy array in python:

numpy.array(elements)

where, elements are the input data elements.

 

Now lets see few examples of how to create numpy array in python?


numpy array in python example creating numpy array from list

In this numpy array in python example, we will create integer, float and string arrays through list and display the elements in each of the array.

 

Lets see the code to creating numpy array from list:

#import the numpy module
import numpy

#create an integer array with 5 elements
integers=numpy.array([12,34,43,34,21])

#display the array
print(integers)

#create an float array with 5 elements
floats=numpy.array([12.2,34.45,43.45,34.43,21.43])

#display the array
print(floats)

#create an string array with 3 elements
strings=numpy.array(['w','q','r'])

#display the array
print(strings)

Output: creating numpy array from list result elements are displayed below

[12 34 43 34 21]
[12.2  34.45 43.45 34.43 21.43]
['w' 'q' 'r']

Lets see another example to create numpy array from tuple.


numpy array in python examplecreate numpy array from tuple

In this numpy array in python example, we will create integer, float and string arrays through tuple and display the elements in each of the array.

 

Lets see code to create numpy array from tuple:

#import the numpy module
import numpy

#create an integer array with 5 elements
integers=numpy.array((12,34,43,34,21))

#display the array
print(integers)

#create an float array with 5 elements
floats=numpy.array((12.2,34.45,43.45,34.43,21.43))

#display the array
print(floats)

#create an string array with 3 elements
strings=numpy.array(('w','q','r'))

#display the array
print(strings)

Outputcreate numpy array from tuple result elements are displayed below

[12 34 43 34 21]
[12.2  34.45 43.45 34.43 21.43]
['w' 'q' 'r']

If we want to get the array type, then we can use type() function. It will return the class IE. 'numpy.ndarray'

numpy array in python example: using type()

In this example, we created three different data type arrays and get the types

#import the numpy module
import numpy

#create an integer array with 5 elements
integers=numpy.array((12,34,43,34,21))

#create an float array with 5 elements
floats=numpy.array((12.2,34.45,43.45,34.43,21.43))


#create an string array with 3 elements
strings=numpy.array(('w','q','r'))

#display the type of arrays
print(type(integers),type(floats),type(strings))

Output:

<class 'numpy.ndarray'> <class 'numpy.ndarray'> <class 'numpy.ndarray'>

Let's talk about array dimensions.


ndim in numpy

We can create multidimensional arrays by specifying elements in nested lists.

 

If we want to get the dimensions, then we have to use function ndim in numpy.

ndim in numpy syntax:

array_name.ndim

Where, array_name is the input array

 

Create numpy 1D Array

Syntax:

numpy.array([elements])

We will use of ndim in numpy in below numpy array in python example.

 

create numpy 1d array example:

In this create numpy 1d array example, we will create three different type arrays of 1 dimension.

 

Lets see code to create numpy 1d array:

#import the numpy module
import numpy

#create an integer array with 5 elements
integers=numpy.array([12,34,43,34,21])

#create an float array with 5 elements
floats=numpy.array([12.2,34.45,43.45,34.43,21.43])


#create an string array with 3 elements
strings=numpy.array(['w','q','r'])

#display the  arrays
print(integers,floats,strings)

#display the  dimensions
print(integers.ndim,floats.ndim,strings.ndim)

Outputcreate numpy 1d array result

[12 34 43 34 21] [12.2  34.45 43.45 34.43 21.43] ['w' 'q' 'r']
1 1 1

From the above code, we created integer, float and string type arrays and displayed the elements along with the dimensions. As the array dimensions are of 1 , so the dimension output will be 1.

 

Now lets learn to create numpy 2d array.


create numpy 2d array

Syntax:

numpy.array([[elements],[elements],...........,[elements]])

 

create numpy 2d array example :

In this create numpy 2d array example, we will create three different type arrays of 2 dimensions.

#import the numpy module
import numpy

#create an integer array with 5 elements for each 3 array elements
integers=numpy.array([[12,34,43,34,21],[12,34,43,34,21],[12,34,43,34,21]])

#create an float array with 5 elements for each 2 array elements
floats=numpy.array([[12.2,34.45,43.45,34.43,21.43],[12.2,34.45,43.45,34.43,21.43]])


#create an string array with 3 elements for each 2 array elements
strings=numpy.array([['w','q','r'],['c','a','e']])

#display the  arrays
print(integers,floats,strings)

#display the  dimensions
print(integers.ndim,floats.ndim,strings.ndim)

Outputcreate numpy 2d array example result

[[12 34 43 34 21]
 [12 34 43 34 21]
 [12 34 43 34 21]] [[12.2  34.45 43.45 34.43 21.43]
 [12.2  34.45 43.45 34.43 21.43]] [['w' 'q' 'r']
 ['c' 'a' 'e']]
2 2 2

From the above code, we created integer , float and string type arrays and displayed the elements along with the dimensions. As the array dimensions are of 2 , so the dimension output will be 2.

 

So, we can create numpy 1d array, create numpy 2d array in this way.

 

This wraps up our session on creating numpy array in python along with numpy array in python example.


Numpy

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com

About the Author
Gottumukkala Sravan Kumar 171FA07058
B.Tech (Hon's) - IT from Vignan's University. Published 1200+ Technical Articles on Python, R, Swift, Java, C#, LISP, PHP - MySQL and Machine Learning
Page Views :    Published Date : Apr 13,2022  
Please Share this page

Related Articles

Like every other website we use cookies. By using our site you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and our Terms of Service. Learn more Got it!