DEV Community

mary kariuki
mary kariuki

Posted on

Numpy (numerical python)

Numpy in python

Numpy is a python library used for working with the arrays.
Numpy stands for numerical python.it is used in performing wide variety of mathemetical operation on arrays.It also has functions for working in domain of linear algebra, fourier transform, and matrices. In python there are list that can operate as numpy but list are slow in process therefore numpy helps in solving the problem since NumPy arrays are stored at one continuous place in memory unlike lists, so processes can access and manipulate them very efficiently.
To install numpy we use the following command

pip install numpy 
Enter fullscreen mode Exit fullscreen mode

Having installed the numpy you have to import the the library using the following command.

import numpy as np
Enter fullscreen mode Exit fullscreen mode

Where np is an alias used for refering numpy.

creating an array

The array object in NumPy is called ndarray.We can create a NumPy ndarray object by using the array() function as shown below

import numpy as np

# creating an array

x = np.array([2,4,6,8,10])

print(x)

print(type(x))
Enter fullscreen mode Exit fullscreen mode

Note: an array can be into one dimension, two dimension or three dimension.

  • One dimensional array, is an array that has 0-D arrays as its elements

  • Two dimensional array, An array that has 1-D arrays as its elements

  • Three dimensional array, is An array that has 2-D arrays as its elements

There are many operations that takes place in numpy arrays which include

  • numpy array indexing
  • numpy array slicing
  • numpy array shape
  • numpy array reshape
  • numpy array split
  • numpy array join

Numpy array indexing

We access can array element through indexing by the help of an index number.

  • Indexing in 1-D array
#indexing in 1-D array

x = np.array([1, 3, 4, 6])

print(x[0]) 
Enter fullscreen mode Exit fullscreen mode

Output

1
Enter fullscreen mode Exit fullscreen mode

Indexing in 2-D array

#indexing in 2-D array

y = np.array([[1,4,6,9,0], [2,7,3,9,1]])

print('2nd element on 1st row: ', y[0, 1]) 

Enter fullscreen mode Exit fullscreen mode

output

4
Enter fullscreen mode Exit fullscreen mode

Numpy array slicing

slicing refers to taking elements from one given index to another given index.

#Slice elements from index 1 to index 5 from the following array

y = np.array([10,20,30,40,50,60])

print(y[1:4]) 
Enter fullscreen mode Exit fullscreen mode

Output

[20 30 40 ]
Enter fullscreen mode Exit fullscreen mode

Note:The result includes the start index, but excludes the end index

Numpy array shape

The shape of an array is the number of elements in each dimension.
NumPy arrays have an attribute called shape that returns a tuple with each index having the number of corresponding elements.

arr = np.array([[2,4,6,8], [8,8,3,4]])

print(arr.shape) 
Enter fullscreen mode Exit fullscreen mode

Output:

(2, 4)
Enter fullscreen mode Exit fullscreen mode

The example above returns (2, 4), which means the array has two rows and 4 columns.The first digit represent row and the second one represent columns

Numpy array reshape

Reshaping refers to changing the shape of an array where we have said that shape in array is the number of elements in each dimension.reshaping can be adding or removing number of elements in each dimension.

  • reshaping can be from 1-D to 2-D
z = np.array([2,4,6,8,10,12,14,16,18,20,22,24])

newarray = z.reshape(4, 3)

print(newarray) 
Enter fullscreen mode Exit fullscreen mode

Output:

[[ 2  4  6]
 [ 8 10 12]
 [14 16 18]
 [20 22 24]]
Enter fullscreen mode Exit fullscreen mode

Note: the array have been reshaped from 1-D array to 2-D array with 4rows and 3 columns

  • Reshaping 1-D to 3-D
z = np.array([2,4,6,8,10,12,14,16,18,20,22,24])

newarray = z.reshape(2, 3, 2)

print(newarray) 
Enter fullscreen mode Exit fullscreen mode

Output:

[[[ 2  4]
  [ 6  8]
  [10 12]]

 [[14 16]
  [18 20]
  [22 24]]]
Enter fullscreen mode Exit fullscreen mode

Note:The outermost dimension will have 2 arrays that contains 3 arrays, each with 2 elements

Numpy joining array

Joining means putting contents of two or more arrays in a single array.

x = np.array([10, 20, 30])

y = np.array([40, 50, 60])

arr1 = np.concatenate((x, y))

print(arr1) 
Enter fullscreen mode Exit fullscreen mode

Output:

[10 20 30 40 50 60]
Enter fullscreen mode Exit fullscreen mode

we can join the arrays using stack functions such as vstack which stacks along the columns.lets have an example,


x = np.array([10, 20, 30])

y = np.array([40, 50, 60])

arr2 = np.vstack((x,y))

print(arr2) 
Enter fullscreen mode Exit fullscreen mode

Output:

[[10 20 30]
 [40 50 60]]
Enter fullscreen mode Exit fullscreen mode

Numpy splitting array

Splitting is reverse operation of Joining.
Joining merges multiple arrays into one and Splitting breaks one array into multiple.To split the arrays we use array_split() function where we pass some arguments which are the array to be split and the number of split.

x = np.array([20,40, 60,70,80,100])

arr3 = np.array_split(x, 3)

print(arr3)
Enter fullscreen mode Exit fullscreen mode

Output:

[array([20, 40]), array([60, 70]), array([ 80, 100])]
Enter fullscreen mode Exit fullscreen mode

Note:The return value from the example above is an array containing three arrays.

Top comments (4)

Collapse
 
gathurum profile image
Gathuru_M

Nice work👏

Collapse
 
marykariuki90 profile image
mary kariuki

thank you

Collapse
 
andrea189699 profile image
Andrea189699

Good job Mary 👏

Collapse
 
marykariuki90 profile image
mary kariuki

thank you so much