The basics of Python data analysis and Numpy

Chapter 1 Contents

What is data analysis

Briefly describe the basic process of data analysis

What are the advantages of Python for data analysis


Data analysis refers to the use of appropriate statistical analysis methods to analyze a large amount of collected data, summarize them, understand and digest them, in order to maximize the development of data functions and play the role of data. Data analysis is the process of studying and summarizing data in detail in order to extract useful information and form conclusions.

 

 It has a wide range of applications, is easy to use, has multiple directions of use, and has a fast development speed. It is a common language in the era of artificial intelligence and has strong general programming capabilities.

exercise:


Chapter 2 Contents 

What are vectorized operations?

What conditions need to be met to implement the array broadcasting mechanism?

Numpy overview


Numpy arrays do not need to be looped through to perform batch arithmetic operations on each element. This process is called vectorized operations. 

When an array is vectorized, it is required that the shapes of the arrays be equal. When arithmetic operations are performed on arrays with unequal shapes, a broadcast mechanism will appear, which will expand the array so that the shape attribute values ​​of the arrays are the same, and vectorized operations can be performed.

  1. Numerical Python, the numerical Python, complements the numerical computing capabilities that the Python language lacks.
  2. Numpy is the underlying library for other data analysis and machine learning libraries.
  3. Numpy is completely implemented in standard C language, and its operating efficiency is fully optimized.
  4. Numpy is open source and free.

numpy-basics

Represent n-dimensional arrays with objects of class np.ndarray

import numpy as np
ary = np.array([1, 2, 3, 4, 5, 6])
print(type(ary))

Creation of ndarray array object

np.array

import numpy as np
a = np.array([1, 2, 3, 4, 5, 6])
print(a)

np.arange(start value(0), stop value, step size(1))

import numpy as np
a = np.arange(0, 5, 1) 
print(a)
b = np.arange(0, 10, 2)
print(b)

np.zeros(Number of array elements, dtype='type' (integer or floating point, or Boolean))

import numpy as np
a = np.zeros(10)
print(a)
out:[0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

np.ones (number of array elements, dtype='type')

import numpy as np
a = np.ones((2,3))
print(a)
out: [[1. 1. 1.]
      [1. 1. 1.]]

Dimension operation of ndarray array object

import numpy as np
a = np.arange(1, 9)
print(a)		# [1 2 3 4 5 6 7 8]
b = a.reshape(2, 4)	#视图变维  : 变为2行4列的二维数组
print(b)
c = b.reshape(2, 2, 2) #视图变维    变为2页2行2列的三维数组
print(c)
d = c.ravel()	#视图变维	变为1维数组
print(d)

ndarray array slice operation

#The parameter setting of the array object slice is similar to the list section parameters
# step+: default cut from the beginning to the end
# step-: default cut from the end to the first
array object [start position: end position: step size, ... ]
#Default position step size: 1


Slicing operation of one-dimensional array

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

Slicing operations for multidimensional arrays

import numpy as np
a = np.arange(1, 28)
a.resize(3,3,3)
print(a)
#切出1页 
print(a[1, :, :])		
#切出所有页的1行
print(a[:, 1, :])		
#切出0页的1行1列
print(a[0, :, 1])		

Simple one-dimensional array combination scheme

a = np.arange(1,9)		#[1, 2, 3, 4, 5, 6, 7, 8]
b = np.arange(9,17)		#[9,10,11,12,13,14,15,16]
#把两个数组摞在一起成两行
c = np.row_stack((a, b))
print(c)
#把两个数组组合在一起成两列
d = np.column_stack((a, b))
print(d)

vectorized operations

NumPy Broadcasting

NumPy slicing and indexing

integer array index

boolean index

fancy index

 

 Transpose and axisymmetric arrays

 Operation:

Guess you like

Origin blog.csdn.net/qq_68890680/article/details/130140114