【发布时间】: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')
-
在定义
X和y之前尝试df = df.reset_index(drop=True)。或者在for循环中使用X.iloc[train_index]。此外,您可能希望在循环内定义reg。