gpt4 book ai didi

python - 值错误: x and y must be the same size

转载 作者:行者123 更新时间:2023-11-30 08:26:02 26 4
gpt4 key购买 nike

import numpy as np
import pandas as pd
import matplotlib.pyplot as pt

data1 = pd.read_csv('stage1_labels.csv')

X = data1.iloc[:, :-1].values
y = data1.iloc[:, 1].values

from sklearn.preprocessing import LabelEncoder, OneHotEncoder
label_X = LabelEncoder()
X[:,0] = label_X.fit_transform(X[:,0])
encoder = OneHotEncoder(categorical_features = [0])
X = encoder.fit_transform(X).toarray()

from sklearn.cross_validation import train_test_split
X_train, X_test, y_train,y_test = train_test_split(X, y, test_size = 0.4, random_state = 0)

#fitting Simple Regression to training set

from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
regressor.fit(X_train, y_train)

#predecting the test set results
y_pred = regressor.predict(X_test)

#Visualization of the training set results
pt.scatter(X_train, y_train, color = 'red')
pt.plot(X_train, regressor.predict(X_train), color = 'green')
pt.title('salary vs yearExp (Training set)')
pt.xlabel('years of experience')
pt.ylabel('salary')
pt.show()

我需要帮助理解执行上述代码时出现的错误。以下是错误:

"raise ValueError("x and y must be the same size")"

我有 1398 行 2 列的 .csv 文件。我取了 40% 作为 y_test 集,如上面代码中所示。

最佳答案

打印 X_train 形状。你看到了什么?我敢打赌 X_train 是 2d(单列矩阵),而 y_train 是 1d(向量)。反过来你会得到不同的尺寸。

我认为使用 X_train[:,0] 进行绘图(这是错误产生的地方)应该可以解决问题

关于python - 值错误: x and y must be the same size,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41659535/

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