2012-04-16 78 views
3

使用一些實驗數據,我不能在我的生活中找到如何使用splrep創建B樣條。該數據在這裏:http://ubuntuone.com/4ZFyFCEgyGsAjWNkxMBKWD如何使用scipy.interpolate.splrep插入曲線?

下面是摘錄:

#Depth Temperature 
1 14.7036 
-0.02 14.6842 
-1.01 14.7317 
-2.01 14.3844 
-3 14.847 
-4.05 14.9585 
-5.03 15.9707 
-5.99 16.0166 
-7.05 16.0147 

,這裏是它與深入y和溫度X上的一個情節: enter image description here

這裏是我的代碼:

import numpy as np 
from scipy.interpolate import splrep, splev 

tdata = np.genfromtxt('t-data.txt', 
         skip_header=1, delimiter='\t') 
depth = tdata[:, 0] 
temp = tdata[:, 1] 

# Find the B-spline representation of 1-D curve: 
tck = splrep(depth, temp) 
### fails here with "Error on input data" returned. ### 

我知道我在做什麼bleedingly愚蠢的,但我就是不能看到它。

回答

6

你只需要你的值從最小到最大:)。對你來說不應該是一個問題@不同的本,但要注意未來的讀者,depth[indices]將拋出一個TypeError如果深度是一個列表,而不是一個numpy數組!

>>> indices = np.argsort(depth) 
>>> depth = depth[indices] 
>>> temp = temp[indices] 
>>> splrep(depth, temp) 
(array([-7.05, -7.05, -7.05, -7.05, -5.03, -4.05, -3. , -2.01, -1.01, 
     1. , 1. , 1. , 1. ]), array([ 16.0147 , 15.54473241, 16.90606794, 14.55343229, 
     15.12525673, 14.0717599 , 15.19657895, 14.40437622, 
     14.7036 , 0.  , 0.  , 0.  , 0.  ]), 3) 

帽尖@FerdinandBeyer用於argsort代替我的難看的建議「ZIP的值,排序的拉鍊,重新分配值」的方法。

+2

你或許應該使用'np.argsort()'函數來排序的數據:'指數= np.argsort(深度);深度=深度[指數]; temp = temp [indices]'。這爲您節省了元組列表的繞行。 – 2012-04-16 08:24:12

+0

@FerdinandBeyer這是一個比我的更好的解決方案!現在編輯它。 – 2012-04-16 08:25:11

+0

當我試圖做'深度[指數]'時,我得到了一個錯誤,但是請讓我知道是否有比'[深度[我]在指數中]更好的方式「。無論哪種方式,這是一個更清潔的解決方案,謝謝。 – 2012-04-16 08:31:23