python related tools

 

 

 

 

1. Data transfer between matlab and python

1  import scipy.io as sio
 2  import numpy as np
 3  
4  # ##The following is an explanation of how python reads .mat files and how to process the results ### 
5 load_fn = ' xxx.mat ' 
6 load_data = sio.loadmat (load_fn)
 7 load_matrix = load_data[ ' matrix ' ] #Assume that the character variable stored in the file is matrix, such as save(load_fn, 'matrix') in matlab; of course, multiple save(load_fn, 'matrix_x', 'matrix_y' can be saved ', ...); 
8 load_matrix_row = load_matrix[0] #Take the first row of the matrix in matlab at that time, and the array row in python is arranged 
9  
10  # ##The following is an explanation of how python saves .mat files for use by matlab programs# ## 
11save_fn = ' xxx.mat ' 
12 save_array = np.array([1,2,3,4 ])
 13 sio.savemat(save_fn, { ' array ' : save_array}) #Same as above, there is an array variable First line 
14  
15 save_array_x = np.array([1,2,3,4 ])
 16 save_array_y = np.array([5,6,7,8 ])
 17 sio.savemat(save_fn, { ' array_x ' : save_array_x, ' array_x ' : save_array_x}) #Similarly , just

 

2. Python drawing

 1 import matpylib.pyplot as plt
 2  
 3 a=np.arange(0,4,0.01).reshape(400,1)
 4 
 5 figure1=plt.figure()
 6 plt.plot(np.linspace(0,400,400),a,'b-',label='ckc')
 7 plt.title("ckc")
 8 plt.xlabel("c")
 9 plt.ylabel("x")
10 plt.legend()
11 plt.show()

 

3. Array creation operation in python

 

 1  1 #数组的初始化
 2  2 >>> import numpy as np
 3  3 >>> a = np.arange(15).reshape(3, 5)
 4  4 >>> a
 5  5 array([[ 0,  1,  2,  3,  4],
 6  6        [ 5,  6,  7,  8,  9],
 7  7        [10, 11, 12, 13, 14]])
 8  8 >>> a.shape
 9  9 (3, 5)
10 10 >>> a.ndim
11 11 2
12 12 >>> a.dtype.name
13 13 'int64'
14 14 >>> a.itemsize
15 15 8
16 16 >>> a.size
17 17 15
18 18 >>> type(a)
19 19 <type 'numpy.ndarray'>
20 20 >>> b = np.array([6, 7, 8])
21 21 >>> b
22 22 array([6, 7, 8])
23 23 >>> type(b)
24 24 <type 'numpy.ndarray'>
zeros: all 0s 
2828ones: all 1s 
272726 
2625 
2529 29 empty: random number, depends on memory
 30 30 
 31 31 >>> np.zeros( (3,4 ) )
 32 32 array([[ 0., 0., 0., 0.],
 33 33         [ 0., 0., 0., 0.],
 34 34         [ 0., 0., 0., 0.]])
 35 35 >>> np.ones( (2,3,4), dtype=np .int16 )                 # dtype can also be specified 
36 36 array([[[ 1, 1, 1, 1 ],
 37 37 [ 1, 1, 1, 1 ],
 38 38 [ 1, 1, 1, 1 ]],
 39 39 [[ 1, 1, 1, 1 ],
 40 40 [ 1, 1, 1, 1],
41 41         [ 1, 1, 1, 1]]], dtype=int16)
42 42 >>> np.empty( (2,3) )                                 # uninitialized, output may vary
43 43 array([[  3.73603959e-262,   6.02658058e-154,   6.55490914e-260],
44 44        [  5.30498948e-313,   3.14673309e-307,   1.00000000e+000]])
45 45 
46 46 #np.arange()的用法
47 47 >>> np.arange( 10, 30, 5 )
48 48 array([10, 15, 20, 25])
49 49 >>> np.arange( 0, 2, 0.3 )                 # it accepts float arguments
50 50 array([ 0. ,  0.3,  0.6,  0.9,  1.2,  1.5,  1.8])
51 51 
52 52 >>> np.linspace( 0, 2, 9 )                 # 9 numbers from 0 to 2
53 53 array([ 0.  ,  0.25,  0.5 ,  0.75,  1.  ,  1.25,  1.5 ,  1.75,  2.  ])
54 54 >>> x = np.linspace( 0, 2*pi, 100 )

 

4.python gets .mat from malab

 

1  import scipy.io as sio # io related modules to operate. 
2 curcwd= os.getcwd()
 3  
4 mat_theory= ' noise_784.mat ' 
5 data_theory= sio.loadmat(mat_theory)
 6 load_matrix=data_theory[ ' noise_784 ' ]
 7  
8 signal=load_matrix[0] #Take the first line 
9 signal =np.reshape(signal,(784,1))

 5. python generates random numbers

1  # rand function, generate random numbers from 0 to 1, the parameter is shape 
2 np.random.rand(3,4 )
 3 >> Generate random numbers from 0 to 1, shape is 3 rows and 4 columns
 4  
5  # randn function, Generate a standard normal distribution with a mean of 0 and a variance of 1. The parameters are also shape 
6  np.random.randn
 7  # randint function, which generates random integers in the specified range. The first two parameters represent the range, and the last parameter is size=( shape) 
8 np.random.randint(0,3,size=(3,4 ))
 9  
10  # numpy.random can generate random numbers of a specific distribution, such as normal distribution, uniform distribution, poisson distribution, etc. 
11  The front of these functions Several parameters are the parameters of the distribution function, the last parameter is shape 
 12 ,  such as normal distribution, normal is the mean and variance, uniform is the upper and lower bounds, and Poisson distribution is
 13  
14 np.random.normal(mean, variance, size=(3 ,4 ))
 15 
16 np.random.uniform(2,3,size=(3,4)) #The first two parameters are uniform distribution in the range 
17  
18 np.random.pession(2,size=()) #Poisson distribution

 6. Notes on python file reading

 

1 file=open( ' abc.tex ' , ' w ' ) >> Note that writing ' w ' once will erase the previous
 2 >> continue to write ' a ' 
3 file=open( " abc.txt " . ' a ' )
 4  Note that the file must be opened
 5 file.close() >> otherwise the writing operation will encounter problems
 6  
7  # ###Get the elements of each line and put them in the array 
8 file = open( ' text_c.txt ' )
 9   
10 lines = file.  
readlines()  
11aa= []  
 12  for line in lines:  
 13      temp=line.replace( ' \n ' , '' )   #Remove the newline character of each line. 
14      aa.append(temp)  

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325209311&siteId=291194637