×

Numpy Attributes

Numpy Attributes

The Numpy collections give attributes to the numpy arrays. These attributes assist us with knowing the shape, aspect, and different properties of a given numpy array. As we know, it is a Python library consisting of multidimensional array objects. The quantity of components in each aspect is named as the state of an array. NumPy arrays have a characteristic considered shape that profits a tuple, with each record having the quantity of comparing components. This can likewise be utilized to resize the array

ndarray.shape

This array property returns a tuple comprising array aspects. It can likewise be utilized to resize the array. The ndarray.shape characteristic returns the element of the array. A tuple of whole numbers demonstrates the size of your NumPy array. For example, if you make a matrix with a rows and b columns, the shape will be (a * b).

Example 1

import numpy as np
int = np.array ([[2,3,4],[5,6,7]])
print ( int. shape)	

Output

Numpy Attributes

In the above example, we first imported numpy as np by using features of the numpy library in Python. After that, we implemented arrays, and we assigned that to a variable named int.In the last line of code, we have printed the shape of the np.array by utilizing int.shape. It will give the shape of the array.

Example 2

#This is how we can resize the ndarray
import numpy as np 
int = np.array([[2,3,4],[5,6,7]]) 
int.shape = (3,2) 
print (int) 

Output

Numpy Attributes

In example 2, we have also imported numpy as np. But here, we have resized the shape of the array by assigning the value to a.shape.

NumPy library also gives us a reshape function to resize an array.

import numpy as np 
int = np.array([[2,3,4],[5,6,7]]) 
b = int.reshape(3,2) 
print (b)

Output

Numpy Attributes

In the numpy library, we also have a reshape function through which we can change the shape of the array. Now we have two methods to reshape the dimensions of the array.

ndarray.ndim

This array attribute provides the number of array dimensions. These characteristic assists track down the quantity of components of a NumPy with arraying. For example, 1 implies that the array is 1D,2 means that the array is 2D, etc.

Example 1

# array of even numbers 
import numpy as np 
inte = np.arange(28) 
print (inte)

Output

Numpy Attributes

In the above example, we have used np.arange to check the number of array dimensions from 0 to 28. It will provide all the array dimensions from 0 to 28.

Example2

# the one-dimensional array 
import numpy as np 
ara = np.arange(24) 
ara.ndim  
# now we are reshaping it 
b = ara.reshape(2,4,3) 
print (b)

Output

Numpy Attributes

We can also reshape the array dimension after arranging the dimensions. Reshaping an array implies changing the state of an array. By this, we can add or eliminate aspects or change the number of components in each aspect.

ndarray.T

The T attribute is used to get the transpose of an array.

import numpy as np
ara = np.array([[1, 2, 3], [4, 5, 6]])
print("ara is:")
print(ara)
print("\nTranspose of ara is:")
print(ara.T)

Output

Numpy Attributes

Size: This attribute computes the complete number of components present in the NumPy array.

dtype: This characteristic assists with checking what kind of components are put away in the NumPy array.

itemsize: This characteristic assists with finding the length of every component of the NumPy array in bytes. For instance, if you have whole number qualities in your array, this characteristic will bring 8 back 8 as whole number qualities and take 8,8-bits in memory.

Information: This characteristic is a cushioned object that focuses on the beginning of the NumPy array. Be that as it may, this characteristic isn't involved much since we mostly access the components in the array utilizing records.

Let us understand these numpy attributes by using a single program. In this program, we have implemented all the numpy attributes.

import numpy as np


fst_array = np.array([2,3,4])


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


print(" The First array is: ",fst_array)


print(" The Second array is: ",sec_array)


print("\nThe number of dimensions in  first array: ",fst_array.ndim)


print("The number of dimensions in second array: ",sec_array.ndim)


print("\nThe shape of array first array: ",fst_array.shape)


print("The shape of array second array: ",sec_array.shape)


print("\nThe itemsize of first array: ",fst_array.itemsize)


print("The itemsize of first array: ",sec_array.itemsize)


print("\nThe data of first array is: ",fst_array.data)


print("The data of second array is: ",sec_array.data)


print("\nThe data type of first array: ",fst_array.dtype)


print("The data type of second array: ",sec_array.dtype)


print("\nThe size of first array: ",fst_array.size)


print("The size of second array: ",sec_array.size)

Output

Numpy Attributes

Related Topics

numpy.ndarray.T in Python

numpy.ndarray.T in Python The numpy.ndarray.T attribute makes a Transpose of an array having a dimension greater than or equal to 2. Syntax ndarray.T() Parameter NA Return This attribute returns the transpose of an array Example 1 # Python Program explaining # numpy.ndarray.T()...

1 minute read.

numpy.expand_dims() in Python

numpy.expand_dims() in Python The numpy.expand_dims() function expands the shape of an array. It Inserts a new axis that appears at the axis position in the expanded array shape. Syntax numpy.expand_dims(a, axis) Parameter The numpy.expand_dims() function has two parameters...

1 minute read.

Numpy.copy() in Python with Example

Numpy.copy() in Python The copy() function of Python numpy class returns an array copy for the given object. Syntax numpy.copy(a, order='K') Parameter a: It represent the array_like input data. order :  This parameter controls the memory layout of...

1 minute read.

numpy.unique() in Python

numpy.unique() in Python The numpy.unique() function finds the unique elements of an array and returns the sorted unique elements for the specified array.  Syntax numpy.unique(ar, return_index=False, return_inverse=False, return_counts=False, axis=None) Parameter ar : This parameter returns an Input array.If the aaray is...

2 minutes read.

numpy.tile() in Python

numpy.tile() in Python The numpy.tile() function constructs an array by repeating the parameter ‘A’  the number of times as specified by the ‘reps’ parameter. Syntax numpy.tile(A, reps) Parameter The numpy.tile() function consists of two parameters, which...

1 minute read.

numpy.repeat() in Python

numpy.repeat() in Python The numpy.repeat() function repeats the elements of an array. Syntax numpy.repeat(a, repeats, axis=None) Parameter The numpy.repeat() function consists of three parameters, which are as follows: a: This parameter represents the input array. repeats: This parameter represents the...

1 minute read.

numpy.asarray() in Python

numpy.asarray() in Python The numpy.asarray()function is used to convert the input to an array. Input includes lists, lists of tuples, tuples, tuples of tuples,etc,. Syntax numpy.asarray(a, dtype=None, order=None) Parameter arr : This parameter includes theinput data, in any...

1 minute read.

numpy.column_stack() in Python

numpy.column_stack() in Python The numpy.column_stack() function stacks  the 1-D arrays as columns into a 2-D array. It takes a sequence of 1-D arrays and stacks them as columns to make a...

1 minute read.

numpy.split() in Python

numpy.split() in Python The numpy.split() function splits an array into multiple sub-arrays. Syntax numpy.split(ary, indices_or_sections, axis=0) Parameter ary: This parameter represents the Array to be divided into sub-arrays. indices_or_sections : This parameter represents an int or an 1-D array. If indices_or_sections is an...

1 minute read.

numpy.arrange() in Python

Python numpy.arrange() The arrange() function of Python numpy class returns an array with equally spaced elements as per the interval where the interval mentioned is half opened, i.e. [Start, Stop). Syntax numpy.arange([start, ]stop, [step, ]dtype=None) Parameter start :It...

1 minute read.

numpy.atleast_1d() in Python

numpy.atleast_1d() in Python The numpy.atleast_1d() function converts the inputs to arrays with at least one dimension. Syntax numpy.atleast_1d(*arys) Parameter arys1, arys2, … : This parameter represents one or more input arrays. Return This function returns an array, or list...

1 minute read.

numpy.tri() in Python

numpy.tri() in Python The tri() function of Python numpy class returns an array with ones at and below the given diagonal(k value) and zeros elsewhere. Syntax numpy.tri(N, M=None, k=0, dtype=<class 'float'>) Parameter R : It represents the number...

1 minute read.

numpy.ndarray.flat() in Python

numpy.ndarray.flat() in Python The numpy.ndarray.flat() returns a 1-D iterator over the array. This function is not a subclass of, Python’s built-in iterator object, otherwise it will act the same as a...

1 minute read.

numpy.empty_like() in Python

numpy.empty_like() in Python The empty_like() method of Python numpy class returns a new array with the same shape and type as the specified array. Syntax numpy.empty_like(prototype, dtype=None, order='K', subok=True) Parameters The numpy. empty_like() method consists of four parameters,...

1 minute read.

numpy.delete() in Python

numpy.delete() in Python The numpy.delete() function returns a new array with sub-arrays along an axis deleted. For 1-D array, this function returns those entries which are not returned by arr[obj]. Syntax numpy.delete(arr, obj, axis=None) Parameter arr: This parameter returns...

2 minutes read.

numpy.ones_like() in Python

numpy.ones_like() in Python The one_like() method of Python numpy class returns an array of ones with the same shape and type as the specified array. Syntax numpy.ones_like(a, dtype=None, order='K', subok=True) Parameter The numpy.ones_like() method consists of four parameters,...

1 minute read.

numpy.zeros_like() in Python

Numpy.zeros_like() in Python The zeros_like() method of Python numpy class returna an array of zeros with the same shape and type as that of specified array. Syntax numpy.zeros_like(a, dtype=None, order='K', subok=True) Parameter The numpy.zeros_like() method consists of four...

1 minute read.

numpy.concatenate() in Python

numpy.concatenate() in Python The numpy.concatenate() function joins a sequence of arrays along an existing axis. Syntax numpy.concatenate((a1, a2, ...), axis=0, out=None) Parameter a1, a2, … : This parameter represents the sequence of the array where they must have the same shape,...

1 minute read.

numpy.meshgrid() in Python

numpy.meshgrid() in Python The meshgrid() function of Python numpy class returns the coordinate matrices from coordinate vectors. Syntax numpy.meshgrid(*xi, **kwargs) Parameter The numpy.meshgrid() function consists of four parameters which are as follow: x1, x2,…, xn: This parameter signifies...

2 minutes read.

numpy.squeeze() in Python

numpy.squeeze() in Python The numpy.squeeze() function removes single-dimensional entries from the shape of an array. Syntax numpy.squeeze(a, axis=None) Parameter The numpy.squeeze() function has three parameters which are as follows: a: It represents the Input data. axis: This parameter signifies an int or...

1 minute read.