1. Data Analysis - Representation Unit 1: Getting Started with NumPy Library

1. Dimensions of the data

  • Dimension: The organizational form of a set of data.

(Data dimension is a very important basic concept to form a specific relationship between data and express the meaning of various data.)

  • One-dimensional data: It is composed of ordered or unordered data in a peer-to-peer relationship, and is organized in a linear manner . Corresponds to concepts such as lists, arrays, and sets.

Lists and arrays are ordered structures of a set of data, and they are distinguished :

List: data types can be different

Arrays: same data type

  • Two-dimensional data: It is composed of multiple one-dimensional data and is a combination of one-dimensional data.

A table is a typical two-dimensional data, where the header is a part of the two-dimensional data

  • Multidimensional data: formed by extending one-dimensional or two-dimensional data in new dimensions.
  • High-dimensional data: use only the most basic binary relationship to show the complex structure between data.

       

  • Python representation of data dimensions

One-dimensional data: list (ordered) and set (unordered) types

Two-dimensional data: list type

Multidimensional Data: List Type

High-dimensional data: dictionary type or data representation format


2. NumPy array object: ndarray

NumPy is an open source Python scientific computing base library.

  • A powerful n-dimensional array object ndarray
  • broadcast function
  • Tools for integrating C/C++/Fortran code
  • Linear algebra, Fourier transform, random number generation and other functions

NumPy is the basis for data processing or scientific computing libraries such as SciPy and Pandas.

NumPy application: import numpy as np Although aliases can be omitted or changed, it is recommended to use aliases with the above conventions.

In the Python language, there is already a list type, why do we need an array type?

  • Array objects can remove the loops required for operations between elements, making a one-dimensional vector more like a single piece of data.
  • Setting a special array object, after optimization, can improve the operation speed of this type of application.

Observation : In scientific computing, the types of all data in a dimension are often the same.

  • Array objects use the same data type, which helps to save operation and storage space.

ndarray is a multidimensional array object consisting of two parts:

  • actual data
  • Metadata describing these data (data dimensions, data types, etc.)

The ndarray array generally requires that all elements are of the same type (homogeneous), and the array subscript starts from 0.

Properties of ndarray objects
Attributes illustrate

.it's me

rank, i.e. number of axes or number of dimensions
.shape The scale of the ndarray object, for a matrix, n rows and m columns
.size The number of ndarray object elements, which is equivalent to the value of n*m in .shape
.dtype The element type of the ndarray object
.itemsize The size of each element in the ndarray object, in bytes

Why does ndarray support so many element types?

 Contrast : Python syntax only supports 3 types of integers, floating point numbers and complex numbers

  • Scientific computing involves a lot of data and has high requirements for storage and performance
  • Fine definition of element types helps NumPy use storage space reasonably and optimize performance.
  • The fine definition of element types helps programmers to have a reasonable assessment of the program size.


3. Creation and transformation of ndarray array

How to create an ndarray array:

  • Create ndarray arrays from lists, tuples, etc. in Python.
  • Use functions in NumPy to create ndarray arrays, such as: arange, ones, zeros, etc.
  • Create ndarray array from byte stream (raw bytes).
  • Read a specific format from a file, creating an ndarray array.

1) Create ndarray arrays from lists, tuples, etc. in Python

        x = np.array(list/tuple)

        x = np.array(list/tuple, dtype=np.float32)

        When np.array() does not specify a dtype, NumPy will associate a dtype according to the data.

                corresponding to:

 2) Use functions in NumPy to create ndarray arrays, such as arange, ones, zeros, etc.

function illustrate
np.arange(n) Similar to the range() function, returns ndarray type, with elements from 0 to n-1
np.ones(shape) Generate an array of all 1s according to shape, shape is a tuple type
np.zeros(shape) Generate an array of all 0s according to shape, shape is a tuple type
np.full(shape,val) Generate an array according to shape, each element value is val

np.eye(n)

Create a square n*n identity matrix with 1s on the diagonal and 0s on the rest
np.ones_like(a) Generate an array of all 1s according to the shape of the array a
np.zeros_like(a) Generate an array of all 0s according to the shape of the array a
np.full_like(a,val) Generate an array according to the shape of the array a, each element value is val
np.linspace() Fill data at equal intervals according to the start and end data to form an array
np.concatenate() Merges two or more arrays into a new array

          

         

Transformation of ndarray arrays:

For the created ndarray array, dimension transformation and element type transformation can be performed on it.   

  • dimension transformation    
Dimension transformation of ndarray array
method illustrate
.reshape(shape) Returns an array of shape without changing the array elements, and the original array remains unchanged
.resize(shape) Same function as .reshape(), but modify the original array
.swapaxes(ax1,ax2) Swap two dimensions in the n dimensions of the array
.flatten() Reduce the dimension of the array, return the folded one-dimensional array, and keep the original array unchanged

          

  •  type conversion

        new_a = a.astype(new_type)

           

         The astype() method always creates a new array (a copy of the original array), even if the two types are the same.

        Conversion of ndarray array to list: ls = a.tolist()

        


 4. Operation of ndarray array

Array indexing and slicing

Indexing: the process of getting an element at a specific position in an array

Slicing: the process of obtaining a subset of array elements

Indexing and slicing of one-dimensional arrays : similar to Python's lists

start number: end number (not included); step size

Indexing of multidimensional arrays :

 Slicing of multidimensional arrays :


 5. Operation of ndarray array

Operations between arrays and scalars

Operations between arrays and scalars act on each element of the array

  

NumPy unary functions
function illustrate
np.abs(x)        np.fabs(x) Calculate the absolute value of each element of an array
np.sqrt(x) Calculates the square root of each element of an array
np.square(x) Calculates the square of each element of an array
np.log(x)        np.log10(x)        np.log2(x) Calculate the natural logarithm, base 10 logarithm, and base 2 logarithm of each element of an array
np.ceil(x)        np.floor(x) Calculate the ceiling value or floor value of each element of the array
np.rint(x) Calculates the rounded value of each element of an array
np.modf(x) Returns the fractional and integer parts of each element of an array as two separate arrays

np.cos(x)        np.cosh(x)

np.sin(x) np.sinh(x)

np.tan(x) np.tanh(x)

Computes ordinary and hyperbolic trigonometric functions for the elements of an array
np.exp(x) Calculate the index value of each element of the array
np.sign(x) Calculate the sign value of each element of the array, 1(+), 0, -1(-)

           

NumPy binary functions
function illustrate
+         -         *         /         ** The corresponding operation is performed on the elements of the two arrays

np.maximum()        np.fmax()

np.minmum()        np.fmin()

Element-wise max/min calculations
np.mod(x,y) element-wise modulo
np.copysign(x,y) Assign the symbol of each element in the array y to the corresponding element of the array x
>        <        >=        <=        ==        != Arithmetic comparison, yielding an array of booleans

   

The content of the article comes from: Chinese University MOOC - Teacher Songtian

Guess you like

Origin blog.csdn.net/panlan7/article/details/124478851