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

Numpy Python open source scientific computing Toolkit

編輯:Python

Numpy python Open source scientific computing toolkit

NumPy An array is a multidimensional array object , be called ndarray. It consists of two parts :
① The actual data
② Metadata that describes the data

 # Multidimensional arrays ndarray
import numpy as np
ar = np.array([1,2,3,4,5,6,7])
print(ar) # The output array , Notice the format of the array : brackets , There are no commas between elements ( Distinguish between and list )
print(ar.ndim) # Output the number of array dimensions ( Number of axles ), Or say “ Rank ”, The number of dimensions is also called rank
print(ar.shape) # Dimension of array , about n That's ok m Array of columns ,shape by (n,m)
print(ar.size) # The total number of elements in the array , about n That's ok m Array of columns , The total number of elements is n*m
print(ar.dtype) # The type of element in the array , similar type()( Pay attention ,type() Is the function ,.dtype Is the method )
print(ar.itemsize) # The byte size of each element in the array ,int32l The type byte is 4,float64 The byte of is 8
print(ar.data) # Buffer containing actual array elements , Because we usually get the elements through the index of the array , So you don't usually need to use this property .
ar # Output in interactive mode , There will be array( Array )
# Basic properties of array
# ① The dimension of an array is called rank (rank), The rank of one-dimensional array is 1, The rank of a two-dimensional array is 2, And so on
# ② stay NumPy in , Every linear array is called an axis (axes), Rank actually describes the number of axes :
# for instance , A two-dimensional array is equivalent to two one-dimensional arrays , Each element in the first one-dimensional array is another one-dimensional array
# So a one-dimensional array is NumPy Axis in (axes), The first axis is the same as the underlying array , The second axis is the array in the underlying array .
# And the number of shafts —— Rank , That's the dimension of the array .

Create array :array() function , In parentheses can be a list 、 Yuan Zu 、 Array 、 Generator, etc

ar1 = np.array(range(10)) # integer
ar2 = np.array([1,2,3.14,4,5]) # floating-point
ar3 = np.array([[1,2,3],('a','b','c')]) # Two dimensional array : Nested sequence ( list , Yuanzu can )
ar4 = np.array([[1,2,3],('a','b','c','d')]) # Notice how the number of nested sequences varies
print(ar1,type(ar1),ar1.dtype)
print(ar2,type(ar2),ar2.dtype)
print(ar3,ar3.shape,ar3.ndim,ar3.size) # Two dimensional array , common 6 Elements
print(ar4,ar4.shape,ar4.ndim,ar4.size) # One dimensional array , common 2 Elements
**# Create array :arange(), similar range(), Returns the value of a uniform interval within a given interval .
print(np.arange(10)) # return 0-9, integer
print(np.arange(10.0)) # return 0.0-9.0, floating-point
print(np.arange(5,12)) # return 5-11
print(np.arange(5.0,12,2)) # return 5.0-12.0, In steps of 2
print(np.arange(10000)) # If the array is too large to print ,NumPy Will automatically skip the center of the array , And print only the corners :**
# Create array :linspace(): Return at interval [ Start , stop it ] It's calculated on the computer num Evenly spaced samples .
ar1 = np.linspace(2.0, 3.0, num=5)
ar2 = np.linspace(2.0, 3.0, num=5, endpoint=False)
ar3 = np.linspace(2.0, 3.0, num=5, retstep=True)
print(ar1,type(ar1))
print(ar2)
print(ar3,type(ar3))
# numpy.linspace(start, stop, num=50, endpoint=True, retstep=False, dtype=None)
# start: Starting value ,stop: End value 
# num: Number of generated samples , The default is 50
# endpoint: If it is true , Then stop is the last sample . otherwise , Not included . The default value is True.
# retstep: If it is true , return ( sample , step ), Where step size is the spacing between samples → Output as a containing 2 The ancestor of the elements , The first element is array, The second step is the actual value 
# Create array :zeros()/zeros_like()/ones()/ones_like()
ar1 = np.zeros(5)
ar2 = np.zeros((2,2), dtype = np.int)
print(ar1,ar1.dtype)
print(ar2,ar2.dtype)
print('------')
# numpy.zeros(shape, dtype=float, order='C'): Returns a new array of the given shape and type , Fill with zero .
# shape: Array latitude , More than two dimensions need to use (), And the input parameter is an integer 
# dtype: data type , Default numpy.float64
# order: In memory C or Fortran continuity ( By row or column ) Store multidimensional data .
ar3 = np.array([list(range(5)),list(range(5,10))])
ar4 = np.zeros_like(ar3)
print(ar3)
print(ar4)
print('------')
# Returns an array of zeros with the same shape and type as the given array , here ar4 according to ar3 The shape and dtype Create a full 0 Array of
ar5 = np.ones(9)
ar6 = np.ones((2,3,4))
ar7 = np.ones_like(ar3)
print(ar5)
print(ar6)
print(ar7)
# ones()/ones_like() and zeros()/zeros_like() equally , Just fill in 1
# Create array :eye()
print(np.eye(5))
# Create a square N*N The identity matrix of , The diagonal value is 1, Others are 0

ndarray Data type of

bool Boolean type stored in one byte (True or False)
inti An integer whose size is determined by the platform ( It's usually int32 or int64)
int8 One byte size ,-128 to 127
int16 Integers ,-32768 to 32767
int32 Integers ,-2 ** 31 to 2 ** 32 -1
int64 Integers ,-2 ** 63 to 2 ** 63 - 1
uint8 Unsigned integer ,0 to 255
uint16 Unsigned integer ,0 to 65535
uint32 Unsigned integer ,0 to 2 ** 32 - 1
uint64 Unsigned integer ,0 to 2 ** 64 - 1
float16 Semi precision floating point number :16 position , The sign 1 position , Index 5 position , precision 10 position
float32 Single-precision floating-point :32 position , The sign 1 position , Index 8 position , precision 23 position
float64 or float Double precision floating point :64 position , The sign 1 position , Index 11 position , precision 52 position
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

numpy The generic function

# Array shape :.T/.reshape()/.resize()
ar1 = np.arange(10)
ar2 = np.ones((5,2))
print(ar1,'\n',ar1.T)
print(ar2,'\n',ar2.T)
print('------')
# .T Method : Transposition , For example, Yuan shape by (3,4)/(2,3,4), Transpose result is (4,3)/(4,3,2) → Therefore, the result of one-dimensional array transposition remains unchanged
ar3 = ar1.reshape(2,5) # usage 1: Change the shape of the existing array directly
ar4 = np.zeros((4,6)).reshape(3,8) # usage 2: After generating the array, change the shape directly
ar5 = np.reshape(np.arange(12),(3,4)) # usage 3: Add an array to the parameter , Target shape
print(ar1,'\n',ar3)
print(ar4)
print(ar5)
print('------')
# numpy.reshape(a, newshape, order='C'): Provide a new shape for the array , Without changing its data , So the number of elements needs to be consistent !!
ar6 = np.resize(np.arange(5),(3,4))
print(ar6)
# numpy.resize(a, new_shape): Returns a new array with the specified shape , If necessary, fill in the required number of elements repeatedly .
# Pay attention :.T/.reshape()/.resize() Is to generate a new array !!!
# Array type conversion :.astype()
ar1 = np.arange(10,dtype=float)
print(ar1,ar1.dtype)
print('-----')
# You can set the array type at the parameter position
ar2 = ar1.astype(np.int32)
print(ar2,ar2.dtype)
print(ar1,ar1.dtype)
# a.astype(): Convert array type 
# Array stacking
a = np.arange(5) # a It's a one-dimensional array ,5 Elements
b = np.arange(5,9) # b It's a one-dimensional array ,4 Elements
ar1 = np.hstack((a,b)) # Be careful :((a,b)), The shape here can be different
print(a,a.shape)
print(b,b.shape)
print(ar1,ar1.shape)
a = np.array([[1],[2],[3]]) # a It's a two-dimensional array ,3 That's ok 1 Column
b = np.array([['a'],['b'],['c']]) # b It's a two-dimensional array ,3 That's ok 1 Column
ar2 = np.hstack((a,b)) # Be careful :((a,b)), The shape here must be the same
print(a,a.shape)
print(b,b.shape)
print(ar2,ar2.shape)
print('-----')
# numpy.hstack(tup): level ( In the order of columns ) Stacked array 
a = np.arange(5)
b = np.arange(5,10)
ar1 = np.vstack((a,b))
print(a,a.shape)
print(b,b.shape)
print(ar1,ar1.shape)
a = np.array([[1],[2],[3]])
b = np.array([['a'],['b'],['c'],['d']])
ar2 = np.vstack((a,b)) # The shape here can be different
print(a,a.shape)
print(b,b.shape)
print(ar2,ar2.shape)
print('-----')
# numpy.vstack(tup): vertical ( In the order of columns ) Stacked array 
a = np.arange(5)
b = np.arange(5,10)
ar1 = np.stack((a,b))
ar2 = np.stack((a,b),axis = 1)
print(a,a.shape)
print(b,b.shape)
print(ar1,ar1.shape)
print(ar2,ar2.shape)
# numpy.stack(arrays, axis=0): Join the sequence of arrays along the new axis , The shape must be the same !
# Focus on axis Meaning of parameter , Suppose two arrays [1 2 3] and [4 5 6],shape Are all (3,0)
# axis=0:[[1 2 3] [4 5 6]],shape by (2,3)
# Array splitting
ar = np.arange(16).reshape(4,4)
ar1 = np.hsplit(ar,2)
print(ar)
print(ar1,type(ar1))
# numpy.hsplit(ary, indices_or_sections): Set the array horizontally ( Column by column ) Split into multiple sub arrays → Split by column 
# The output result is a list , The elements in the list are arrays
ar2 = np.vsplit(ar,4)
print(ar2,type(ar2))
# numpy.vsplit(ary, indices_or_sections):: Set the array vertically ( Line direction ) Split into multiple sub arrays → Split by row 
# Array simple operation
ar = np.arange(6).reshape(2,3)
print(ar + 10) # Add
print(ar * 2) # Multiplication
print(1 / (ar+1)) # division
print(ar ** 0.5) # power
# Operations with scalars
print(ar.mean()) # averaging
print(ar.max()) # For maximum
print(ar.min()) # For the minimum
print(ar.std()) # Find standard deviation
print(ar.var()) # Variance estimation
print(ar.sum(), np.sum(ar,axis = 0)) # Sum up ,np.sum() → axis by 0, Sum by column ;axis by 1, Sum up by line
print(np.sort(np.array([1,4,3,2,5,6]))) # Sort

numpy Index and slice of

# Basic index and slice
ar = np.arange(20)
print(ar)
print(ar[4])
print(ar[3:6])
print('-----')
# One dimensional array index and slice
ar = np.arange(16).reshape(4,4)
print(ar, ' The number of array axes is %i' %ar.ndim) # 4*4 Array of
print(ar[2], ' The number of array axes is %i' %ar[2].ndim) # Slice is an element of the next dimension , So it's a one-dimensional array
print(ar[2][1]) # Secondary index , Get a value in a one-dimensional array
print(ar[1:3], ' The number of array axes is %i' %ar[1:3].ndim) # The slice is a two-dimensional array composed of two one-dimensional arrays
print(ar[2,2]) # The third row and the third column in the slice array → 10
print(ar[:2,1:]) # In the slice array 1,2 That's ok 、2,3,4 Column → Two dimensional array
print('-----')
# Two dimensional array index and slice
ar = np.arange(8).reshape(2,2,2)
print(ar, ' The number of array axes is %i' %ar.ndim) # 2*2*2 Array of
print(ar[0], ' The number of array axes is %i' %ar[0].ndim) # The first element of the next dimension of a three-dimensional array → A two-dimensional array
print(ar[0][0], ' The number of array axes is %i' %ar[0][0].ndim) # The first element under the first element of the next dimension of the three-dimensional array → A one-dimensional array
print(ar[0][0][1], ' The number of array axes is %i' %ar[0][0][1].ndim)
# numpy.random.rand(d0, d1, ..., dn): Generate a [0,1) Between random floating-point numbers or N Dimensional floating point array —— Uniform distribution 
import matplotlib.pyplot as plt # Import matplotlib modular , Used for chart aided analysis
% matplotlib inline
# Magical function , Automatically generate charts for each run
a = np.random.rand()
print(a,type(a)) # Generate a random floating point number
b = np.random.rand(4)
print(b,type(b)) # The generated shape is 4 One dimensional array of
c = np.random.rand(2,3)
print(c,type(c)) # The generated shape is 2*3 Two dimensional array of , Note that this is not ((2,3))
samples1 = np.random.rand(1000)
samples2 = np.random.rand(1000)
plt.scatter(samples1,samples2)
# Generate 1000 Well distributed sample values
# Store array data .npy file
import os
os.chdir('C:/Users/Hjx/Desktop/')
ar = np.random.rand(5,5)
print(ar)
np.save('arraydata.npy', ar)
# It can also be direct np.save('C:/Users/Hjx/Desktop/arraydata.npy', ar)
#%%
# Read array data .npy file
ar_load =np.load('arraydata.npy')
print(ar_load)
# It can also be direct np.load('C:/Users/Hjx/Desktop/arraydata.npy')
#%%
# Storage / Read text file
ar = np.random.rand(5,5)
np.savetxt('array.txt',ar, delimiter=',')
# np.savetxt(fname, X, fmt='%.18e', delimiter=' ', newline='\n', header='', footer='', comments='# '): Store as text txt file 
ar_loadtxt = np.loadtxt('array.txt', delimiter=',')
print(ar_loadtxt)
# It can also be direct np.loadtxt('C:/Users/Hjx/Desktop/array.txt')

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