Preliminary knowledge of artificial intelligence: data analysis: numpy foundation of scientific computing

Keywords: Python

1. numpy scientific computing base

1.1 what is numpy

NumPy(Numerical Python) is an extension library of Python language, which supports a large number of dimension arrays and matrix operations. In addition, it also provides a large number of mathematical function libraries for array operations.

NumPy's predecessor, Numeric, was first developed by Jim Hugunin and other collaborators. In 2005, Travis Oliphant combined the features of another program library Numarray of the same nature in Numeric and added other extensions to develop NumPy. NumPy is open source and is maintained and developed by many collaborators.

NumPy is a very fast math library, mainly used for array calculation, including:

  • 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

1.2 create array (matrix)

# coding=utf-8
import numpy as np

#Use numpy Generated array,obtain ndarray Types
t1 = np.array([1,2,3,])
print(t1)
print(type(t1))

t2 = np.array(range(10))
print(t2)
print(type(t2))

t3 = np.arange(4,10,2)
print(t3)
print(type(t3))
print(t3.dtype)

Operation result

1.3 data type

Name describe
bool_ Boolean data type (True or False)
int_ Default integer type (similar to long, int32 or int64 in C)
intc Like the int type of C, it is generally int32 or int 64
intp Integer type used for index (similar to ssize'ut of C, still int32 or int64 in general)
int8 Bytes (- 128 to 127)
int16 Integer (- 32768 to 32767)
int32 Integer (- 2147483648 to 2147483647)
int64 Integer (- 9223372036854775808 to 9223372036854775807)
uint8 Unsigned integer (0 to 255)
uint16 Unsigned integer (0 to 65535)
uint32 Unsigned integer (0 to 4294967295)
uint64 Unsigned integer (0 to 18446744073709551615)
float_ Shorthand for float64 type
float16 Semi precision floating-point number, including: 1 sign bit, 5 fingers, 10 tails
float32 Single precision floating-point number, including: 1 sign bit, 8 fingers, 23 tails
float64 Double precision floating-point number, including: 1 sign bit, 11 fingers, 52 tails
complex_ Short for complex128 type, i.e. 128 bit complex
complex64 Complex number, indicating double 32-bit floating-point number (real part and imaginary part)
complex128 Complex number, indicating double 64 bit floating-point number (real part and imaginary part)
# coding=utf-8
import numpy as np
import random

# int8, int16, int32, int64 Four data types can use strings 'i1', 'i2','i4','i8' replace
t1 = np.array(range(1,4),dtype="i1")
print(t1)
print(t1.dtype)

##bool type in numpy
t2 = np.array([1,1,0,1,0,0],dtype=bool)
print(t2)
print(t2.dtype)

#Adjust data type
t3 = t2.astype("int8")
print(t3)
print(t3.dtype)

#numpy Decimal fraction
t4 = np.array([random.random() for i in range(10)])
print(t4)
print(t4.dtype)

t5 = np.round(t4,2)
print(t5)

Operation result:

1.4 shape of array

# coding=utf-8
import numpy as np

a = np.array([[3,4,5,6,7,8],[4,5,6,7,8,9]])
print(a)

#View array shapes
print(a.shape)

#Modify array shape
print(a.reshape(3,4))

#Original array shape unchanged
print(a.shape)

b = a.reshape(3,4)

print(b.shape)

print(b)

#Convert array to 1 dimension data
print(b.reshape(1,12))

print(b.flatten())

Operation result:

1.5 calculation of array and number

# coding=utf-8
import numpy as np

a = np.array([[3,4,5,6,7,8],[4,5,6,7,8,9]])

print(a)

#addition and subtraction
print(a+5)
print(a-5)

#Multiplication division
print(a*3)
print(a/3)

Operation result:

1.6 array and array calculation

# coding=utf-8
import numpy as np

a = np.array([[3,4,5,6,7,8],[4,5,6,7,8,9]])

b = np.array([[21,22,23,24,25,26],[27,28,29,30,31,32]])

#Addition and subtraction of arrays and arrays
print(a+b)
print(a-b)

#Multiplication and division of arrays and arrays
print(a*b)
print(a/b)

Operation result:

Calculation of different dimension arrays:

# coding=utf-8
import numpy as np

a = np.array([[3,4,5,6,7,8],[4,5,6,7,8,9]])

c = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])

#Array calculation of different dimensions
print(a*c)

Operation result:

# coding=utf-8
import numpy as np
#2 Array of Row 6 columns
a = np.array([[3,4,5,6,7,8],[4,5,6,7,8,9]])
#1 Array of Row 6 columns
c = np.array([1,2,3,4,5,6])

print(a-c)
print(a*c)

Operation result:

# coding=utf-8
import numpy as np
#2 Array of Row 6 columns
a = np.array([[3,4,5,6,7,8],[4,5,6,7,8,9]])
#1 Array of Row 6 columns
c = np.array([[1],[2]])

print(a+c)
print(a*c)
print(c*a)

Operation result:

Posted by FrozNic on Sun, 05 Apr 2020 02:08:36 -0700