����λ�ã���ҳ > �����̳� > �̳� > ���É��R����Ԥ�⹤��
������ϸ������R���Խ���Ԥ��Ĵ���ʾ�����Լ����É��R���Ե�Ӧ��ʵ����ͬʱ��ϸ���������É�ֵ�Ӧ��ʵ������������ϸ�Ĵ���ʾ�����������⣬�ɻ�������
���É�֣�Random Forest����һ�ֻ��ھ������ļ���ѧϰ��������ͨ������������������������ǵ�Ԥ���������Ԥ���׼ȷ�ԡ���R�����У����ǿ���ʹ��
randomForest
����������ѵ�����É��ģ�͡������Ƕ����É�ֵ���ϸ�����Լ�ʹ��R���Խ���Ԥ��Ĵ���ʾ����
���É��ͨ�����²�����й�����
��1�� ������������Bootstrap Sampling�� ����Ôʼ���ݼ����зŻص������ȡ���������������ѵ����þ�������
��2�� �������ѡ�� ����ѵ��ÿ�þ�����ʱ�����������������ѡ��һ�����������нڵ���ѡ�
��3�� ���������� �����������������õ��������������ѡ�����������������þ�������
��4�� ����Ԥ�� �����ڷ������⣬ͨ��ͶƱ��������ͶƱ���������о�������Ԥ���������ڻع����⣬ͨ��ȡƽ��ֵ�������о�������Ԥ������
���É�ֵ��ŵ������
������һ��ʹ��R�����е�
randomForest
���������É��Ԥ��Ĵ���ʾ����
# ��װrandomForest���������δ��װ��
install.packages("randomForest")
# ����randomForest��
library(randomForest)
# �������ݼ���������iris���ݼ�Ϊ����
data(iris)
# ����ѵ�����Ͳ��Լ�
set.seed(123) # ������������Ա�֤����Ŀ��ظ���
train_index <- sample(1:nrow(iris), nrow(iris)*0.7) # ���ѡ��70%��������Ϊѵ����
train_data <- iris[train_index,]
test_data <- iris[-train_index,]
# ʹ��randomForest����ѵ�����É���
# ntreeָ����������������mtryָ��ÿ�η���ʱ���ѡ�����������
model <- randomForest(Species ~ ., data=train_data, ntree=500, mtry=2)
# ʹ��ѵ���õ�ģ�ͶԲ��Լ�����Ԥ��
predictions <- predict(model, newdata=test_data)
# ���������
# ���ڷ������⣬���Լ���׼ȷ�ʡ����������ָ��
confusionMatrix <- table(predictions, test_data$Species)
accuracy <- sum(diag(confusionMatrix)) / sum(confusionMatrix)
print(paste("Accuracy:", accuracy))
# �����Ҫ�������Ի���������Ҫ��ͼ
# importance(model) # ����������Ҫ�Ծ���
# plot(importance(model)) # ����������Ҫ��ͼ
���É����ʵ��Ӧ���о��й㷺�����壬�ر����ڴ����������ݼ��ͽ���Ԥ�����ʱ�����磬��������Ϣѧ��ҽѧ��ϡ�����Ԥ����������É�ֿ������ڷ��ࡢ�ع顢����ѡ������⡣ͨ�����ɶ�þ�������Ԥ���������É�ֿ������Ԥ���׼ȷ�ԣ������͹���ϵķ��ա����⣬���É�ֻ������ṩ������Ҫ������������������������Щ������Ԥ����������ҪӰ�졣
��̸�����É�ֵ�Ӧ��ʵ��ʱ��������һЩ����ij����Լ����ʹ��R�����е�
randomForest
����ʵ����Щʵ������ϸ����ʾ����
breastCancer
��
����������һ�����ٰ����ݼ������а���һЩ�방֢��ص�������һ�������������Ƿ�Ϊ���ԣ������ǵ�Ŀ����ѵ��һ�����É��ģ����Ԥ���µIJ����Ƿ�Ϊ���ԡ�
# ���ر�Ҫ�İ�
library(randomForest)
# �������ݼ���������������Ѿ�����breastCancer���ݼ���
# �����Ҫ�����Դ��ⲿ����Դ���أ���read.csv
data(breastCancer, package = "mlbench") # ����breastCancer��mlbench����
# ����ѵ�����Ͳ��Լ�
set.seed(123) # Ϊ�˽���Ŀɸ�����
trainIndex <- sample(1:nrow(breastCancer), nrow(breastCancer)*0.7)
trainData <- breastCancer[trainIndex, ]
testData <- breastCancer[-trainIndex, ]
# ʹ�����É��ģ�ͽ���ѵ��
rfModel <- randomForest(Class ~ ., data = trainData, ntree = 500, importance = TRUE)
# �ڲ��Լ��Ͻ���Ԥ��
predictions <- predict(rfModel, newdata = testData)
# �鿴���������׼ȷ��
confusionMatrix <- table(predictions, testData$Class)
accuracy <- sum(diag(confusionMatrix)) / sum(confusionMatrix)
print(paste("Accuracy:", accuracy))
# �鿴������Ҫ��
importance(rfModel)
# ����������Ҫ��ͼ
plot(rfModel, main="Feature Importance")
housingData
��
����������һ���������ݼ������а������ݵĸ�������������������������ضεȣ��ͷ��ݵļ۸����ǵ�Ŀ����Ԥ���·��ݵļ۸�
# ���ر�Ҫ�İ�
library(randomForest)
# ����housingData�Ѿ����ص�R������
# �����Ҫ�����Դ��ⲿ����Դ���أ���read.csv
# ����������Ŀ�����
features <- housingData[, -ncol(housingData)] # �������һ���Ǽ۸�
prices <- housingData[, ncol(housingData)]
# ����ѵ�����Ͳ��Լ�
set.seed(123)
trainIndex <- sample(1:nrow(housingData), nrow(housingData)*0.7)
trainFeatures <- features[trainIndex, ]
trainPrices <- prices[trainIndex]
testFeatures <- features[-trainIndex, ]
testPrices <- prices[-trainIndex]
# ʹ�����É��ģ�ͽ���ѵ��
rfModel <- randomForest(trainPrices ~ ., data = data.frame(trainPrices, trainFeatures), ntree = 500, importance = TRUE)
# �ڲ��Լ��Ͻ���Ԥ��
predictedPrices <- predict(rfModel, newdata = data.frame(testPrices = rep(NA, nrow(testFeatures)), testFeatures))
# ����Ԥ���������磬ʹ�þ�����
mse <- mean((predictedPrices - testPrices)^2)
print(paste("Mean Squared Error:", mse))
# �鿴������Ҫ��
importance(rfModel)
# ����������Ҫ��ͼ
plot(rfModel, main="Feature Importance")
��ע�⣬��������ʾ���е����ݼ���
breastCancer
��
housingData
���Ǽ���ģ����ҿ�����Ҫ���ⲿ����Դ���ء����⣬���ڷ���Ԥ�⣬���Ǽ���۸��������ݼ������һ�У�������ʵ��Ӧ���п�����Ҫ��һ��������Ԥ�������������̡�ͬ�������É�ֵIJ�������
ntree
��Ҳ���Ը��ݾ���������е�����
��R�����У����ǿ���ʹ�ö��ְ�������Ԥ�⣬����
randomForest
��
caret
��
e1071
������SVM����
glmnet
�����ڵ�������ع飩�ȡ������ҽ���������ʹ��R���Խ���Ԥ��Ĵ���ʾ����
���ȣ�������Ҫ��װ������
randomForest
���������δ��װ����
# ��װrandomForest���������δ��װ��
install.packages("randomForest")
# ����randomForest��
library(randomForest)
# ���ػ򴴽�����
# ��������ʹ��iris���ݼ���Ϊʾ��
data(iris)
# �����ݼ�����Ϊѵ�����Ͳ��Լ�
set.seed(123) # Ϊ�˽���Ŀ��ظ���
train_index <- sample(1:nrow(iris), 0.8 * nrow(iris))
train_data <- iris[train_index, ]
test_data <- iris[-train_index, ]
# ʹ��ѵ����ѵ�����É���
rf_model <- randomForest(Species ~ ., data = train_data, ntree = 500)
# ʹ�ò��Լ�����Ԥ��
rf_predictions <- predict(rf_model, newdata = test_data)
# �鿴Ԥ����
print(table(test_data$Species, rf_predictions))
# ����Ԥ��׼ȷ��
accuracy <- sum(test_data$Species == rf_predictions) / nrow(test_data)
print(paste("Accuracy:", accuracy))
# ����MASS���������δ��װ��
# MASS�������������߼��ع�Ķ�����ݼ�
install.packages("MASS")
library(MASS)
# ʹ��MASS���е�Pima Indians Diabetes���ݼ�
data(PimaIndiansDiabetes)
# �����ݼ�����Ϊѵ�����Ͳ��Լ�
set.seed(123)
train_index <- sample(1:nrow(PimaIndiansDiabetes), 0.8 * nrow(PimaIndiansDiabetes))
train_data <- PimaIndiansDiabetes[train_index, ]
test_data <- PimaIndiansDiabetes[-train_index, ]
# ʹ��ѵ����ѵ���߼��ع�ģ��
glm_model <- glm(diabetes ~ ., data = train_data, family = binomial)
# ʹ�ò��Լ�����Ԥ�⣨ע�⣺�߼��ع�Ԥ����Ǹ��ʣ���Ҫת��Ϊ���
glm_probabilities <- predict(glm_model, newdata = test_data, type = "response")
glm_predictions <- ifelse(glm_probabilities > 0.5, "pos", "neg")
# �鿴Ԥ����
print(table(test_data$diabetes, glm_predictions))
# ����Ԥ��׼ȷ�ʣ�����'pos'�������࣬'neg'�������ࣩ
accuracy <- sum(test_data$diabetes == (glm_predictions == "pos")) / nrow(test_data)
print(paste("Accuracy:", accuracy))
# ��װe1071���������δ��װ��
install.packages("e1071")
library(e1071)
# ʹ��iris���ݼ�
data(iris)
# �����ݼ�����Ϊѵ�����Ͳ��Լ�
set.seed(123)
train_index <- sample(1:nrow(iris), 0.8 * nrow(iris))
train_data <- iris[train_index, ]
test_data <- iris[-train_index, ]
# ��Speciesת��Ϊ�������ͣ������δ�ǣ�
train_data$Species <- as.factor(train_data$Species)
test_data$Species <- as.factor(test_data$Species)
# ʹ��ѵ����ѵ��SVMģ��
svm_model <- svm(Species ~ ., data = train_data, kernel = "radial", cost = 10, gamma = 0.1)
# ʹ�ò��Լ�����Ԥ��
svm_predictions <- predict(svm_model, newdata = test_data)
# �鿴Ԥ����
print(table(test_data$Species, svm_predictions))
# ����Ԥ��׼ȷ��
accuracy <- sum(test_data$Species == svm_predictions) / nrow(test_data)
print(paste("Accuracy:", accuracy))
���ϴ���ʾ��չʾ�������R������ʹ�����É�֡��߼��ع��֧������������Ԥ�⣬��������Ԥ��׼ȷ�ʡ���ע�⣬��Щʾ��ʹ�������õ����ݼ�
�β�����ݼ���һ�����õķ������ݼ�������150��������ÿ���������ĸ����������೤�ȡ�������ȡ����곤�ȡ�������ȣ������ڷ��������β����
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score
# �����β�����ݼ�
iris = load_iris()
X = iris.data
y = iris.target
# ����ѵ�����Ͳ��Լ�
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, 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)
print(f"Accuracy: {accuracy}")
����������һ���������ݼ����������ݵ����������������������¥�����ȣ��Ͷ�Ӧ�ķ��ۡ�
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestRegressor
from sklearn.metrics import mean_squared_error
# �������ݣ��������������һ��CSV�ļ���
data = pd.read_csv('housing_data.csv')
X = data.drop('price', axis=1) # ����
y = data['price'] # Ŀ�����
# ����ѵ�����Ͳ��Լ�
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# �������É�ֻع���
rf_regressor = RandomForestRegressor(n_estimators=100, random_state=42)
# ѵ��ģ��
rf_regressor.fit(X_train, y_train)
# Ԥ����Լ�
y_pred = rf_regressor.predict(X_test)
# ����������
mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse}")
����������һ����Ӱ�������ݼ������������ı��Ͷ�Ӧ����б�ǩ��������棩��
from sklearn.datasets import fetch_20newsgroups
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
# �������ݼ�������ʹ��20 Newsgroups���ݼ���һ���Ӽ���Ϊʾ����
categories = ['alt.atheism', 'soc.religion.christian']
newsgroups_train = fetch_20newsgroups(subset='train', categories=categories, shuffle=True, random_state=42)
X_train, y_train = newsgroups_train.data, newsgroups_train.target
# �ı�������ȡ������ʹ�ô�Ƶ����������
vectorizer = CountVectorizer()
X_train_counts = vectorizer.fit_transform(X_train)
# ����ѵ�����Ͳ��Լ�������Ϊ�˼򻯣�ֱ�Ӵ�ѵ�����л��֣�
X_train_counts, X_test_counts, y_train, y_test = train_test_split(X_train_counts, y_train, test_size=0.2, random_state=42)
# �������É�ַ�����
clf = RandomForestClassifier(n_estimators=100, random_state=42)
# ѵ��ģ��
clf.fit(X_train_counts, y_train)
# Ԥ����Լ�
y_pred = clf.predict(X_test_counts)
# �����
print(classification_report(y_test, y_pred
��Ȼ���É��ͨ����ֱ������Ôʼ���ؼ����ͼ����ࣨ��Ϊ���ַ����ڴ�����ά����ʱ���ܲ�����Ч���������ǿ���ʹ�����É��������ͼ����������HOG��SIFT��SURF�������������ߴ�Ԥѵ�������ѧϰģ������ȡ��������
������һ���򻯵����ӣ����������Ѿ�����һ������ͼ�������Ͷ�Ӧ��ǩ�����ݼ���
from sklearn.model_selection import train_test_split
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import classification_report
import numpy as np
# ���������Ѿ�����һ����������X�����磬��ͼ������ȡ���������ͱ�ǩy
# X = ... (��״Ϊ (n_samples, n_features) ��NumPy����)
# y = ... (��״Ϊ (n_samples,) ��NumPy����)
# Ϊ����ʾ�������������һЩģ������
n_samples = 1000
n_features = 64 # ����ÿ��ͼ�񱻱�ʾΪһ��64ά����������
X = np.random.rand(n_samples, n_features)
y = np.random.randint(0, 2, n_samples) # ����������
# ����ѵ�����Ͳ��Լ�
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# �������É�ַ�����
clf = RandomForestClassifier(n_estimators=100, random_state=42)
# ѵ��ģ��
clf.fit(X_train, y_train)
# Ԥ����Լ�
y_pred = clf.predict(X_test)
# �����
print(classification_report(y_test, y_pred))
���É�ֲ����������ڷ���ͻع����ñ£¬»ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Òªï¿½Ô¡ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ñ¡ï¿½ï¿½Í½ï¿½ï¿½ï¿½Ä£ï¿½Í½ï¿½ï¿½ï¿½Ç³ï¿½ï¿½ï¿½ï¿½Ã¡ï¿½
# ʹ��֮ǰ���β�����ݼ�ʾ��
# ...���������ݡ�����ѵ�����Ͳ��Լ���ѵ��ģ�͵Ĵ��룩
# ��ȡ������Ҫ��
importances = clf.feature_importances_
std = np.std([tree.feature_importances_ for tree in clf.estimators_], axis=0)
indices = np.argsort(importances)[::-1]
# ��ӡ��������
print("Feature ranking:")
for f in range(X.shape[1]):
print("%d. feature %d (%f)" % (f + 1, indices[f], importances[indices[f]]))
# ���ǿ���ʹ����Щ������Ҫ������������ͼ�����߸�����Ҫ��ѡ����ų�ijЩ����
���ϴ���ʾ��չʾ�����É���ڲ�ͬ�����µ�Ӧ�ã��������ࡢ�ع顢������Ҫ�������ȡ�ע�⣬��Щʾ���е����ݺ���������ģ��Ļ�ò»¯µÄ£ï¿½Êµï¿½ï¿½Ó¦ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Òªï¿½ï¿½ï¿½ï¿½ï¿½Ô¼ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ý¼ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ë¡£
���É��Ҳ���������쳣������Ⱥ���⡣ͨ���������É��ģ�Ͳ�����ÿ����������Ҷ�ڵ��ƽ�����루���磬ʹ�ù���É�� Isolation Forest�������ǿ���ʶ���������������ͬ���쳣�㡣
������һ��ʹ��
sklearn-extensions
���е�
IsolationForest
�����쳣����ʾ����ע�⣺
sklearn-extensions
������
scikit-learn
�ٷ����һ���֣����ṩ�����Ƶ�ʵ�֣���
from sklearn_extensions.ensemble import IsolationForest
import numpy as np
# ���� X �����ǵ���������������������һЩģ������
X = np.random.normal(size=(100, 2))
# ����һ���쳣��
X = np.r_[X + 2, np.array([[10, 10]])]
# ���� IsolationForest ʵ��
clf = IsolationForest(contamination=0.1) # �������ݼ�����10%���쳣��
# ����
clf.fit(X)
# Ԥ���쳣����������Խ�ͣ�Խ�������쳣�㣩
y_pred = clf.predict(X)
scores = clf.decision_function(X)
# ��ӡ�쳣������Ԥ����
for i, s in enumerate(scores):
print(f"Sample {i}: Score = {s}, Prediction = {y_pred[i]}")
# ���ǿ�������һ����ֵ��ʶ���쳣��
threshold = -0.5 # �����ֵ��Ҫ�������ǵ����ݺ�����������
outliers = X[scores < threshold]
print(f"Outliers: \n{outliers}")
��ע�⣬�����
IsolationForest
����ܲ���
scikit-learn
�ٷ����һ���֣������ǿ���ʹ��
scikit-learn
�е�
OneClassSVM
��
LocalOutlierFactor
��ʵ�����ƵĹ��ܡ�
���É��Ҳ�������ڶ��ǩ�������ñ£¬¼ï¿½Ã¿ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Ú¶ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½Í¨ï¿½ï¿½Í¨ï¿½ï¿½Ê¹ï¿½Ã¶ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½ï¿½multi-output classifier����ʵ�֣��÷�����Ϊÿ����ǩѵ��һ�������ķ�������
from sklearn.datasets import make_multilabel_classification
from sklearn.ensemble import RandomForestClassifier
from sklearn.metrics import accuracy_score, precision_recall_fscore_support
# ����һ�����ǩ�������ݼ�
X, y = make_multilabel_classification(n_samples=1000, n_features=20, n_classes=5, n_labels=2, random_state=42)
# ����ѵ�����Ͳ��Լ�
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
# �������É�ַ�������Ϊÿ����ǩѵ��һ��������
clf = RandomForestClassifier(n_estimators=100, random_state=42)
# ѵ��ģ��
clf.fit(X_train, y_train)
# Ԥ����Լ�
y_pred = clf.predict(X_test)
# ����ÿ����ǩ�ľ��ȡ��ٻ��ʺ�F1����
precision, recall, fscore, support = precision_recall_fscore_support(y_test, y_pred, average=None)
# ��ӡ���
for i in range(y.shape[1]):
print(f"Label {i}: Precision = {precision[i]}, Recall = {recall[i]}, F1 Score = {fscore[i]}")
# ע�⣺���ڶ��ǩ���࣬ͨ�������������׼ȷ�ʣ���Ϊ��ǩ֮������Ƕ�����
��Щʾ��չʾ�����É���ڶ��ֲ�ͬ�����µ�Ӧ�ã������쳣��⡢���ǩ����ȡ���ʵ��Ӧ���У����ǿ�����Ҫ���ݾ�����������ݼ�����ģ�͵IJ��������á�
ʹ��Blender���ɳ���ģ��
�Ķ�ȫ����������ERA5�����ط���
�Ķ�Xpath���������﷨
�Ķ�����ѧϰ�������繹�����£�
�Ķ���ΪMateƷ��ʢ�䣺HarmonyOS NEXT�ӳ�����Ϸ���ܵõ�����ͷ�
�Ķ�ʵ�ֶ��󼯺���DataTable���໥ת��
�Ķ�Ӳ�̵Ļ���֪ʶ��ѡ��ָ��
�Ķ�������й��ƶ��ı�ͼ��ײ�
�Ķ�����NEXTԪ�����������ѿ����ϼ���Ʒ
�Ķ��ᳲ���С������������Ƽ��رշ���
�Ķ������ArcMap�����н���դ��ͼ���ز�������
�Ķ��㷨�����ݽṹ 1 - ģ��
�Ķ���Ѷ�����߿ͷ���Ӫ��ϵͳ����
�Ķ���Ѷ��Ƶҹ��ģʽ���ý̳�
�Ķ����ں���NEXT��Ѫ���Ŵ���������������
�Ķ�5. Spring Cloud OpenFeign ����ʽ WebService �ͻ��˵ij���ϸʹ��
�Ķ�Java����ģʽ����̬�����Ͷ�̬�����ĶԱȷ���
�Ķ�Win11�ʼDZ����Զ�����Ӧ�õ���ɫ����ʾ����
�Ķ�˼�� V1.5.6 ��׿��
��ս�귨 V7.5.0 ��׿��
У��������������׵������� V1.0 ��׿��
��˸֮�� V1.9.7 ��׿��
������Ե����� v1.0.4 ��׿��
������֮ŠV5.2.3 ��׿��
��������������Դ V1.0 ��׿��
���֮Ϣ V1.0 ��׿��
��ħ������������䣩 V1.0 ��׿��
���ں�������ϵ�����������������վ�����������������Ƽ�����
Ƶ�� ����Ƶ��������ר������������׿�������app����
�Ƽ� ��Ô���������°��������ܿ������ز���
���� ����ɫ������������ ���������ս������������
ɨ��ά�����������ֻ��汾��
ɨ��ά����������΢�Ź��ںţ�
��վ�������������������ϴ��������ַ���İ�Ȩ���뷢�ʼ�[email protected]
��ICP��2022002427��-10 �湫��������43070202000427��© 2013~2025 haote.com ������