0% found this document useful (0 votes)
32 views3 pages

Modelling and Simulation Sample Model 4

The document shows how to use a random forest classifier with scikit-learn to classify synthetic data and calculate the accuracy of the model's predictions on test data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views3 pages

Modelling and Simulation Sample Model 4

The document shows how to use a random forest classifier with scikit-learn to classify synthetic data and calculate the accuracy of the model's predictions on test data.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

import numpy as np

from sklearn.model_selection import train_test_split

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import accuracy_score

# Generate some synthetic data

np.random.seed(42)

X = np.random.rand(100, 2) # Features

y = (X[:, 0] + X[:, 1] > 1).astype(int) # Labels

# 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 Random Forest classifier

rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)

# Train the model on the training data

rf_classifier.fit(X_train, y_train)

# Make predictions on the test data

y_pred = rf_classifier.predict(X_test)

# Calculate the accuracy of the model

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")import numpy as np

from sklearn.model_selection import train_test_split

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import accuracy_score


# Generate some synthetic data

np.random.seed(42)

X = np.random.rand(100, 2) # Features

y = (X[:, 0] + X[:, 1] > 1).astype(int) # Labels

# 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 Random Forest classifier

rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)

# Train the model on the training data

rf_classifier.fit(X_train, y_train)

# Make predictions on the test data

y_pred = rf_classifier.predict(X_test)

# Calculate the accuracy of the model

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")import numpy as np

from sklearn.model_selection import train_test_split

from sklearn.ensemble import RandomForestClassifier

from sklearn.metrics import accuracy_score

# Generate some synthetic data

np.random.seed(42)

X = np.random.rand(100, 2) # Features

y = (X[:, 0] + X[:, 1] > 1).astype(int) # Labels


# 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 Random Forest classifier

rf_classifier = RandomForestClassifier(n_estimators=100, random_state=42)

# Train the model on the training data

rf_classifier.fit(X_train, y_train)

# Make predictions on the test data

y_pred = rf_classifier.predict(X_test)

# Calculate the accuracy of the model

accuracy = accuracy_score(y_test, y_pred)

print(f"Accuracy: {accuracy:.2f}")

You might also like