RandomForest Classifier Example using SkLearn in Python

An example code of Sklearn to do basic data analysis and inference. This is just a simplified example, and in a real-world scenario, there would be more complex data and additional features to consider.

# Import necessary libraries
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score

# Sample data represented as a list of dictionaries
data = [
    {'amount': 100.0, 'merchant': 'Online Retail', 'is_fraud': 0},
    {'amount': 50.0, 'merchant': 'Coffee Shop', 'is_fraud': 0},
    {'amount': 200.0, 'merchant': 'Electronics Store', 'is_fraud': 1},
    {'amount': 75.0, 'merchant': 'Grocery Store', 'is_fraud': 0},
    {'amount': 300.0, 'merchant': 'Online Retail', 'is_fraud': 1},
    # Add more transaction data as needed

# Prepare the data
X = []
y = []

for transaction in data:

# Split the data into training and testing sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)

# Create a machine learning model (Random Forest, in this case)
model = RandomForestClassifier()

# Train the model on the training data
model.fit(X_train, y_train)

# Make predictions on the test data
predictions = model.predict(X_test)

# Calculate accuracy
accuracy = accuracy_score(y_test, predictions)

# Print the accuracy
print(f'Accuracy: {accuracy * 100:.2f}%')
Thanks for reading and happy learning! billy-at-python.sg

Leave a Reply 0

Your email address will not be published. Required fields are marked *