2012-04-16 75 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