Saturday, September 21, 2019

Python NdArray

Ndarray:

The most important object defined in NumPy is an N-dimensional array type called ndarray. It describes the collection of items of the same type. Items in the collection can be accessed using a zero-based index.

The ndarray object consists of contiguous one-dimensional segment of computer memory, combined with an indexing scheme that maps each item to a location in the memory block. The memory block holds the elements in a row-major order (C style) or a column-major order (FORTRAN or MatLab style).

Creation:

numpy.array(object,

                                       dtype = None,

                                       copy = True,

                                       order = None,

                                       ndmin = 0)

Object : Any object exposing the array interface method returns an array, or any (nested) sequence.

Dtype :Desired data type of array, optional

Copy: Optional. By default (true), the object is copied

Order: C (row major) or F (column major) or A (any) (default)

Ndmin: Specifies minimum dimensions of resultant array

Attributes:

ndarray.ndim: The number of axes (dimensions) of the array.

ndarray.shape: The dimensions of the array. This is a tuple of integers indicating the size of the array in each dimension. For a matrix with nrows and m columns, shape will be (n,m). The length of the shape tuple is therefore the number of axes, ndim.

ndarray.size: The total number of elements of the array. This is equal to the product of the elements of shape.

ndarray.dtype: An object describing the type of the elements in the array. One can create or specify dtype’s using standard Python types. Additionally, NumPy provides types of its own. numpy.int32, numpy.int16, and numpy.float64 are some examples.

ndarray.itemsize: The size in bytes of each element of the array. For example, an array of elements of type float64 has itemsize 8 (=64/8), while one of type complex32 has itemsize 4 (=32/8). It is equivalent to ndarray.dtype.itemsize.

ndarray.data: tTe buffer containing the actual elements of the array. Normally, we won’t need to use this attribute because we will access the elements in an array using indexing facilities.

Data Types:

The following examples define a structured data type called student with a string field 'name', an integer field 'age' and a float field 'marks'. This dtype is applied to ndarray object.

Import numpy as np

dt = np.dtype(np.int32)

Student = np.dtype([('name','S20'), ('age', 'i1'), ('marks', 'f4')])

print student

Each built-in data type has a character code that uniquely identifies it

 

 

No comments:

Post a Comment

ML-Model DecisionTree Example-IncomePrediction

DecisionTree -- IncomePrediction Decision Tree: Income Prediction ¶ In this l...