Data Science for Fundamental Python Library-NumPy

ANIL NEBİ ŞENTÜRK
5 min readMar 9, 2021
NumPY

NumPy (Numerical Python) is a math library that allows us to do scientific calculations quickly. Numpy is based on numpy arrays. Numpy arrays are similar to python lists but are more useful than python lists in terms of speed and functionality. Also, unlike python lists, Numpy arrays must be homogeneous, meaning all elements in the array must be of the same data type.

  1. Importing the Numpy library
    2) Basic Operations with Numpy
    3) Mathematical Operations with Numpy
    4) Working with Condition Expressions in Numpy
  2. Importing the Numpy library: import numpy as np

Basic Operations with Numpy
- Numpy array creation

# python list
python_list = [0, 1, 2, 3,]

# numpy array
numpy_array = np.array([0, 1, 2, 3,]

print(“Python list :”)
print(python_list)

print(“Numpy sequence:”)
print(numpy_array)

Output:
Python list :
[0, 1, 2, 3,]

Numpy sequence:
[0 1 2 3]

When we look at the example above, we see that there is not much difference between the python list and numpy arrays. However, since it provides the opportunity to work with multidimensional arrays (with matrices) and is faster than python lists, the Numpy library is usually used in the field of data science.

  • Finding the size of the directory: ndarray.ndim:

numpy_array1 = np.array([0, 1, 2, 3, 4, 5]

print(numpy_array1.ndim):Output:
1

  • Finding the number of rows and columns of the array: ndarray.shape
    Returns a tupple object indicating how many rows and columns the .shape → numpy array object consists of.
  • numpy_array1 = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
    print(numpy_array1.shape)
    print(numpy_array1.ndim)
  • # A 1-dimensional array (vector) consisting of 10 elements.
    Output:
    (10,)
    one

numpy_array2 = np.array([[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])
print(numpy_array2.ndim)
print(numpy_array2.shape)

# A 2-dimensional array (matrix) consisting of 1 rows and 10 columns.
Output:
(1, 10)
2nd

  • Changing the number of rows and columns of the array: ndarray.reshape ()
    We can use the reshape () method when we want to reshape the numpy array, ie change the number of rows and columns. Let’s reshape our variable named numpy_array:
numpy_array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
print(numpy_array.reshape(1,10))
Output :
[[0 1 2 3 4 5 6 7 8 9]]print(numpy_array.reshape(10,1))
Output:
[[0]
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]]print(numpy_array.reshape(5,2))
Output:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]print(numpy_array.reshape(2,5))
Output:
[[0 1 2 3 4]
[5 6 7 8 9]]
  • np.arange ()
    np.arange () → Similar to the range () function in Python. Returns a numpy array containing numbers starting from the specified starting value and incrementing the number of steps each time up to the end value.
    Note: Note that the ending value is not included in the array.
    General use :
    np.arange (start, end, number of steps)
    np.arange (end) → In this use, the default starting value is 0 and the number of steps is accepted as 1. In other words, if we express it in the above format, we can write it as np.arange (0, ending, 1).
np.arange(0,10,3)Output:
array([0, 3, 6, 9])np.arange(10)
Output:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])np.arange(0,10,1)
Output:
array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
- select the elements of the array
Let's look at how to get the element or elements we need from the array.
Common usage: ndarray [rows, columns]
Note: ndarray [:,:] → using “:” here allows us to select all rows and columns.
numpy_array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
numpy_array = numpy_array.reshape(5,2)
print(numpy_array)
Output:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]#Dizinin herhangi bir satırını seçmek#1.satır
first_row = numpy_array[0]#1. ve 2. satır
first_and_second_rows = numpy_array[0:2]print(first_row)
print(first_and_second_rows)
Output:
[0 1]
[[0 1] [2 3]]
- Reverse the sequence:
numpy_array = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
numpy_array = numpy_array.reshape(5,2)print(numpy_array)
Output:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]print(numpy_array[::-1])
Output:
[[8 9]
[6 7]
[4 5]
[2 3]
[0 1]]
Create -0 matrix: np.zeros ()
np.zeros () → This function returns a matrix of 0's with the specified row and column.
print(np.zeros((5,4)))Output:
[[ 0 0 0 0]
[ 0 0 0 0]
[ 0 0 0 0]
[ 0 0 0 0]
[ 0 0 0 0]]
np.ones (): returns a matrix of given size 1s, similar to the zeros () function.
print(np.ones((3,3,3)))Output:[[[ 1 1 1] [ 1 1 1] [ 1 1 1]]
[[ 1 1 1] [ 1 1 1] [ 1 1 1]]
[[ 1 1 1] [ 1 1 1] [ 1 1 1]]]
- Create identity matrix: np.eye ()
np.eye () → The function that allows us to create an identity matrix of specified dimensions.
print(np.eye(4))Output:
[[ 1 0 0 0]
[ 0 1 0 0]
[ 0 0 1 0]
[ 0 0 0 1]]
3) Mathematical Operations with Numpy:
numpy_array = np.array([0,1, 2, 3, 4, 5, 6, 7, 8, 9])
numpy_array = numpy_array.reshape(5,2)
print(numpy_array)
Output:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]print(numpy_array.max())
Output:9print(numpy_array.min())
Output:0
print(numpy_array.sum())
Output:45
- calculate mean, median, variance and standard deviation:
numpy_array = np.array([0,1, 2, 3, 4, 5, 6, 7, 8, 9])print(numpy_array.mean())
Output:4.5print(np.median(numpy_array))
Output:4.5print(numpy_array.var())
Output:8.25print(numpy_array.std())
Output:2.8722813232690143
Arithmetic operations in matrices
As we remember from matrices, addition, subtraction, etc. In order to do operations, the row and column numbers of the matrices to be processed had to be equal.
print(numpy_array)
Output:
[[1 2 3]
[4 5 6]
[7 8 9]]print(numpy_array + numpy_array)Output:
[[ 2 4 6]
[ 8 10 12]
[14 16 18]]print(numpy_array - numpy_array)
Output:
[[0 0 0]
[0 0 0]
[0 0 0]]print(numpy_array * numpy_array)
Output:
[[ 1 4 9]
[16 25 36]
[49 64 81]]print(numpy_array / numpy_array)
Output:
[[1 1 1]
[1 1 1]
[1 1 1]]print(numpy_array + 5)
Output:
[[ 6 7 8]
[ 9 10 11]
[12 13 14]]print(numpy_array * 2)
Output:
[[ 2 4 6]
[ 8 10 12]
[14 16 18]]
- Transactions with special functions
Numpy provides the opportunity to work with simple mathematical operations such as addition and subtraction, as well as more complex functions such as trigonometric, logarithmic, exponential functions.
print(np.sin(numpy_array))
Output:
[[0.84147 0.9093 0.14112]
[-0.7568 -0.95892 -0.27942]
[0.65699 0.98936 0.41212]]print(np.cos(numpy_array))
Output:
[[0.5403 -0.41615 -0.98999]
[-0.65364 0.28366 0.96017]
[0.7539 -0.1455 -0.91113]] [print(np.sqrt(numpy_array))
Output:
[[ 1 1.4142 1.7321]
[ 2 2.2361 2.4495]
[2.6458 2.8284 3]]print(np.exp(numpy_array))
Output:
[[2.7183 7.3891 20.086]
[54.598 148.41 403.43]
[1096.6 2981 8103.1]]print(np.log(numpy_array))
Output:
[[ 0 0.69315 1.0986]
[1.3863 1.6094 1.7918]
[1.9459 2.0794 2.1972]]
Multiplication of matrices
Let's start this section by remembering that when two matrices are multiplied, the number of columns of the first matrix must be equal to the number of rows of the second matrix.
numpy_array = np.array([0,1, 2, 3, 4, 5, 6, 7, 8, 9])
numpy_array1 = numpy_array.reshape(5,2)
numpy_array2 = numpy_array.reshape(2,5)
print(np.dot(numpy_array1,numpy_array2))Output:
[[ 5 6 7 8 9]
[ 15 20 25 30 35]
[ 25 34 43 52 61]
[ 35 48 61 74 87]
[ 45 62 79 96 113]]
Transposition of the matrix:numpy_array = np.array([0,1, 2, 3, 4, 5, 6, 7, 8, 9])
numpy_array = numpy_array.reshape(5,2)print(numpy_array)
Output:
[[0 1]
[2 3]
[4 5]
[6 7]
[8 9]]
print(numpy_array.T)
Output:
[[0 2 4 6 8]
[1 3 5 7 9]]
4) Working with Condition Expressions in Numpy:
numpy_array = np.array([0,1, 2, 3, 4, 5, 6, 7, 8, 9])
boolean_array = numpy_array >= 5
print(boolean_array)
Output :
[False False False False False True True True True True]

--

--