Numpy – Ndarray

Array Defination

Array is a fundamental data structure used in programming to store a collection of elements of the same type.

Basic Array Defination by Numpy:

Fixed Types and Memory

Size of Any Object: Typically refers to the amount(bytes) of memory that the object occupies in the computer’s memory(RAM).

Byte: Byte is a memory measurement unit in electronics and computer science, generally consisting of 1 or 0 values across an 8-bit sequence, independent of the type of information being stored and recorded.

Dtypes

Dtype: In NumPy, dtype refers to the data type object, which represents the data type of elements stored in a NumPy array.

Signed Integers:

  • int8,
  • int16
  • int32
  • int64

Int dtypes provides holding all signed and unsigned number among maximum supported number of bit.

Unsigned Integers:

  • uint8
  • uint16
  • uint32
  • uint64

Unlike Int dtypes, UInt dtypes doesn’t support signed values such as -5, -2 …

Floating Point Numbers:

  • float16
  • float32
  • float64
  • float128

Float dtype provides storing all floating numbers among maximum supported range of defined dtype.

Complex Numbers:

  • complex64
  • complex128
  • complex256

We can define same array such as [1, 2, 3, 4, 5] different datatypes such as float64, int8, int32 but it will occupy different sizes in memory.

Boolean:

  • bool

Object:

  • object

Object dtype allows you to store arbitrary Python objects in the array.

String:

  • str

Memory

In programming languages all objects (such as list, dictionaries, arrays etc.) occupy memory in RAM. This occupation ratio directly related to data-type of array such as int8, int16 or complex64 etc.

Lets Define base array to measure number of bytes.

Default array dtype returns 20 bytes. Let’s change our dtype and measure change of number of bytes.

Let’s change our dtype to int16;

To sum up, numpy is mostly written in C so, we can define/change dtypes of any array. Change in dtype occurs change in memory. We need to choose the correct dtype for our ndarrays.

Accessing Arrays

Define base array to index and update:

Shape of array:

Basic Indexing

Indexing: Accessing individual elements within a data structure, such as an array, list, or string, using a numerical key.

Standart Index

In Python, counting starts from zero.

Unlike lists, numpy arrays are usually multi-dimensional. When indexing any n-dimensional array, we need to specify coordinates according to number of dimensions:

  1. 1D Array: [x]
  2. 2D Array: [x, y]
  3. 3D array: [x ,y ,z]

Let’s try Index array_1(defined above):

  • Indexing First Row:
  • Indexing Second Row
  • Indexing first element
  • Indexing Any Element
  • Indexing First Row
  • Indexing Any Row
  • Indexing Any Range

Reverse Index

In python, reverse indexing starts from -1 and decreases.

  • Indexing Last Row
  • Indexing Last Element
  • Indexing Any Element

Alter/Update Arrays

Numpy arrays are changeable. We can concatenate, remove, alter elements/lines.

Update Elements

We can update element by assigning new value to spesific index.

We can update row/column:

Insert Row/Column

We can add row or column.

Row Insertion:

Column Insertion:

Row/Column Deletion

Other Array Defination Types

Zeros Array

Numpy provides an array creation that filled with zeros.

Ones Array

Numpy provides an array creation that filled with ones.

Fixed Value Arrays

Numpy provides an array creation that filled with fixed value.

Indentity Matrix

Numpy provides an indentity array creation.

Range Based Arrays

Numpy provides array that made from range based

Step Based

In numpy, we can create arrays which has start point, end point and step size.

Piece Based

In numpy, we can generate an array of evenly spaced numbers over a specified interval.

Random Based Arrays

Numpy provides random based arrays.

randn

randn generates an array of random numbers drawn from a standard normal distribution (mean 0, standard deviation 1).

randint

randint generates random integers.

Copying Arrays

In NumPy, there’s a difference between assigning arrays and copying arrays. This difference could be crucial as it affects how changes made to one array affect another.

In the Above, we defined “array_1”, than we assigned “array_1” to “array_2”

If we update array_1 or array_2:

In the above we realized , if we assiagned any array to array: we’re just referencing array from memory. We can overcome this situation by using numpy’s copy() method:

ibrahim tunc
ibrahim tunc
Articles: 13

Leave a Reply

Your email address will not be published. Required fields are marked *