ValueError Traceback (most recent call last) Cell In[10], line 72 68 B3[a2 - 1, 9] = np.min(B6[0, :]) 69 B3[a2 - 1, 10] = np.max(B6[0, :]) ---> 72 f = interp1d(B6[0, :], B6[1, :], kind='cubic') 73 xx = np.array([0, 0.03, 0.09, 0.18, 0.3, 0.45, 0.63, 0.84, 1.0]) 74 yy = f(xx) File ~\AppData\Roaming\Python\Python39\site-packages\scipy\interpolate\_interpolate.py:616, in interp1d.__init__(***failed resolving arguments***) 613 yy = np.ones_like(self._y) 614 rewrite_nan = True --> 616 self._spline = make_interp_spline(xx, yy, k=order, 617 check_finite=False) 618 if rewrite_nan: 619 self._call = self.__class__._call_nan_spline File ~\AppData\Roaming\Python\Python39\site-packages\scipy\interpolate\_bsplines.py:1293, in make_interp_spline(x, y, k, t, bc_type, axis, check_finite) 1290 raise ValueError('Shapes of x {} and y {} are incompatible' 1291 .format(x.shape, y.shape)) 1292 if np.any(x[1:] == x[:-1]): -> 1293 raise ValueError("Expect x to not have duplicates") 1294 if x.ndim != 1 or np.any(x[1:] < x[:-1]): 1295 raise ValueError("Expect x to be a 1D strictly increasing sequence.") ValueError: Expect x to not have duplicates该怎么修改
时间: 2023-11-12 11:06:03 浏览: 162
Python解析json之ValueError: Expecting property name enclosed in double quotes: line 1 column 2(char 1)
这个错误是因为你的输入 x 中有重复的值,而 `interp1d` 函数不能处理这种情况。解决方法是将重复的值删除或修改。你可以使用 `np.unique` 函数来删除重复的值,例如:
```python
B6_unique, indices = np.unique(B6[0, :], return_index=True)
B6_new = np.array([B6_unique, B6[1, :][indices]])
f = interp1d(B6_new[0, :], B6_new[1, :], kind='cubic')
```
这里的 `np.unique` 函数会返回一个去除重复值后的数组 `B6_unique`,以及一个索引数组 `indices`,用于重新排序原始数组 `B6`。然后使用 `B6_new` 代替原始的 `B6` 数组进行插值操作。
阅读全文