gpt4 book ai didi

python-3.x - Numpy Python : Exception: Data must be 1-dimensional

转载 作者:行者123 更新时间:2023-12-04 16:42:30 24 4
gpt4 key购买 nike

获取异常 Exception: Data must be 1-dimensional在 Python 3.7 中使用 NumPy
相同的代码适用于其他人,但不适用于我的情况。波纹管是我的代码请帮忙

Working_code_in_diff_system

Same_code_not_working_in_my_system

import numpy as np 
from sklearn import linear_model
from sklearn.model_selection import train_test_split
import seaborn as sns
from sklearn import metrics
import matplotlib.pyplot as plt
%matplotlib inline

df = pd.read_csv('./Data/new-data.csv', index_col=False)

x_train, x_test, y_train, y_test = train_test_split(df['Hours'], df['Marks'], test_size=0.2, random_state=42)

sns.jointplot(x=df['Hours'], y=df['Marks'], data=df, kind='reg')

x_train = np.reshape(x_train, (-1,1))
x_test = np.reshape(x_test, (-1,1))
y_train = np.reshape(y_train, (-1,1))
y_test = np.reshape(y_test, (-1,1))



#
print('Train - Predictors shape', x_train.shape)
print('Test - Predictors shape', x_test.shape)
print('Train - Target shape', y_train.shape)
print('Test - Target shape', y_test.shape)

预期输出应该是

Train - Predictors shape (80, 1)

Test - Predictors shape (20, 1)

Train - Target shape (80, 1)

Test - Target shape (20, 1)


作为输出获取异常 Exception: Data must be 1-dimensional

最佳答案

我想你需要打电话np.reshape在底层的 numpy 数组上而不是在 Pandas 系列上 - 你可以使用 .values 来做到这一点:

x_train = np.reshape(x_train.values, (-1, 1))

对接下来的三行重复相同的想法。

或者,如果您使用的是 Pandas 的最新版本 >= 0.24, to_numpy首选:
x_train = np.reshape(x_train.to_numpy(), (-1, 1))

关于python-3.x - Numpy Python : Exception: Data must be 1-dimensional,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57192304/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com