Python , I need to Implement a Random Forest Classification Model in Python from any open dataset available online
#Text code
# importing the libraries
import pandas as pd
from sklearn.metrics import accuracy_score
# reading csv file "heart.csv"
# file source: https://www.kaggle.com/ronitf/heart-disease-uci#heart.csv
dataset = pd.read_csv("heart.csv")
# extracting features
# “iloc” in pandas is used to select rows and columns by number
X = dataset.iloc[:, :-1]
y = dataset.iloc[:, -1]
# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 0)
# Feature Scaling
from sklearn.preprocessing import StandardScaler
# Scale X object
sc_X = StandardScaler()
X_train = sc_X.fit_transform(X_train)
X_test = sc_X.fit_transform(X_test)
# Fitting Random Forest Classification to the Training set
from sklearn.ensemble import RandomForestClassifier
random_forest_classifier = RandomForestClassifier(n_estimators = 10, criterion = 'entropy', random_state = 0)
random_forest_classifier.fit(X_train, y_train)
# Predicting the Test set results
y_pred_random_forest = random_forest_classifier.predict(X_test)
# finding percentage accuracy
print(accuracy_score(y_test, y_pred_random_forest))
# Output: 0.8688524590163934
#CODE SCREENSHOT
Get Answers For Free
Most questions answered within 1 hours.