【问题标题】:Why it's showing 'ValueError' even though there are no NaN, infinity or large values?为什么即使没有 NaN、无穷大或大值,它也会显示“ValueError”?
【发布时间】:2022-06-15 23:28:18
【问题描述】:

我用 nan 值替换了所有 inf 值。然后,所有删除了具有 nan 值的行。 我还将 dtype 更改为 float32。

df.replace([np.inf, -np.inf], np.nan, inplace=True)
df = df.dropna()

X = df.iloc[:,:-1].astype('float32')
y = df.iloc[:,-1].astype('float32')

from sklearn.model_selection import KFold
from sklearn.tree import DecisionTreeRegressor 
from sklearn.metrics import accuracy_score
import matplotlib.pyplot as plt
k=10
kf = KFold(n_splits=k, random_state=1, shuffle=True)
reg = DecisionTreeRegressor(random_state=1)
acc_score = []

for train_index, test_index in kf.split(X):
    X_train, X_test = X.reindex(index = train_index), X.reindex(index = test_index)
    y_train, y_test = y.reindex(index = train_index), y.reindex(index = test_index)
    reg.fit(X_train, y_train)
    y_pred = reg.predict(X_test)
    acc_score.append(accuracy_score(y_pred, y_test))

【问题讨论】:

  • 在删除 Null 之前尝试移动 .astype('float32')
  • 在定义 Xy 之前尝试 df = df.reset_index(drop=True)。或者在for 循环中使用X.iloc[train_index]。此外,您可能希望在循环内定义 reg

标签: python pandas


猜你喜欢
  • 2016-10-16
  • 2019-10-11
  • 2019-11-28
  • 2023-03-16
  • 1970-01-01
  • 2019-10-12
  • 2020-12-01
  • 2017-11-23
相关资源
最近更新 更多