text_html_output_1.png text_plain_output_1.png
from sklearn.ensemble import RandomForestClassifier

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import accuracy_score, confusion_matrix, classification_report

from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, classification_report

from sklearn.model_selection import GridSearchCV

from sklearn.model_selection import train_test_split

from sklearn.model_selection import train_test_split, GridSearchCV, StratifiedKFold

from sklearn.preprocessing import LabelEncoder

from sklearn.preprocessing import StandardScaler

import pandas as pd

import pandas as pd

df_train = pd.read_csv('/kaggle/input/playground-series-s3e13/train.csv')
df_test = pd.read_csv('/kaggle/input/playground-series-s3e13/test.csv')

df_train = df_train.drop('id', axis=1)
df_test = df_test.drop('id', axis=1)
df_train['prognosis'] = pd.Categorical(df_train['prognosis'])
X_train = df_train.drop('prognosis', axis=1)
y_train = df_train['prognosis']
scaler = StandardScaler()
X_train_scaled = scaler.fit_transform(X_train)
X_test_scaled = scaler.transform(df_test)
X_train, X_val, y_train, y_val = train_test_split(X_train_scaled, y_train, test_size=0.2, random_state=42)

params = {'n_estimators': [100, 200, 300], 'max_depth': [5, 10, 15, None], 'min_samples_split': [2, 5, 10], 'min_samples_leaf': [1, 2, 4]}
rf = RandomForestClassifier(random_state=42)
skf = StratifiedKFold(n_splits=5, shuffle=True, random_state=42)
grid_search = GridSearchCV(rf, params, cv=skf, n_jobs=-1, verbose=2)
grid_search.fit(X_train, y_train)
best_params = grid_search.best_params_
best_score = grid_search.best_score_

y_pred_val = grid_search.predict(X_val)
acc_score_val = accuracy_score(y_val, y_pred_val)
class_report = classification_report(y_val, y_pred_val)

import pandas as pd
from sklearn.preprocessing import LabelEncoder
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, precision_score, recall_score, f1_score, classification_report
from sklearn.model_selection import GridSearchCV
df_train = pd.read_csv('/kaggle/input/playground-series-s3e13/train.csv')
df_test = pd.read_csv('/kaggle/input/playground-series-s3e13/test.csv')
le = LabelEncoder()
df_train['prognosis'] = le.fit_transform(df_train['prognosis'])
X_train, X_val, y_train, y_val = train_test_split(df_train.drop(['id', 'prognosis'], axis=1), df_train['prognosis'], test_size=0.2, random_state=42)
rfc = RandomForestClassifier(random_state=42)
rfc.fit(X_train, y_train)
y_pred_val = rfc.predict(X_val)
acc_score_val = accuracy_score(y_val, y_pred_val)
prec_score_val = precision_score(y_val, y_pred_val, average='weighted')
rec_score_val = recall_score(y_val, y_pred_val, average='weighted')
f1_score_val = f1_score(y_val, y_pred_val, average='weighted')
class_report_val = classification_report(y_val, y_pred_val)
print('Validation Accuracy Score:', acc_score_val)
print('Validation Precision Score:', prec_score_val)
print('Validation Recall Score:', rec_score_val)
print('Validation F1 Score:', f1_score_val)
print('Validation Classification Report:\n', class_report_val)
param_grid = {'n_estimators': [10, 50, 100, 200], 'max_depth': [None, 10, 20, 30], 'min_samples_split': [2, 5, 10], 'min_samples_leaf': [1, 2, 4]}
grid_search = GridSearchCV(estimator=rfc, param_grid=param_grid, cv=5)
grid_search.fit(X_train, y_train)
rfc_final = grid_search.best_estimator_
rfc_final.fit(df_train.drop(['id', 'prognosis'], axis=1), df_train['prognosis'])
y_pred_test = rfc_final.predict(df_test.drop(['id'], axis=1))
submission_df = pd.DataFrame({'id': df_test['id'], 'prognosis': le.inverse_transform(y_pred_test)})
submission_df