我在Python中使用方括号有问题.我写了一个产生以下输出的代码:
[[180.0], [173.8], [164.2], [156.5], [147.2], [138.2]]
Run Code Online (Sandbox Code Playgroud)
但我想用它来进行一些计算,但方括号不会让我.
如何删除括号?我看到了一些例子,但我不能将它们应用于这种情况.
我编写了以下代码来执行样条插值:
import numpy as np
import scipy as sp
x1 = [1., 0.88, 0.67, 0.50, 0.35, 0.27, 0.18, 0.11, 0.08, 0.04, 0.04, 0.02]
y1 = [0., 13.99, 27.99, 41.98, 55.98, 69.97, 83.97, 97.97, 111.96, 125.96, 139.95, 153.95]
x = np.array(x1)
y = np.array(y1)
new_length = 25
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)
Run Code Online (Sandbox Code Playgroud)
但我得到了:
ValueError: A value in x_new is below the interpolation range.
Run Code Online (Sandbox Code Playgroud)
在 interpolate.py
任何帮助,将不胜感激.
我编写了一个执行样条插值的代码:
x1 = [ 0., 13.99576991, 27.99153981, 41.98730972, 55.98307963, 69.97884954, 83.97461944, 97.97038935, 111.9661593, 125.9619292, 139.9576991, 153.953469 ]
y1 = [ 1., 0.88675318, 0.67899118, 0.50012243, 0.35737022, 0.27081293, 0.18486778, 0.11043095, 0.08582272, 0.04946131, 0.04285015, 0.02901567]
x = np.array(x1)
y = np.array(y1)
# Interpolate the data using a cubic spline to "new_length" samples
new_length = 50
new_x = np.linspace(x.min(), x.max(), new_length)
new_y = sp.interpolate.interp1d(x, y, kind='cubic')(new_x)
Run Code Online (Sandbox Code Playgroud)
但是在生成的新数据集中 new_x
,new_y
原始点被消除,只保留第一个和最后一个值.我想保留原点.