Python
Python
Python
Arrays in Numpy
Array in Numpy is a table of elements (usually
numbers), all of the same type, indexed by a tuple of
positive integers. In Numpy, number of dimensions of
the array is called rank of the array.A tuple of integers
giving the size of the array along each dimension is
known as shape of the array. An array class in Numpy
is called as ndarray. Elements in Numpy arrays are
accessed by using square brackets and can be
initialized by using nested Python Lists.
Creating a Numpy Array
Arrays in Numpy can be created by multiple ways,
with various number of Ranks, defining the size of the
Array. Arrays can also be created with the use of
various data types such as lists, tuples, etc. The type
of the resultant array is deduced from the type of the
elements in the sequences.
#python program for
#creation of arrays
Import numpy as np
#creating a rank 1Array
arr=np.array([1, 2, 3])
print(“Array with Rank 1:\n”,arr)
#creating a rank 2 Array
arr=np.array([[1,2,3],
[4,5,6]])
print(“Array with Rank 2:\n”,arr)
#creating an array from tuple
arr=np.array((1,3,2))
print(“\nArray created using”
“passed tuple:\n”,arr)
Output
import numpy as np
# Initial Array
arr = np.array([[-1, 2, 0, 4]
# Printing elements at
# specific Indices
Index_arr = arr[[1, 1, 0, 3],
[3, 2, 1, 0]]
print ("\nElements at indices (1, 3), "
"(1, 2), (0, 1), (3, 0):\n", Index_arr)
Output
import numpy as np
# Integer datatype
# guessed by Numpy
x = np.array([1, 2])
Output