Skip to content

Commit 828a081

Browse files
[pre-commit.ci] auto fixes from pre-commit.com hooks
for more information, see https://pre-commit.ci
1 parent 27b9ea6 commit 828a081

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

machine_learning/best_random_state_in_random_forest.py

+12-6
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
from sklearn.ensemble import RandomForestClassifier
66
from sklearn.preprocessing import StandardScaler
77

8-
warnings.filterwarnings('ignore')
8+
warnings.filterwarnings("ignore")
99

1010

11-
def find_best_random_state(data: pd.DataFrame, target_column: str, iterations: int = 200) -> int:
11+
def find_best_random_state(
12+
data: pd.DataFrame, target_column: str, iterations: int = 200
13+
) -> int:
1214
"""
1315
Find the best random state for the Random Forest Classifier that maximizes accuracy.
1416
@@ -25,7 +27,9 @@ def find_best_random_state(data: pd.DataFrame, target_column: str, iterations: i
2527
target = data[target_column]
2628

2729
# Split dataset into train and test sets
28-
X_train, X_test, y_train, y_test = train_test_split(predictors, target, test_size=0.20, random_state=0)
30+
X_train, X_test, y_train, y_test = train_test_split(
31+
predictors, target, test_size=0.20, random_state=0
32+
)
2933

3034
# Scale features
3135
scaler = StandardScaler()
@@ -40,19 +44,21 @@ def find_best_random_state(data: pd.DataFrame, target_column: str, iterations: i
4044
rf = RandomForestClassifier(random_state=random_state)
4145
rf.fit(X_train_scaled, y_train)
4246
y_pred_rf = rf.predict(X_test_scaled)
43-
47+
4448
current_accuracy = round(accuracy_score(y_test, y_pred_rf) * 100, 2)
4549
if current_accuracy > max_accuracy_rf:
4650
max_accuracy_rf = current_accuracy
4751
best_random_state = random_state
4852

49-
print(f"The best random state is: {best_random_state} with an accuracy score of: {max_accuracy_rf} %")
53+
print(
54+
f"The best random state is: {best_random_state} with an accuracy score of: {max_accuracy_rf} %"
55+
)
5056
return best_random_state
5157

5258

5359
if __name__ == "__main__":
5460
# Load dataset
5561
dataset = pd.read_csv("heart.csv")
56-
62+
5763
# Find the best random state
5864
best_state = find_best_random_state(dataset, target_column="target")

0 commit comments

Comments
 (0)