程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python -- common properties and methods of numpy

編輯:Python

numpy

  • numpy Array objects
    • Create an array object
    • Generate random number
    • Accessing arrays by index
    • Transform the shape of the array
  • numpy Matrices and general functions
    • establish numpy matrix
    • ufunc function
  • utilize numpy Conduct statistical analysis
    • read / Writing documents
    • Simple statistical analysis using functions
    • Common statistical functions
    • Statistical analysis of iris length data

numpy Array objects

Create an array object

  1. Array attribute
attribute explain ndim return int. Represents the dimensions of an array shape return tuple. Represents the size of an array , about n That's ok m Columns of the matrix , Shape is (n,m)size return int. Represents the total number of elements in the array , Equal to the product of array shape dtype return data-type. Describes the type of elements in an array itemsize return int. Represents the size of each element of the array ( In bytes )
  1. Array creation
    numpy.array(object, dtype=None, copy=True, order='K', subok=False, ndmin=0)
Parameter name explain object receive array. Represents the array you want to create . No default dtype receive data-type. Represents the data type required by the array . If not given , Select the minimum type required to save the object . The default is Nonendmin receive int. Specify the minimum dimension that the generated array should have . The default is None
  1. Array data type
type describe bool Boolean type stored in one bit ( The value is True or False)inti Integer whose precision is determined by the platform ( It's usually int32 or int64)int8 Integers , The scope is -128~127int16 Integers , The scope is -32768~32767int32 Integers , The scope is -231~232-1int64 Integers , The scope is -263~263-1uint8 Unsigned integer , Range 0~255uint16 Unsigned integer , Range 0~65535uint32 Unsigned integer , Range 0~232-1uint64 Unsigned integer , Range 0~264-1float16 Semi precision floating point number (16 position ), Among them 1 Bits indicate signs , use 5 Bitwise index , use 10 Bits represent mantissa float32 Single-precision floating-point (32 position ), Among them 1 Bits indicate signs , use 8 Bitwise index , use 23 Bits represent mantissa float64 or float Single-precision floating-point (64 position ), Among them 1 Bits indicate signs , use 11 Bitwise index , use 52 Bits represent mantissa complex64 The plural , Use two... Respectively 32 Floating point number represents real part and virtual part complex128 or complex The plural , Use two... Respectively 64 Floating point number represents real part and virtual part
  • example
import numpy as np
arr1 = np.array([1, 2, 3, 4])
print(' Array created arr1 by :', arr1)
arr2 = np.array([[1, 2, 3, 4], [4, 5, 6, 7], [7, 8, 9, 10]])
print(' Array created arr2 by :\n', arr2)
 Array created arr1 by : [1 2 3 4]
Array created arr2 by :
[[ 1 2 3 4]
[ 4 5 6 7]
[ 7 8 9 10]]
print(' The array dimension is :', arr2.shape)
print(' The array type is :', arr2.dtype)
print(' The number of array elements is :', arr2.size)
print(' The size of each element of the array is :', arr2.itemsize)
 The array dimension is : (3, 4)
The array type is : int32
The number of array elements is : 12
The size of each element of the array is : 4
arr2.shape=4,3
print(' To reset shape After arr2 by :\n', arr2)
# The order of the elements has not changed , Only the size of each axis is changed 
 To reset shape After arr2 by :
[[ 1 2 3]
[ 4 4 5]
[ 6 7 7]
[ 8 9 10]]
print(' Use arange The array created is :\n', np.arange(0, 1, 0.1))
print(' Use linspace The array created is :\n', np.linspace(0, 1, 12))
# What you create is a series of proportional numbers 
print(' Use logspace The array created is :\n', np.logspace(0, 2, 20))
# All arrays are 0
print(' Use zeros The array created is :\n', np.zeros((2, 3)))
# The main diagonal is all 1
print(' Use eye The array created is :\n', np.eye(3))
# The elements on the main diagonal 
print(' Use diag The array created is :\n', np.diag([1,2,3,4]))
# All arrays are 1
print(' Use ones The array created is :\n', np.ones((5,3)))
 Use arange The array created is :
[0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
Use linspace The array created is :
[0. 0.09090909 0.18181818 0.27272727 0.36363636 0.45454545
0.54545455 0.63636364 0.72727273 0.81818182 0.90909091 1. ]
Use logspace The array created is :
[ 1. 1.27427499 1.62377674 2.06913808 2.6366509
3.35981829 4.2813324 5.45559478 6.95192796 8.8586679
11.28837892 14.38449888 18.32980711 23.35721469 29.76351442
37.92690191 48.32930239 61.58482111 78.47599704 100. ]
Use zeros The array created is :
[[0. 0. 0.]
[0. 0. 0.]]
Use eye The array created is :
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Use diag The array created is :
[[1 0 0 0]
[0 2 0 0]
[0 0 3 0]
[0 0 0 4]]
Use ones The array created is :
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
# Array type conversion 
print(' Convert integer to floating point , The result of the conversion is :', np.float64(42))
print(' Convert floating point to integer , The result of the conversion is :', np.int8(42.0))
print(' Integer to Boolean , The result of the conversion is :', np.bool(42))
print(' Integer to Boolean , The result of the conversion is :', np.float64(0))
print(' Convert Boolean to floating point , The result of the conversion is :', np.float(True))
print(' Convert Boolean to floating point , The result of the conversion is :', np.float(False))
 Convert integer to floating point , The result of the conversion is : 42.0
Convert floating point to integer , The result of the conversion is : 42
Integer to Boolean , The result of the conversion is : True
Integer to Boolean , The result of the conversion is : 0.0
Convert Boolean to floating point , The result of the conversion is : 1.0
Convert Boolean to floating point , The result of the conversion is : 0.0
# Create data types 
df=np.dtype([("name",np.str_,40),("numitems",np.int64),("price",np.float64)])
print(' The data type is :', df)
 The data type is : [('name', '<U40'), ('numitems', '<i8'), ('price', '<f8')]
# View data type 
print(' The data type is :', df["name"])
print(' The data type is :', np.dtype(df["name"]))
# Custom array data 
itemz=np.array([("tomatoes",42,4.14),("cabbages",13,1.72)],dtype=df)
print(' Custom data is :',itemz)
 The data type is : <U40
The data type is : <U40
Custom data is : [('tomatoes', 42, 4.14) ('cabbages', 13, 1.72)]

Generate random number

random Modules often use random number generation functions

function explain seed Determine the seed of the random number generator permutaion Returns a random sequence or a range of random sequences shuffle Randomly arrange a sequence binomial A random number that produces a binomial distribution normal A random number that produces a normal distribution beta produce beta Random number of distribution chisquare A random number that produces a chi square distribution gamma produce gamma Random number of distribution uniform Produced in [0,1] Uniformly distributed random numbers in
print(' The generated random array is :',np.random.random(10))
print(' Generate uniformly distributed random arrays as :\n',np.random.rand(2,3))
print(' Generate a random array that obeys the normal distribution as :\n',np.random.randn(2,3))
print(' The random array that generates the given upper and lower bounds is :\n',np.random.randint(2,10,size=[2,5]))
 The generated random array is : [0.05478456 0.31793173 0.63195643 0.96141967 0.00333223 0.72768221
0.30489522 0.90413895 0.15791078 0.99559445]
Generate uniformly distributed random arrays as :
[[0.66725553 0.44391885 0.95413037]
[0.77064322 0.53726875 0.26902613]]
Generate a random array that obeys the normal distribution as :
[[ 0.95840647 -0.51848368 0.68529844]
[-0.61515571 0.37733786 0.43860996]]
The random array that generates the given upper and lower bounds is :
[[5 5 7 9 4]
[2 3 5 9 4]]

Accessing arrays by index

  1. Index of one dimensional array – And list The indexing method is consistent
arr=np.arange(10)
print(' Integer as index , The index result is :',arr[5])
print(' Index the range , barring arr[5], The index result is :',arr[3:5])
print(' Omit the starting index for indexing , The index result is :',arr[:5])
print(' Negative numbers are indexed , The index result is :',arr[-1])
arr[2:4]=100,101
print(' Subscripts are used to modify elements , The array result is :',arr)
print(' Step size as index , The index result is :',arr[1:-1:2])
print(' Negative step numbers are indexed , The start subscript must be greater than the end subscript , The index result is :',arr[5:1:-2])
 Integer as index , The index result is : 5
Index the range , barring arr[5], The index result is : [3 4]
Omit the starting index for indexing , The index result is : [0 1 2 3 4]
Negative numbers are indexed , The index result is : 9
Subscripts are used to modify elements , The array result is : [ 0 1 100 101 4 5 6 7 8 9]
Step size as index , The index result is : [ 1 101 5 7]
Negative step numbers are indexed , The start subscript must be greater than the end subscript , The index result is : [ 5 101]
  1. Index of multidimensional array – Each dimension has an index , The indexes of each dimension are separated by commas
arr=np.array([[1,2,3,4,5],[4,5,6,7,8],[7,8,9,10,11]])
print(' The two-dimensional array created is :\n',arr)
print(' Index No 0 In line 3 Column sum 4 The elements of the column , The index result is :\n',arr[0,3:5])
print(' Index No 2 and 3 In line 3~5 The elements of the column , The index result is :\n',arr[1:,2:])
print(' Index No 2 The elements of the column , The index result is :',arr[:,2])
print(' Take two integers from the corresponding positions of the two sequences to form the subscript , The index result is :',arr[[(0,1,2),(1,2,3)]])
print(' Index No 2、3 In line 0、2、3 The elements of the column , The index result is :\n',arr[1:,(0,2,3)])
mask=np.array([1,0,1],dtype=np.bool)
print('mask Is a Boolean array , Index No 1、3 In line 2 The elements of the column , The index result is :',arr[mask,2])
 The two-dimensional array created is :
[[ 1 2 3 4 5]
[ 4 5 6 7 8]
[ 7 8 9 10 11]]
Index No 0 In line 3 Column sum 4 The elements of the column , The index result is :
[4 5]
Index No 2 and 3 In line 3~5 The elements of the column , The index result is :
[[ 6 7 8]
[ 9 10 11]]
Index No 2 The elements of the column , The index result is : [3 6 9]
Take two integers from the corresponding positions of the two sequences to form the subscript , The index result is : [ 2 6 10]
Index No 2、3 In line 0、2、3 The elements of the column , The index result is :
[[ 4 6 7]
[ 7 9 10]]
mask Is a Boolean array , Index No 1、3 In line 2 The elements of the column , The index result is : [3 9]

Transform the shape of the array

  • Change array shape instance
arr=np.arange(12)
print(' The one-dimensional array created is :\n',arr)
print(' The new one-dimensional array is :\n',arr.reshape(3,4))
print(' The new one-dimensional array is :',arr.reshape(3,4).ndim)
 The one-dimensional array created is :
[ 0 1 2 3 4 5 6 7 8 9 10 11]
The new one-dimensional array is :
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
The new one-dimensional array is : 2
  • Flatten array instances
# Use ravel The function flattens the array 
arr=np.arange(12).reshape(3,4)
print(' The two-dimensional array created is :\n',arr)
print('ravel After flattening the function array :',arr.ravel())
 The two-dimensional array created is :
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
ravel After flattening the function array : [ 0 1 2 3 4 5 6 7 8 9 10 11]
# Use flatten The function flattens the array 
print('flatten The function array is horizontally flattened to :',arr.flatten())
print('flatten The function array is vertically flattened to :',arr.flatten('F'))
flatten The function array is horizontally flattened to : [ 0 1 2 3 4 5 6 7 8 9 10 11]
flatten The function array is vertically flattened to : [ 0 4 8 1 5 9 2 6 10 3 7 11]
  • Combine array instances
# Use hstack Function array horizontal combination 
arr1 = np.arange(12).reshape(3,4)
print(' Array created 1 by :\n',arr1)
arr2=arr1*3
print(' Array created 2 by :\n',arr2)
print('hstack Function to horizontally combine an array :\n',np.hstack((arr1,arr2)))
 Array created 1 by :
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
Array created 2 by :
[[ 0 3 6 9]
[12 15 18 21]
[24 27 30 33]]
Horizontal combination :
[[ 0 1 2 3 0 3 6 9]
[ 4 5 6 7 12 15 18 21]
[ 8 9 10 11 24 27 30 33]]
# Use vstack Function array vertical combination 
print('vstack Function to vertically combine arrays :\n',np.vstack((arr1,arr2)))
vstack Vertical combination :
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[ 0 3 6 9]
[12 15 18 21]
[24 27 30 33]]
print('concatenate Function to horizontally combine an array :\n',np.concatenate((arr1,arr2),axis=1))
print('concatenate Function to vertically combine arrays :\n',np.concatenate((arr1,arr2),axis=0))
concatenate Function to horizontally combine an array :
[[ 0 1 2 3 0 3 6 9]
[ 4 5 6 7 12 15 18 21]
[ 8 9 10 11 24 27 30 33]]
concatenate Function to vertically combine arrays :
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[ 0 3 6 9]
[12 15 18 21]
[24 27 30 33]]
  • Split array instances
arr=np.arange(16).reshape(4,4)
print(' The two-dimensional array created is :',arr)
print('hsplit Function to split the array horizontally :\n',np.hsplit(arr,2))
print('vsplit Function to split the array vertically :\n',np.vsplit(arr,2))
 The two-dimensional array created is : [[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]]
hsplit Function to split the array horizontally :
[array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]), array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])]
vsplit Function to split the array vertically :
[array([[0, 1, 2, 3],
[4, 5, 6, 7]]), array([[ 8, 9, 10, 11],
[12, 13, 14, 15]])]
print('split Function to split the array horizontally :\n',np.split(arr,2,axis=1))
print('split Function to split the array vertically :\n',np.split(arr,2,axis=0))
split Function to split the array horizontally :
[array([[ 0, 1],
[ 4, 5],
[ 8, 9],
[12, 13]]), array([[ 2, 3],
[ 6, 7],
[10, 11],
[14, 15]])]
split Function to split the array vertically :
[array([[0, 1, 2, 3],
[4, 5, 6, 7]]), array([[ 8, 9, 10, 11],
[12, 13, 14, 15]])]

numpy Matrices and general functions

establish numpy matrix

Matrix specific attributes and descriptions

attribute explain T Return to the transposition of itself H Returns the conjugate transposition of itself I Return the inverse matrix of itself A Returns a view of a two-dimensional array of its own data
  • Create a matrix instance
# Use mat Function and matrix Function to create a matrix 
import numpy as pd
matr1=np.mat("1 2 3;4 5 6;7 8 9")
print('mat Create a matrix for :\n',matr1)
matr2=np.mat([[1,2,3],[4,5,6],[7,8,9]])
print('matrix Create a matrix for :\n',matr2)
mat Create a matrix for :
[[1 2 3]
[4 5 6]
[7 8 9]]
matrix Create a matrix for :
[[1 2 3]
[4 5 6]
[7 8 9]]
arr1=np.eye(3)
print(' Array created 1 by :\n',arr1)
arr2=3*arr1
print(' Array created 2 by :\n',arr2)
print('bmat Create a matrix for :\n',np.bmat("arr1 arr2;arr1 arr2"))
 Array created 1 by :
[[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
Array created 2 by :
[[3. 0. 0.]
[0. 3. 0.]
[0. 0. 3.]]
bmat Create a matrix for :
[[1. 0. 0. 3. 0. 0.]
[0. 1. 0. 0. 3. 0.]
[0. 0. 1. 0. 0. 3.]
[1. 0. 0. 3. 0. 0.]
[0. 1. 0. 0. 3. 0.]
[0. 0. 1. 0. 0. 3.]]
  • Matrix operation example
# Matrix operations 
matr1=np.mat("1 2 3;4 5 6;7 8 9")
print(' Create a matrix for :\n',matr1)
matr2=matr1*3
print(' Create a matrix for :\n',matr2)
print(' The result of matrix addition is :\n',matr1+matr2)
print(' The result of matrix subtraction is :\n',matr1-matr2)
print(' The result of matrix multiplication is :\n',matr1*matr2)
print(' The result of multiplying the corresponding elements of the matrix is :\n',np.multiply(matr1,matr2))
 Create a matrix for :
[[1 2 3]
[4 5 6]
[7 8 9]]
Create a matrix for :
[[ 3 6 9]
[12 15 18]
[21 24 27]]
The result of matrix addition is :
[[ 4 8 12]
[16 20 24]
[28 32 36]]
The result of matrix subtraction is :
[[ -2 -4 -6]
[ -8 -10 -12]
[-14 -16 -18]]
The result of matrix multiplication is :
[[ 90 108 126]
[198 243 288]
[306 378 450]]
The result of multiplying the corresponding elements of the matrix is :
[[ 3 12 27]
[ 48 75 108]
[147 192 243]]
# View matrix properties 
print(' The result of matrix transpose is :\n',matr1.T)
print(' The result of matrix conjugate transpose is :\n',matr1.H)
try:
print(matr1.I)
except:
print(" Singular matrix , The inverse matrix does not exist !")
print(' The two-dimensional array result of the matrix is ( Return to the matrix view ):\n',matr1.A)
 The result of matrix transpose is :
[[1 4 7]
[2 5 8]
[3 6 9]]
The result of matrix conjugate transpose is :
[[1 4 7]
[2 5 8]
[3 6 9]]
Singular matrix , The inverse matrix does not exist !
The two-dimensional array result of the matrix is ( Return to the matrix view ):
[[1 2 3]
[4 5 6]
[7 8 9]]

ufunc function

ufunc function : The generic function , A function that can operate on all elements in an array , Operate on arrays ; When repeating an array , Use ufunc Function is better than using math The functions in the library are much more efficient .

  • Commonly used ufunc function : arithmetic 、 Comparison operation and logic operation .
  • ufunc Broadcast mechanism of function
# Broadcast mechanism of one-dimensional array 
arr1=np.array([[0,0,0],[1,1,1],[2,2,2],[3,3,3]])
print(' Array created 1 by :\n',arr1)
print(' Array 1 Of shape by :\n',arr1.shape)
arr2=np.array([1,2,3])
print(' Array created 2 by :\n',arr2)
print(' Array 2 Of shape by :\n',arr2.shape)
print(' The result of adding arrays is :\n',arr1+arr2)
 Array created 1 by :
[[0 0 0]
[1 1 1]
[2 2 2]
[3 3 3]]
Array 1 Of shape by :
(4, 3)
Array created 2 by :
[1 2 3]
Array 2 Of shape by :
(3,)
The result of adding arrays is :
[[1 2 3]
[2 3 4]
[3 4 5]
[4 5 6]]
# Broadcasting mechanism of two-dimensional array 
arr1=np.array([[0,0,0],[1,1,1],[2,2,2],[3,3,3]])
print(' Array created 1 by :\n',arr1)
print(' Array 1 Of shape by :\n',arr1.shape)
arr2=np.array([1,2,3,4]).reshape((4,1))
print(' Array created 2 by :\n',arr2)
print(' Array 2 Of shape by :\n',arr2.shape)
print(' The result of adding arrays is :\n',arr1+arr2)
 Array created 1 by :
[[0 0 0]
[1 1 1]
[2 2 2]
[3 3 3]]
Array 1 Of shape by :
(4, 3)
Array created 2 by :
[[1]
[2]
[3]
[4]]
Array 2 Of shape by :
(4, 1)
The result of adding arrays is :
[[1 1 1]
[3 3 3]
[5 5 5]
[7 7 7]]

utilize numpy Conduct statistical analysis

read / Writing documents

  1. save Save data in binary format ,load Function to read data from a binary file .
    np.save(file,arr,allow_pickle=True,fix_imports=True)
  • example
# Binary storage 
import numpy as np
arr=np.arange(100).reshape(10,10)
np.save("./save_arr",arr)
print(' The saved array is :\n',arr)
 The saved array is :
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
# Multiple arrays store 
arr1=np.array([[1,2,3],[4,5,6]])
arr2=np.arange(0,1.0,0.1)
np.savez('./savez_arr',arr1,arr2)
print(' Saved array 1 by :\n',arr1)
print(' Saved array 2 by :',arr2)
 Saved array 1 by :
[[1 2 3]
[4 5 6]]
Saved array 2 by : [0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
# Binary file read 
loaded_data = np.load("./save_arr.npy")
print(' The array read is :\n',loaded_data)
 The array read is :
[[ 0 1 2 3 4 5 6 7 8 9]
[10 11 12 13 14 15 16 17 18 19]
[20 21 22 23 24 25 26 27 28 29]
[30 31 32 33 34 35 36 37 38 39]
[40 41 42 43 44 45 46 47 48 49]
[50 51 52 53 54 55 56 57 58 59]
[60 61 62 63 64 65 66 67 68 69]
[70 71 72 73 74 75 76 77 78 79]
[80 81 82 83 84 85 86 87 88 89]
[90 91 92 93 94 95 96 97 98 99]]
# Read a file containing multiple arrays 
loaded_data1=np.load('./savez_arr.npz')
print(' Read array 1 by :\n',loaded_data1['arr_0'])
print(' Read array 2 by :\n',loaded_data1['arr_1'])
  1. savetxt The array() function writes an array to a text file separated by some delimiter .
    np.savetxt(fname,X,fmt='%.18e',delimiter=' ',newline='\n',header='',footer='',comments='# '
    The first parameter is the filename fname, The second parameter X For array data , The fourth parameter is the data separator felimiter.
    loadtxt Function performs the opposite number of operations , That is, load the file into a two-dimensional array .
 Read array 1 by :
[[1 2 3]
[4 5 6]]
Read array 2 by :
[0. 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9]
arr=np.arange(0,12,0.5).reshape(4,-1)
print(' The array created is :',arr)
 The array created is : [[ 0. 0.5 1. 1.5 2. 2.5]
[ 3. 3.5 4. 4.5 5. 5.5]
[ 6. 6.5 7. 7.5 8. 8.5]
[ 9. 9.5 10. 10.5 11. 11.5]]
# fmt ="%d" Means to save as an integer 
np.savetxt("./arr.txt",arr,fmt="%d",delimiter=",")
# You also need to specify comma separation when reading 
loaded_data=np.loadtxt("./arr.txt",delimiter=",")
print(" The array read is :",loaded_data)
 The array read is : [[ 0. 0. 1. 1. 2. 2.]
[ 3. 3. 4. 4. 5. 5.]
[ 6. 6. 7. 7. 8. 8.]
[ 9. 9. 10. 10. 11. 11.]]
# Use genfromtxt Function to read an array 
loaded_data=np.genfromtxt("./arr.txt",delimiter=",")
print(' The array read is :',loaded_data)
 The array read is : [[ 0. 0. 1. 1. 2. 2.]
[ 3. 3. 4. 4. 5. 5.]
[ 6. 6. 7. 7. 8. 8.]
[ 9. 9. 10. 10. 11. 11.]]

Simple statistical analysis using functions

  1. Sort –sort、argsort、lexsort
  • sort example
np.random.seed(42)
arr=np.random.randint(1,10,size=10)
print(' The array created is :',arr)
# Direct sort 
arr.sort()
print(' The sorted array is :',arr)
arr=np.random.randint(1,10,size=(3,3))
print(' The array created is :\n',arr)
arr.sort(axis=1)
print(' After sorting along the horizontal axis, the array is :\n',arr)
arr.sort(axis=0)
print(' After sorting along the vertical axis, the array is :\n',arr)
 The array created is : [7 4 8 5 7 3 7 8 5 4]
The sorted array is : [3 4 4 5 5 7 7 7 8 8]
The array created is :
[[8 8 3]
[6 5 2]
[8 6 2]]
After sorting along the horizontal axis, the array is :
[[3 8 8]
[2 5 6]
[2 6 8]]
After sorting along the vertical axis, the array is :
[[2 5 6]
[2 6 8]
[3 8 8]]
  • argsort example
arr=np.array([2,3,6,8,0,7])
print(' The array created is :',arr)
print(' The sorted array is :',arr.argsort())
# The return value is the subscript of the reordering value 
 The array created is : [2 3 6 8 0 7]
The sorted array is : [4 0 1 2 5 3]
  • lexsort example
a=np.array([3,2,6,4,5])
b=np.array([50,30,40,20,10])
c=np.array([400,300,600,100,200])
# lexsort Only one parameter is received , namely abc
# When sorting multiple key values, it is calculated according to the last incoming data 
d=np.lexsort((a,b,c))
print(' The sorted array is :',list(zip(a[d],b[d],c[d])))
 The sorted array is : [(4, 20, 100), (5, 10, 200), (2, 30, 300), (3, 50, 400), (6, 40, 600)]
  1. De duplication and duplicate data
  • unique De duplication instance
names=np.array([' Xiao Ming ',' floret ',' Xiao Huang ',' Xiao Ming ',' floret ',' Xiaolan ',' The small white '])
print(' Array created names by :',names)
print(' Array after de duplication names by :',np.unique(names))
# Follow unique equivalent python Code implementation process 
print('python The array after Code de duplication is :',sorted(set(names)))
ints=np.array([1,2,3,4,4,5,6,6,7,7,7,8,9,10])
print(' Array created ints by :',ints)
print(' Array after de duplication ints by :',np.unique(ints))
 Array created names by : [' Xiao Ming ' ' floret ' ' Xiao Huang ' ' Xiao Ming ' ' floret ' ' Xiaolan ' ' The small white ']
Array after de duplication names by : [' Xiaolan ' ' Xiao Ming ' ' The small white ' ' floret ' ' Xiao Huang ']
python The array after Code de duplication is : [' Xiaolan ', ' Xiao Ming ', ' The small white ', ' floret ', ' Xiao Huang ']
Array created ints by : [ 1 2 3 4 4 5 6 6 7 7 7 8 9 10]
Array after de duplication ints by : [ 1 2 3 4 5 6 7 8 9 10]
  • tile Function to realize data duplication
arr=np.arange(5)
print(' The array created is :',arr)
print(' The array after repetition is :',np.tile(arr,3))
 The array created is : [0 1 2 3 4]
The array after repetition is : [0 1 2 3 4 0 1 2 3 4 0 1 2 3 4]
  • repeat Function to realize data duplication
np.random.seed(42)
arr=np.random.randint(0,10,size=(3,3))
print(' The array created is :',arr)
print(' Repeat elements by line , The array after repetition is :\n',arr.repeat(2,axis=0))
print(' Repeat elements by column , The array after repetition is :\n',arr.repeat(2,axis=1))
 The array created is : [[6 3 7]
[4 6 9]
[2 6 7]]
Repeat elements by line , The array after repetition is :
[[6 3 7]
[6 3 7]
[4 6 9]
[4 6 9]
[2 6 7]
[2 6 7]]
Repeat elements by column , The array after repetition is :
[[6 6 3 3 7 7]
[4 4 6 6 9 9]
[2 2 6 6 7 7]]

Common statistical functions

arr=np.arange(20).reshape(4,5)
print(' The array created is :\n',arr)
print(' The sum of arrays is :',np.sum(arr))
print(' The sum of the vertical axis array is :',arr.sum(axis=0))
print(' The sum of the horizontal axis array is :',arr.sum(axis=1))
print(' The average value of the array is :',np.mean(arr))
print(' The mean value of the vertical axis array is :',arr.mean(axis=0))
print(' The mean value of the horizontal axis array is :',arr.mean(axis=1))
print(' The standard deviation of the array is :',np.std(arr))
print(' The variance of the array is :',np.var(arr))
print(' The minimum value of the array is :',np.min(arr))
print(' The maximum value of the array is :',np.max(arr))
print(' The minimum element index of the array is :',np.argmin(arr))
print(' The maximum element index of the array is :',np.argmax(arr))
 The array created is :
[[ 0 1 2 3 4]
[ 5 6 7 8 9]
[10 11 12 13 14]
[15 16 17 18 19]]
The sum of arrays is : 190
The sum of the vertical axis array is : [30 34 38 42 46]
The sum of the horizontal axis array is : [10 35 60 85]
The average value of the array is : 9.5
The mean value of the vertical axis array is : [ 7.5 8.5 9.5 10.5 11.5]
The mean value of the horizontal axis array is : [ 2. 7. 12. 17.]
The standard deviation of the array is : 5.766281297335398
The variance of the array is : 33.25
The minimum value of the array is : 0
The maximum value of the array is : 19
The minimum element index of the array is : 0
The maximum element index of the array is : 19
# cumsum and cumprod Use of functions 
arr=np.arange(2,10)
print(' The array created is :',arr)
print(' The cumulative sum of array elements is :',np.cumsum(arr))
print(' The cumulative product of array elements is :',np.cumprod(arr))
 The array created is : [2 3 4 5 6 7 8 9]
The cumulative sum of array elements is : [ 2 5 9 14 20 27 35 44]
The cumulative product of array elements is : [ 2 6 24 120 720 5040 40320 362880]

Statistical analysis of iris length data

# Statistical analysis of iris 
iris_sepal_length=np.loadtxt('E:/anaconda/data/iris_sepal_length.csv',delimiter=',')
print(' The calyx length is :',iris_sepal_length)
iris_sepal_length.sort()
print(' The sorted calyx length table is :',iris_sepal_length)
 The calyx length is : [5.1 4.9 4.7 4.6 5. 5.4 4.6 5. 4.4 4.9 5.4 4.8 4.8 4.3 5.8 5.7 5.4 5.1
5.7 5.1 5.4 5.1 4.6 5.1 4.8 5. 5. 5.2 5.2 4.7 4.8 5.4 5.2 5.5 4.9 5.
5.5 4.9 4.4 5.1 5. 4.5 4.4 5. 5.1 4.8 5.1 4.6 5.3 5. 7. 6.4 6.9 5.5
6.5 5.7 6.3 4.9 6.6 5.2 5. 5.9 6. 6.1 5.6 6.7 5.6 5.8 6.2 5.6 5.9 6.1
6.3 6.1 6.4 6.6 6.8 6.7 6. 5.7 5.5 5.5 5.8 6. 5.4 6. 6.7 6.3 5.6 5.5
5.5 6.1 5.8 5. 5.6 5.7 5.7 6.2 5.1 5.7 6.3 5.8 7.1 6.3 6.5 7.6 4.9 7.3
6.7 7.2 6.5 6.4 6.8 5.7 5.8 6.4 6.5 7.7 7.7 6. 6.9 5.6 7.7 6.3 6.7 7.2
6.2 6.1 6.4 7.2 7.4 7.9 6.4 6.3 6.1 7.7 6.3 6.4 6. 6.9 6.7 6.9 5.8 6.8
6.7 6.7 6.3 6.5 6.2 5.9]
The sorted calyx length table is : [4.3 4.4 4.4 4.4 4.5 4.6 4.6 4.6 4.6 4.7 4.7 4.8 4.8 4.8 4.8 4.8 4.9 4.9
4.9 4.9 4.9 4.9 5. 5. 5. 5. 5. 5. 5. 5. 5. 5. 5.1 5.1 5.1 5.1
5.1 5.1 5.1 5.1 5.1 5.2 5.2 5.2 5.2 5.3 5.4 5.4 5.4 5.4 5.4 5.4 5.5 5.5
5.5 5.5 5.5 5.5 5.5 5.6 5.6 5.6 5.6 5.6 5.6 5.7 5.7 5.7 5.7 5.7 5.7 5.7
5.7 5.8 5.8 5.8 5.8 5.8 5.8 5.8 5.9 5.9 5.9 6. 6. 6. 6. 6. 6. 6.1
6.1 6.1 6.1 6.1 6.1 6.2 6.2 6.2 6.2 6.3 6.3 6.3 6.3 6.3 6.3 6.3 6.3 6.3
6.4 6.4 6.4 6.4 6.4 6.4 6.4 6.5 6.5 6.5 6.5 6.5 6.6 6.6 6.7 6.7 6.7 6.7
6.7 6.7 6.7 6.7 6.8 6.8 6.8 6.9 6.9 6.9 6.9 7. 7.1 7.2 7.2 7.2 7.3 7.4
7.6 7.7 7.7 7.7 7.7 7.9]
print(' The calyx length after weight removal is :',np.unique(iris_sepal_length))
print(' The sum of calyx length table is :',np.sum(iris_sepal_length))
print(' The cumulative sum of calyx length table is :',np.cumsum(iris_sepal_length))
print(' The mean value of calyx length table is :',np.mean(iris_sepal_length))
print(' The standard deviation of calyx length table is :',np.std(iris_sepal_length))
print(' The variance of calyx length table is :',np.var(iris_sepal_length))
print(' The minimum value of calyx length table is :',np.min(iris_sepal_length))
print(' The maximum value of calyx length table is :',np.max(iris_sepal_length))
 The calyx length after weight removal is : [4.3 4.4 4.5 4.6 4.7 4.8 4.9 5. 5.1 5.2 5.3 5.4 5.5 5.6 5.7 5.8 5.9 6.
6.1 6.2 6.3 6.4 6.5 6.6 6.7 6.8 6.9 7. 7.1 7.2 7.3 7.4 7.6 7.7 7.9]
The sum of calyx length table is : 876.5
The cumulative sum of calyx length table is : [ 4.3 8.7 13.1 17.5 22. 26.6 31.2 35.8 40.4 45.1 49.8 54.6
59.4 64.2 69. 73.8 78.7 83.6 88.5 93.4 98.3 103.2 108.2 113.2
118.2 123.2 128.2 133.2 138.2 143.2 148.2 153.2 158.3 163.4 168.5 173.6
178.7 183.8 188.9 194. 199.1 204.3 209.5 214.7 219.9 225.2 230.6 236.
241.4 246.8 252.2 257.6 263.1 268.6 274.1 279.6 285.1 290.6 296.1 301.7
307.3 312.9 318.5 324.1 329.7 335.4 341.1 346.8 352.5 358.2 363.9 369.6
375.3 381.1 386.9 392.7 398.5 404.3 410.1 415.9 421.8 427.7 433.6 439.6
445.6 451.6 457.6 463.6 469.6 475.7 481.8 487.9 494. 500.1 506.2 512.4
518.6 524.8 531. 537.3 543.6 549.9 556.2 562.5 568.8 575.1 581.4 587.7
594.1 600.5 606.9 613.3 619.7 626.1 632.5 639. 645.5 652. 658.5 665.
671.6 678.2 684.9 691.6 698.3 705. 711.7 718.4 725.1 731.8 738.6 745.4
752.2 759.1 766. 772.9 779.8 786.8 793.9 801.1 808.3 815.5 822.8 830.2
837.8 845.5 853.2 860.9 868.6 876.5]
The mean value of calyx length table is : 5.843333333333334
The standard deviation of calyx length table is : 0.8253012917851409
The variance of calyx length table is : 0.6811222222222223
The minimum value of calyx length table is : 4.3
The maximum value of calyx length table is : 7.9

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved