scipy.interpolate.UnivariateSpline无论参数如何都不平滑

scipy.interpolate.UnivariateSpline无论参数如何都不平滑,第1张

scipy.interpolate.UnivariateSpline无论参数如何都不平滑

@Zhenya的手动在数据点之间设置打结的答案太粗糙了,以至于在嘈杂的数据中无法获得良好的结果,而没有选择如何应用此技术。但是,受到他/她建议的启发,我在scikit-
learn软件包中的Mean-Shift聚类方面取得了成功。它执行集群数的自动确定,并且似乎做得相当不错(实际上非​​常平滑)。

# importsimport numpyimport pylabimport scipyimport sklearn.cluster# Set up original data - note that it's monotonically increasing by X value!data = {}data['original'] = {}data['original']['x'] = [0, 5024.2059124920379, 7933.1645067836089, 7990.4664106277542, 9879.9717114947653, 13738.60563208926, 15113.277958924193]data['original']['y'] = [0.0, 3072.5653360000988, 5477.2689107965398, 5851.6866463790966, 6056.3852496014106, 7895.2332350173638, 9154.2956175610598]# Cluster data, sort it and and saveinputNumpy = numpy.array([[data['original']['x'][i], data['original']['y'][i]] for i in range(0, len(data['original']['x']))])meanShift = sklearn.cluster.MeanShift()meanShift.fit(inputNumpy)clusteredData = [[pair[0], pair[1]] for pair in meanShift.cluster_centers_]clusteredData.sort(lambda pair1, pair2: cmp(pair1[0],pair2[0]))data['clustered'] = {}data['clustered']['x'] = [pair[0] for pair in clusteredData]data['clustered']['y'] = [pair[1] for pair in clusteredData]# Build a spline using the clustered data and predictmySpline = scipy.interpolate.UnivariateSpline(x=data['clustered']['x'], y=data['clustered']['y'], k=1)xi = range(0, round(max(data['original']['x']), -3) + 3000, 20)yi = mySpline(xi)# Plot the datapointspylab.plot(data['clustered']['x'], data['clustered']['y'], "D", label="Datapoints (%s)" % 'clustered')pylab.plot(xi, yi, label="Predicted (%s)" %  'clustered')pylab.plot(data['original']['x'], data['original']['y'], "o", label="Datapoints (%s)" % 'original')# Show the plotpylab.grid(True)pylab.xticks(rotation=45)pylab.legend( loc="lower right" )pylab.show()



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5664568.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存