Experiment 1, Numpy for data processing

1. The purpose of the experiment

  1. Understand the basic functions of the numpy library
  2. Master the operations and operations of arrays in the Numpy library

2. Experimental tools:

  1. Anaconda
  2. Numpy
  3. python

3. Introduction to Numpy

Numpy's full English name is Numerical Python, which refers to Python's third-party library for numerical computing. The feature of Numpy is that it expands the built-in array types in Python to support higher-dimensional array and matrix operations, as well as richer mathematical functions. Numpy is one of the most important libraries in Scipy.org. It is also used as the core computing library by well-known third-party libraries such as Pandas and Matplotlib.
NumPy (Numeric Python) provides many advanced numerical programming tools, such as: matrix data types, vector processing, and sophisticated arithmetic libraries. Produced for rigorous digital processing. Mostly used by many large financial companies, as well as core scientific computing organizations such as Lawrence Livermore, NASA uses it to handle some tasks that were originally done using C++, Fortran or Matlab.
Numpy includes: 1. A powerful N-dimensional array object Array; 2. A relatively mature (broadcast) function library; 3. A toolkit for integrating C/C++ and Fortran code; 4. Practical linear algebra, Fourier Leaf transforms and random number generation functions. It is more convenient to use Numpy with the sparse matrix operation package scipy.

Fourth, the experimental content

1. Array creation (create all 0 array, all 1 array, random number array)

import numpy as np
arr = np.zeros((5,),dtype=np.int)
print(arr)
arr1 = np.ones((5,),dtype=np.int)
print(arr1)
arr2 =np.random.randint(0,10,(2,5))
print(arr2)

2. Attributes of the array (view the dimensions of the array, the number of array elements)

import numpy as np
arr = np.array([[1,2,3,4,5],[1,4,5,6,7]])
print("the dimensions of the array:")
print(arr.shape)
print("number of array elements:")
print(arr.size)

3. Dimension operation of arrays (change the rows of the array into columns, return the last element, return the 2nd to 4th elements, and return the array in reverse order)

import numpy as np
arr = np.array([[1,2,3,4,5],[1,4,5,6,7]])
print(arr.T)
print(arr[-1])
a = np.arange(9)
print(a)
print(a[-1])
print(a[2:4])
print(a[::-1])

4. Merge of arrays (horizontal merge, vertical merge, depth merge of arrays)

import numpy as np
a = np.arange(9).reshape(3,3)
b = np.arange(9,18).reshape(3,3)
print(a)
print(b)
print("merge horizontally:")
print(np.hstack((a,b)))
print("vertical merge:")
print(np.vstack((a,b)))
print("deep merge:")
print(np.dstack((a,b)))

5. Splitting of arrays (horizontal splitting, vertical splitting, depth splitting of arrays)

import numpy as np
a = np.arange(9).reshape(3,3)
print(a)
print("split horizontally:")
print(np.hsplit(a,3))
print("vertical split:")
print(np.vsplit(a,3))
a = np.arange(27).reshape(3,3,3)
print("deep split:")
print(np.dsplit(a,3))

6. Array operations (four arithmetic operations with constants, four arithmetic operations with arrays, judging whether the arrays are equal)

import numpy as np
a = np.arange(4,dtype=np.float32).reshape(2,2)
b = np.arange(4,8,dtype=np.float32).reshape(2,2)
print("array a:",a)
print("array b",b)
print("Four arithmetic operations on arrays and constants:")
print(a+2)
print(a-2)
print(a2)
print(a//2)
print("Four arithmetic operations on arrays and arrays")
print(a+b)
print(a-b)
print(ab)
print(a//b)
print("Check if arrays are equal:")
(a==b).all()

7. Common functions of arrays (sum, product, average, maximum, minimum, element replacement, variance, standard deviation of all elements of an array)

import numpy as np
a = np.array([3,2,4])
print("all elements and:",a.sum())
print("product of all elements:",a.prod())
print("Arithmetic mean of all elements:",a.mean())
print("the maximum value of all elements:",a.max())
print("Minimum of all elements:",a.min())
#Elements less than 3 are replaced with 3, elements greater than 4 are replaced with 4
print("element replacement:",a.clip(3,4))
print("variance:",a.var())
print("standard deviation:",a.std())

Tags: Python Machine Learning Data Mining numpy

Posted by ReD_BeReT on Sat, 08 Oct 2022 12:18:00 +1030