#使用前面定义的函数进行画图 X0,X1=X_train[:,0],X_train[:,1] xx,yy=make_meshgrid(X0,X1) for clf,title,ax in zip(models,titles,sub.flatten()): plot_contours(ax,clf,xx,yy,cmap=plt.cm.plasma,alpha=0.8) ax.scatter(X0,X1,c=y,cmap=plt.cm.plasma,s=20,edgecolors='k') ax.set_xlim(xx.min(),xx.max()) ax.set_ylim(yy.min(),yy.max()) ax.set_xlabel('Feature 0') ax.set_ylabel('Feature 1') ax.set_xticks(()) ax.set_yticks(()) ax.set_title(title) plt.show() 代码报错TypeError: unsupported operand type(s) for -: 'str' and 'int' 应该如何修改,请给我代码
时间: 2024-02-29 11:51:47 浏览: 94
这个错误通常是由于在`set_xticks()`或`set_yticks()`函数中传入了一个非整型的参数导致的。
你可以将这两个函数中的参数改为整型,或者去掉这两个函数,让Matplotlib自动计算刻度值。
修改后的代码如下:
```python
X0, X1 = X_train[:, 0], X_train[:, 1]
xx, yy = make_meshgrid(X0, X1)
for clf, title, ax in zip(models, titles, sub.flatten()):
plot_contours(ax, clf, xx, yy, cmap=plt.cm.plasma, alpha=0.8)
ax.scatter(X0, X1, c=y, cmap=plt.cm.plasma, s=20, edgecolors='k')
ax.set_xlim(xx.min(), xx.max())
ax.set_ylim(yy.min(), yy.max())
ax.set_xlabel('Feature 0')
ax.set_ylabel('Feature 1')
ax.set_title(title)
plt.show()
```
在上述代码中,我们去掉了`set_xticks()`和`set_yticks()`函数,并且保证`xx.min()`, `xx.max()`, `yy.min()`和`yy.max()`的返回值为整型。
相关问题
解释以下代码def split_data(x, y, ratio=0.8): to_train = int(input_len * ratio) # 进行调整以匹配 batch_size to_train -= to_train % batch_size x_train = x[:to_train] y_train = y[:to_train] x_test = x[to_train:] y_test = y[to_train:] # 进行调整以匹配 batch_size to_drop = x.shape[0] % batch_size if to_drop > 0: x_test = x_test[:-1 * to_drop] y_test = y_test[:-1 * to_drop] # 一些重塑 reshape_3 = lambda x: x.values.reshape((x.shape[0], x.shape[1], 1)) x_train = reshape_3(x_train) x_test = reshape_3(x_test) reshape_2 = lambda x: x.values.reshape((x.shape[0], 1)) y_train = reshape_2(y_train) y_test = reshape_2(y_test) return (x_train, y_train), (x_test, y_test) (x_train, y_train), (x_test, y_test) = split_data(data_input, expected_output) print('x_train.shape: ', x_train.shape) print('y_train.shape: ', y_train.shape) print('x_test.shape: ', x_test.shape) print('y_test.shape: ', y_test.shape)
这段代码是一个数据分割函数,用于将输入数据和输出数据按照一定比例分割成训练集和测试集。其中,参数 x 和 y 分别是输入数据和输出数据,ratio 表示训练集所占比例,默认为 0.8。
首先,函数根据 ratio 计算出训练集的长度 to_train,并将其调整为能够匹配 batch_size 的长度。然后,函数将输入数据和输出数据分别划分为训练集和测试集,其中测试集的长度为输入数据总长度减去训练集长度。同样地,函数也将测试集的长度调整为能够匹配 batch_size 的长度。
接下来,函数对训练集和测试集进行了一些重塑操作,以便于后续的模型训练。其中,reshape_3 函数将训练集和测试集的输入数据转化为三维张量,reshape_2 函数将训练集和测试集的输出数据转化为二维张量。
最后,函数返回了训练集和测试集的输入数据和输出数据,分别存储在 (x_train, y_train) 和 (x_test, y_test) 中,并输出了各自的形状。
x = -20:0.1:20; y = tanh(x) + cos(x); train_data = [x; y]; % 修改为纵向拼接 x1 = (-20:0.01:20); y1 = tanh(x1) + cos(x1); test_data = [x1; y1]; % 修改为纵向拼接 input_train = train_data(1, :); output_train = train_data(2, :); % 修改为取第二行数据 input_test = test_data(1, :); output_test = test_data(2, :); % 修改为取第二行数据 net = newff(input_train, output_train, [10, 20, 9]); net.trainParam.epochs = 10000; net.trainParam.goal = 0.00001; net.trainParam.max_fail = 10000; net = train(net, input_train, output_train);修正并拟合出曲线
抱歉,上面的代码还是存在问题,`newff 函数的输入参数应该是样本特征标签,而不是单独的输入输出。在这种情下,你需要将input_train` 和 `output_train 转换为矩阵形,然后再传递给 `newff`。同时,为了正确训练神经,你还需要对输入和输出数据进行转置操作。
以下是修正后的代码:
```matlab
x = -20:0.1:20;
y = tanh(x) + cos(x);
train_data = [x; y]'; % 转置数据
x1 = (-20:0.01:20);
y1 = tanh(x1) + cos(x1);
test_data = [x1; y1]'; % 转置数据
input_train = train_data(:, 1);
output_train = train_data(:, 2);
input_test = test_data(:, 1);
output_test = test_data(:, 2);
net = newff(minmax(input_train), minmax(output_train), [10, 20, 9]); % 使用 minmax 对输入和输出进行归一化
net.trainParam.epochs = 10000;
net.trainParam.goal = 0.00001;
net.trainParam.max_fail = 10000;
net = train(net, input_train', output_train'); % 转置输入和输出数据
```
这样,你就可以使用修正后的代码来拟合出给定的曲线。请注意,我还添加了对输入和输出进行归一化的步骤,以提高训练的效果。
阅读全文
相关推荐















