python相关工具

 

1.matlab与python之间的数据传递

 1 import scipy.io as sio
 2 import numpy as np
 3 
 4 ###下面是讲解python怎么读取.mat文件以及怎么处理得到的结果###
 5 load_fn = 'xxx.mat'
 6 load_data = sio.loadmat(load_fn)
 7 load_matrix = load_data['matrix'] #假设文件中存有字符变量是matrix,例如matlab中save(load_fn, 'matrix');当然可以保存多个save(load_fn, 'matrix_x', 'matrix_y', ...);
 8 load_matrix_row = load_matrix[0] #取了当时matlab中matrix的第一行,python中数组行排列
 9 
10 ###下面是讲解python怎么保存.mat文件供matlab程序使用###
11 save_fn = 'xxx.mat'
12 save_array = np.array([1,2,3,4])
13 sio.savemat(save_fn, {'array': save_array}) #和上面的一样,存在了array变量的第一行
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}) #同理,只是

2.python的绘图

 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.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'>
25 25 
26 26 
27 27 ones:全1 
28 28 zeros:全0 
29 29 empty:随机数,取决于内存情况
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从malab中获取.mat

1 import scipy.io as sio#io相关模块,进行操作。
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]#取第一行
9 signal=np.reshape(signal,(784,1))

 5.python生成随机数

 1 #rand函数,产生0到1的随机数,参数是shape
 2 np.random.rand(3,4)
 3 >>生成0到1的随机数,shape为3行四列
 4 
 5 #randn函数,产生生标准正态分布,均值为0,方差为1,参数也是shape
 6 np.random.randn
 7 #randint函数,产生指定范围的随机整数,前两个参数表示范围,最后一个参数是size=(shape)
 8 np.random.randint(0,3,size=(3,4))
 9 
10 #numpy.random能产生特定分布的随机数,如normal分布、uniform分布、poisson分布等 
11 这些函数中前面几个参数是分布函数的参数,最后一个参数是shape 
12 如正态分布normal就是均值和方差,uniform就是上下界,泊松分布就是
13 
14 np.random.normal(均值,方差,size=(3,4))
15 
16 np.random.uniform(2,3,size=(3,4))#前两个参数为范围均匀分布
17 
18 np.random.pession(2,size=())#泊松分布

 6.python文件读取注意事项

 1 file=open('abc.tex','w')>>注意'w'写一次,会擦除之前的
 2 >>要持续写入'a'
 3 file=open("abc.txt".'a')
 4 注意文件打开后必须
 5 file.close()>>否则写入操作会遇到问题
 6 
 7 ####获取每一行的元素放在数组中
 8 file = open('text_c.txt')
 9  
10 lines = file.readlines()  
11 aa=[]  
12 for line in lines:  
13     temp=line.replace('
','')  #将每一行的换行符去掉。
14     aa.append(temp)  
原文地址:https://www.cnblogs.com/ChenKe-cheng/p/8982324.html