python相关工具
生活随笔
收集整理的這篇文章主要介紹了
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('\n','') #將每一行的換行符去掉。 14 aa.append(temp)?
轉載于:https://www.cnblogs.com/ChenKe-cheng/p/8982324.html
總結
以上是生活随笔為你收集整理的python相关工具的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: vue骨架屏、时间选择器、轮播图。。你想
- 下一篇: PHP单元测试使用