Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
November 16, 2022 08:08 am GMT

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 

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

import numpy as np

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 arrayx = np.array([2,4,6,8,10])print(x)print(type(x))

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 arrayx = np.array([1, 3, 4, 6])print(x[0]) 

Output

1

Indexing in 2-D array

#indexing in 2-D arrayy = np.array([[1,4,6,9,0], [2,7,3,9,1]])print('2nd element on 1st row: ', y[0, 1]) 

output

4

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 arrayy = np.array([10,20,30,40,50,60])print(y[1:4]) 

Output

[20 30 40 ]

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) 

Output:

(2, 4)

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) 

Output:

[[ 2  4  6] [ 8 10 12] [14 16 18] [20 22 24]]

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) 

Output:

[[[ 2  4]  [ 6  8]  [10 12]] [[14 16]  [18 20]  [22 24]]]

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) 

Output:

[10 20 30 40 50 60]

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) 

Output:

[[10 20 30] [40 50 60]]

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)

Output:

[array([20, 40]), array([60, 70]), array([ 80, 100])]

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


Original Link: https://dev.to/marykariuki90/numpy-numerical-python-4ap2

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To