gpt4 book ai didi

python - x 和 y 的索引不匹配 - Tensorflow

转载 作者:行者123 更新时间:2023-12-01 00:36:42 25 4
gpt4 key购买 nike

我有一个 (108116, 9) pandas.core.frame.DataFrame (X_train) 和一个 (108116, ) pandas.core.series.Series (y_train) 训练为我的模型创建估计器,但我收到此错误:

 110     if not np.array_equal(x.index, y.index):
111 raise ValueError('Index for x and y are mismatched.\nIndex for x: %s\n'
--> 112 'Index for y: %s\n' % (x.index, y.index))
113 if isinstance(y, pd.DataFrame):
114 y_columns = [(column, _get_unique_target_key(x, column))

ValueError: Index for x and y are mismatched.

我注意到我的两个变量具有相同的大小,但 y_train 从索引 6072 开始,而另一个从索引 0 开始。我收到的错误是因为这个吗?我尝试访问 y_train[0] 并收到错误,只能从第 6072 个位置访问元素。

如果这是原因,我如何将所有元素 6072 向下移动到 0,以便它们都相同?

提前致谢!

最佳答案

您可以通过Series.reset_index在系列y_train中创建默认的RangeIndexdrop=True:

y_train = y_train.reset_index(drop=True)

示例:

y_train = pd.Series([1,5,6], index=[6072, 6073, 6074])
print (y_train)
6072 1
6073 5
6074 6
dtype: int64

y_train = y_train.reset_index(drop=True)
print (y_train)
0 1
1 5
2 6
dtype: int64

print (y_train.index)
RangeIndex(start=0, stop=3, step=1)

关于python - x 和 y 的索引不匹配 - Tensorflow,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57685767/

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