我有以下问题: 我正在使用DecisionTreeRegressor,并且在更改“ max_depth”时需要保存RSME的结果(培训和测试)。
from sklearn.tree import DecisionTreeRegressor
tree_reg = DecisionTreeRegressor(max_depth=25)
tree_reg.fit(X_train, y_train)
y_train_pred = tree_reg.predict(X_train)
from sklearn.metrics import mean_squared_error
tree_train_mse = mean_squared_error(y_train, y_train_pred)
print("RMSE Train: ", np.sqrt(tree_train_mse))
RMSE Train: 2178.5783334392877 # this is the value to save
y_test_pred = tree_reg.predict(X_test)
tree_test_mse = mean_squared_error(y_test, y_test_pred)
print("RMSE Test: ", np.sqrt(tree_test_mse))
RMSE Test: 25188.114240007588 # this is other value to save
答案 0 :(得分:0)
如果您希望将文件保存为纯txt
格式,则可以使用此格式保存文件
with open('RMSE.txt', 'a') as f: # note that "a" is to append each line on top of your file
f.write("max_depth: " + max_depth)
f.write("RMSE Train: " + np.sqrt(tree_train_mse))
f.write("RMSE Test: " + np.sqrt(tree_test_mse))
f.write('\n')
如果您执行长时间的操作可能会破坏脚本,那么将计算机内存中的所有内容通常都不好。您可以做的是保存到一个普通的txt
文件中,以后可以访问该文件。
答案 1 :(得分:0)
假设您将以下值测试为max_depth
:[5,10,15]。
使用for
循环并存储结果:
from sklearn.tree import DecisionTreeRegressor
from sklearn.metrics import mean_squared_error
for value in [5,10,15,25]:
tree_reg = DecisionTreeRegressor(max_depth = value)
tree_reg.fit(X_train, y_train)
y_train_pred = tree_reg.predict(X_train)
tree_train_mse = mean_squared_error(y_train, y_train_pred)
print("RMSE Train: ", np.sqrt(tree_train_mse))
y_test_pred = tree_reg.predict(X_test)
tree_test_mse = mean_squared_error(y_test, y_test_pred)
print("RMSE Test: ", np.sqrt(tree_test_mse))
with open('results.txt', 'a') as f:
f.write("RMSE Train: " + np.sqrt(tree_train_mse))
f.write("RMSE Test: " + np.sqrt(tree_test_mse))
f.write("max_depth: " + max_depth)
f.write('\n')