Home >Backend Development >Python Tutorial >How to write a random forest algorithm in Python?
How to write a random forest algorithm in Python?
Random forest is a powerful machine learning method commonly used for classification and regression problems. The algorithm makes predictions by randomly selecting features and randomly sampling samples, building multiple decision trees, and integrating their results.
This article will introduce how to use Python to write the random forest algorithm and provide specific code examples.
import numpy as np import pandas as pd from sklearn.ensemble import RandomForestClassifier from sklearn.model_selection import train_test_split from sklearn.metrics import accuracy_score
data = pd.read_csv("iris.csv")
# 将特征和标签分开 X = data.drop('species', axis=1) y = data['species'] # 将分类变量转换成数值变量 y = pd.factorize(y)[0]
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
rf = RandomForestClassifier(n_estimators=100, random_state=42) rf.fit(X_train, y_train)
y_pred = rf.predict(X_test) accuracy = accuracy_score(y_test, y_pred) print("Accuracy:", accuracy)
The above is a complete code example of writing a random forest algorithm in Python. Through these codes, we can easily build and train random forest models, and perform prediction and performance evaluation.
Summary:
Random forest is a powerful machine learning method that can effectively solve classification and regression problems. Writing a random forest algorithm in Python is very simple. You only need to import the corresponding library, load data, preprocess the data, divide the training set and test set, build and train the model, and finally perform prediction and performance evaluation. The above code examples can help readers quickly get started with the writing and application of the random forest algorithm.
The above is the detailed content of How to write a random forest algorithm in Python?. For more information, please follow other related articles on the PHP Chinese website!