from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import mean_absolute_error, mean_squared_error, accuracy_score
import matplotlib.pyplot as plt
# Read the data from the provided file
data = pd.read_csv('your_file_path.csv')
# Separate the predictor variables (X) and the target variable (y)
X = data.iloc[:, :-1] # Select all columns except the last one
y = data.iloc[:, -1] # Select the last column
# 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, oob_score=True, random_state=42)
# Fit the classifier to the training data
rf_classifier.fit(X_train, y_train)
# Make predictions on the training set
y_train_pred = rf_classifier.predict(X_train)
# Calculate the out-of-bag mean error
oob_error = 1 - rf_classifier.oob_score_
# Make predictions on the testing set
y_test_pred = rf_classifier.predict(X_test)
# Calculate the out-of-bag mean squared error
oob_mse = mean_squared_error(y_train, y_train_pred)
# Calculate the out-of-bag classification error
oob_classification_error = 1 - accuracy_score(y_train, y_train_pred)
# Calculate the accuracy on the training set
train_accuracy = accuracy_score(y_train, y_train_pred)
# Calculate the accuracy on the testing set
test_accuracy = accuracy_score(y_test, y_test_pred)
# Get the feature importance from the trained model
feature_importance = rf_classifier.feature_importances_
# Plot the feature importance
plt.figure(figsize=(10, 6))
plt.bar(range(len(feature_importance)), feature_importance, tick_label=X.columns)
plt.xlabel('Predictor Variables')
plt.ylabel('Feature Importance')
plt.title('Random Forest Feature Importance')
# Print the calculated metrics
print("Out-of-Bag Mean Error:", oob_error)
print("Out-of-Bag Mean Squared Error:", oob_mse)
print("Out-of-Bag Classification Error:", oob_classification_error)
print("Training Accuracy:", train_accuracy)
print("Testing Accuracy:", test_accuracy)