《Python机器学习基础教程》第二章笔记:ValueError: cannot reshape array of size 4000000 into shape (1000,1000)

@[TOC](《Python机器学习基础教程》第二章笔记:ValueError: cannot reshape array of size 4000000 into shape (1000,1000))

成功解决:
增加命令

y = y % 2

在这里插入图片描述

一、报错

ValueError: cannot reshape array of size 4000000 into shape (1000,1000)

在这里插入图片描述

二、尝试解决

意思:ValueError:无法将大小为4000000的数组重塑为形状(1000,1000)
数组没有办法重塑

尝试解决的思路:
1、查看源码:
在这里插入图片描述
没有看懂!!!!
不过没有关系

2、其中centers为控制y中值类别的参数
默认为2

在这里插入图片描述

此时赋值为4

3、输出一下y
在这里插入图片描述
对比没有赋值为4是:
在这里插入图片描述
即此时为3

将centers赋值为2 时,成功输出

在这里插入图片描述
在这里插入图片描述

由此可见,需要对center或者y进行调整,由于需要center=4,因而调整y,使得y=y%2即可

X, y = make_blobs(centers=4,random_state=8)#centers为数据堆
print(y)##centers为数据堆改变了y的值
y = y % 2
print(y)
linear_svm = LinearSVC().fit(X, y)
mglearn.plots.plot_2d_separator(linear_svm, X)#classification可以
mglearn.discrete_scatter(X[:, 0], X[:, 1], y)
plt.xlabel("Feature 0")
plt.ylabel("Feature 1")
plt.show()

在这里插入图片描述

在这里插入图片描述

参考
[1]小书同学:《监督学习(九)——核支持向量机SVM》,链接: link.

非常抱歉,我之前的回答有误导。根据您的数据形状,模型的输入应该是 `(2, 5, 1)` 而不是 `(2, 6, 1)`。请将代码中的 `X_train.reshape(2, 5, 1)` 和 `y_train.reshape(2, 5, 1)` 改为 `X_train.reshape(2, 3, 1)` 和 `y_train.reshape(2, 3, 1)`。 以下是修改后的代码: ```python import numpy as np from tensorflow.keras.models import Sequential from tensorflow.keras.layers import GRU, Dense from sklearn.metrics import mean_squared_error, mean_absolute_error, r2_score import matplotlib.pyplot as plt # 输入数据 data = np.array([[1, 4, 6, 7, 9, 13], [4, 7, 5, 8, 19, 26], [1, 5, 7, 245, 145, 11]]) # 将数据分为输入和输出序列 X_train = data[:2, :-1] y_train = data[:2, 1:] X_val = data[2:3, :-1] y_val = data[2:3, 1:] # 构建GRU模型 model = Sequential() model.add(GRU(32, input_shape=(3, 1))) # 输入序列长度为3 model.add(Dense(3)) # 编译并训练模型 model.compile(optimizer='adam', loss='mse') model.fit(X_train.reshape(2, 3, 1), y_train.reshape(2, 3, 1), epochs=100) # 预测验证集数据 predictions = model.predict(X_val.reshape(1, 3, 1)) # 计算指标 mse = mean_squared_error(y_val.reshape(-1), predictions.reshape(-1)) mae = mean_absolute_error(y_val.reshape(-1), predictions.reshape(-1)) rmse = np.sqrt(mse) r2 = r2_score(y_val.reshape(-1), predictions.reshape(-1)) # 绘制验证集和预测值 plt.plot(np.arange(2, 8), y_val.reshape(-1), label='Validation') plt.plot(np.arange(3, 9), predictions.reshape(-1), label='Prediction') plt.xlabel('Time Step') plt.ylabel('Value') plt.legend() plt.show() print("MSE:", mse) print("MAE:", mae) print("RMSE:", rmse) print("R2:", r2) ``` 再次非常抱歉给您带来的困扰,希望这次能够顺利运行。如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值