You can download this code by clicking the button below.
This code is now available for download.
This function uses the random forest algorithm for classification and calculates the model's accuracy. First, the dataset is split into training and test sets, then an instance of the random forest classifier is created, followed by training the model, and finally, predictions are made on the test set and accuracy is calculated.
Technology Stack : Scikit-learn (Random Forest, Model Selection, Accuracy Calculation)
Code Type : Function
Code Difficulty : Intermediate
def random_forest_classification(data, target):
from sklearn.ensemble import RandomForestClassifier
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
# 分割数据集
X_train, X_test, y_train, y_test = train_test_split(data, target, test_size=0.3, random_state=42)
# 创建随机森林分类器实例
clf = RandomForestClassifier(n_estimators=100, random_state=42)
# 训练模型
clf.fit(X_train, y_train)
# 预测
y_pred = clf.predict(X_test)
# 计算准确率
accuracy = accuracy_score(y_test, y_pred)
return accuracy