Код: Выделить всё
# Prepare target and features
target_column = 'resolution'
X = data.drop(columns=[target_column])
y = data[target_column]
# Convert categorical data to binary/numerical where needed
X_encoded = pd.get_dummies(X) # One-hot encoding for categorical features
le = LabelEncoder()
y_encoded = le.fit_transform(y) # Encode target variable
# Split the dataset into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X_encoded, y_encoded, test_size=0.3, random_state=42)
# Perform Grid Search to tune max_depth, min_samples_split, min_samples_leaf, and class_weight
param_grid = {
'max_depth': range(1, 11), # Range of max_depth to test
'min_samples_split': [2, 5, 10], # Options for min_samples_split
'min_samples_leaf': [1, 2, 4],
'class_weight': ['balanced']# Options for min_samples_leaf
}
model = DecisionTreeClassifier(random_state=42)
# Initialize GridSearchCV with 5-fold cross-validation
grid = GridSearchCV(model, param_grid, cv=5, n_jobs=-1, verbose=2, scoring='accuracy') # Using accuracy for classification
grid.fit(X_train, y_train)
# Best parameters from grid search
best_params = grid.best_params_
# print("Best Parameters from Grid Search:", best_params)
# Evaluate the best model on the test set
best_model = grid.best_estimator_
test_accuracy = best_model.score(X_test, y_test) * 100 # Accuracy as percentage
# print(f"Test Accuracy of the Best Model: {test_accuracy:.2f}%")
# Display the structure of the decision tree
# tree_structure = tree.export_text(best_model, feature_names=list(X_encoded.columns))
# print("\nDecision Tree Structure:")
# print(tree_structure).
Отчет о классификации:
поддержка точного отзыва по шкале f1
Код: Выделить всё
Outcome 1 0.39 0.88 0.54 388
Outcome 2 0.95 0.61 0.75 1397
accuracy 0.67 1785
macro avg 0.67 0.75 0.64 1785
weighted avg 0.83 0.67 0.70 1785
[[343 45]
[540 857]]
Точность для класса «Результат 1»: 88,40%
Точность для класса «Результат 2»: 61,35%
Точность классификатора дерева решений: 0,67
Хотелось бы, чтобы общая точность и «Результат 2» были выше. Я думаю, проблема в том, что результат 1 здесь меньшинство, и по какой-то причине дерево решений делает точность из-за этого ниже, хотя оно должно быть сбалансированным
Подробнее здесь: https://stackoverflow.com/questions/792 ... ision-tree
Мобильная версия