r/CS_Questions 5d ago

python model accuracy help

Hi!! I am coding a classifier for data and am currently using the random forest classifier. This is currently my code(vibe coded) and unfortunately it is currently at approximately 75% accuracy with a hidden test. I need to get about ~80% accuracy and am wondering if it would be possible if someone could take a look at it and lmk if there are any errors and how i could fix them. Thank you so much!

from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import RandomizedSearchCV
from sklearn.metrics import accuracy_score

# --- quick feature engineering ---
def add_features(df):
df = df.copy()
df['X_Range'] = df['X_Maximum'] - df['X_Minimum']
df['Y_Range'] = df['Y_Maximum'] - df['Y_Minimum']
df['Aspect_Ratio'] = df['X_Range'] / (df['Y_Range'] + 1)
df['Fill_Ratio'] = df['Pixels_Areas'] / (df['X_Range'] * df['Y_Range'] + 1)
df['Perimeter_to_Area'] = (df['X_Perimeter'] + df['Y_Perimeter']) / (df['Pixels_Areas'] + 1)
df['Luminosity_Range'] = df['Maximum_of_Luminosity'] - df['Minimum_of_Luminosity']
return df

train = add_features(train)
test = add_features(test)

feature_columns = [column for column in test.columns if column != 'id']
X_train = train[feature_columns]
y_train = train['fault_type']
X_test = test[feature_columns]

# Same grid as before, just handed to RandomizedSearchCV with n_iter to control runtime
param_grid = {
'n_estimators': [100, 250, 500],
'max_depth': [10, 20, 30, 40],
'min_samples_split': [2, 5, 10, 20],
'min_samples_leaf': [1, 2, 4, 8],
'max_features': ['sqrt', 'log2', 0.8]
}

rf_model = RandomForestClassifier(random_state=42, class_weight='balanced')

grid_search = RandomizedSearchCV(
estimator=rf_model,
param_distributions=param_grid,
n_iter=60,          # samples 60 of the 576 combos instead of all of them
cv=5,
n_jobs=-1,
verbose=2,
scoring='accuracy',
random_state=42
)

grid_search.fit(X_train, y_train)

best_model = grid_search.best_estimator_

predictions = best_model.predict(X_test)

predictions[:10]

2 Upvotes

1 comment sorted by

1

u/domain-nam 1d ago

75% isn't bad for RF honestly. Check if your ratio features are just duplicating XRange/Y_Range , could be hurting more than helping. Also worth running feature_importances, sometimes half the engineered stuff is dead weight.