本文目录导读:

模糊粗糙梯度提升的方法。
Python 实现
基础实现
import numpy as np
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.feature_extraction.text import TfidfVectorizer
import re
import nltk
from nltk.corpus import stopwords
class FuzzyRoughGBM:
def __init__(self, n_estimators=100, learning_rate=0.1, max_depth=3):
self.n_estimators = n_estimators
self.learning_rate = learning_rate
self.max_depth = max_depth
self.model = GradientBoostingClassifier(
n_estimators=n_estimators,
learning_rate=learning_rate,
max_depth=max_depth
)
self.vectorizer = TfidfVectorizer(max_features=5000)
def fuzzy_preprocessing(self, text):
"""模糊预处理:处理文本模糊特征"""
# 小写化
text = text.lower()
# 去除特殊字符
text = re.sub(r'[^\w\s]', ' ', text)
# 模糊词替换(如拼写错误容忍)
text = re.sub(r'(\w)\1{2,}', r'\1', text) # 连续重复字符简化
return text
def rough_feature_extraction(self, texts):
"""粗糙特征提取"""
# 基础统计特征
features = []
for text in texts:
# 粗略特征
words = text.split()
length = len(words)
# 模糊类别特征
if length < 10:
cat = 'short'
elif length < 50:
cat = 'medium'
else:
cat = 'long'
features.append({
'word_count': length,
'char_count': len(text),
'category': cat,
'avg_word_len': np.mean([len(w) for w in words]) if words else 0
})
return features
def gradient_boosting_with_fuzzy(self, X_train, y_train, X_test):
"""模糊梯度提升训练"""
# 模糊文本处理
X_train_fuzzy = [self.fuzzy_preprocessing(x) for x in X_train]
X_test_fuzzy = [self.fuzzy_preprocessing(x) for x in X_test]
# 粗糙特征提取
train_rough = self.rough_feature_extraction(X_train_fuzzy)
test_rough = self.rough_feature_extraction(X_test_fuzzy)
# TF-IDF向量化
X_train_tfidf = self.vectorizer.fit_transform(X_train_fuzzy)
X_test_tfidf = self.vectorizer.transform(X_test_fuzzy)
# 合并特征
X_train_combined = self._combine_features(X_train_tfidf, train_rough)
X_test_combined = self._combine_features(X_test_tfidf, test_rough)
# 训练模型
self.model.fit(X_train_combined, y_train)
predictions = self.model.predict(X_test_combined)
return predictions
def _combine_features(self, tfidf_features, rough_features):
"""合并TF-IDF特征和粗糙特征"""
rough_array = np.array([list(f.values())[:3] for f in rough_features])
from scipy.sparse import hstack, csr_matrix
return hstack([tfidf_features, csr_matrix(rough_array)])
增强版实现
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score, classification_report
import warnings
warnings.filterwarnings('ignore')
class EnhancedFuzzyRoughGBM:
def __init__(self, fuzzy_level=0.5, rough_granularity='medium'):
self.fuzzy_level = fuzzy_level
self.rough_granularity = rough_granularity
self.models = []
self.feature_importance = {}
def fuzzy_tokenization(self, text, threshold=0.3):
"""模糊分词:对相似词进行聚合"""
# 简单模糊匹配实现
from difflib import SequenceMatcher
words = text.split()
fuzzy_groups = []
used = set()
for i, w1 in enumerate(words):
if i in used:
continue
group = [w1]
used.add(i)
for j, w2 in enumerate(words):
if j in used:
continue
similarity = SequenceMatcher(None, w1, w2).ratio()
if similarity > threshold:
group.append(w2)
used.add(j)
fuzzy_groups.append(group)
return ['_'.join(g) for g in fuzzy_groups]
def rough_set_approximation(self, features, labels):
"""粗糙集近似:计算下近似和上近似"""
# 简单的粗糙集实现
lower_approx = {}
upper_approx = {}
for class_label in set(labels):
class_indices = [i for i, l in enumerate(labels) if l == class_label]
# 下近似:确定属于该类的样本
lower_approx[class_label] = class_indices
# 上近似:可能属于该类的样本
similar_indices = []
for idx in class_indices:
# 使用欧氏距离找相似样本
distances = []
for other_idx in range(len(features)):
if other_idx != idx:
dist = np.linalg.norm(np.array(features[idx]) - np.array(features[other_idx]))
distances.append((other_idx, dist))
distances.sort(key=lambda x: x[1])
similar_indices.extend([d[0] for d in distances[:3]])
upper_approx[class_label] = list(set(similar_indices + class_indices))
return lower_approx, upper_approx
def gradient_boosting_with_roughness(self, X, y, n_stages=5):
"""带有粗糙集的梯度提升"""
# 数据预处理
X_processed = []
for text in X:
fuzzy_words = self.fuzzy_tokenization(text)
# 转换为数值特征
word_lengths = [len(w) for w in fuzzy_words]
X_processed.append([
len(word_lengths), # 词数
np.mean(word_lengths) if word_lengths else 0, # 平均词长
np.std(word_lengths) if word_lengths else 0, # 词长标准差
len(text) # 总字符数
])
X_processed = np.array(X_processed)
# 多阶段梯度提升
predictions = np.zeros(len(y))
residuals = y.copy()
for stage in range(n_stages):
# 计算近似集合
lower, upper = self.rough_set_approximation(X_processed, residuals)
# 构建弱学习器
from sklearn.tree import DecisionTreeRegressor
weak_learner = DecisionTreeRegressor(max_depth=2 + stage)
# 使用粗糙集指导训练
for class_label in set(residuals):
if class_label in lower:
# 下近似样本权重更高
weights = np.array([2.0 if i in lower[class_label]
else 1.0 for i in range(len(y))])
weak_learner.fit(X_processed, residuals, sample_weight=weights)
# 更新预测
stage_pred = weak_learner.predict(X_processed)
predictions += self.learning_rate * stage_pred
# 更新残差
residuals = y - predictions
self.models.append(weak_learner)
return np.round(predictions) # 二分类返回0或1
文件处理脚本
import os
import glob
from pathlib import Path
class FileFuzzyRoughProcessor:
def __init__(self, data_dir='./data'):
self.data_dir = Path(data_dir)
self.data_dir.mkdir(exist_ok=True)
def load_files_as_text(self, pattern='*.txt'):
"""加载文件为文本数据"""
files = glob.glob(str(self.data_dir / pattern))
texts = []
labels = []
for file_path in files:
with open(file_path, 'r', encoding='utf-8', errors='ignore') as f:
texts.append(f.read())
# 从文件名或目录获取标签
label = 1 if 'positive' in file_path else 0
labels.append(label)
return texts, labels
def process_file_batch(self, file_patterns=['*.txt', '*.md', '*.csv']):
"""批量处理文件"""
all_texts = []
all_labels = []
for pattern in file_patterns:
texts, labels = self.load_files_as_text(pattern)
all_texts.extend(texts)
all_labels.extend(labels)
return all_texts, all_labels
def apply_fuzzy_rough_gbm(self, test_size=0.2):
"""应用模糊粗糙梯度提升"""
# 加载数据
X_raw, y = self.process_file_batch()
# 创建模型
gbm = FuzzyRoughGBM(n_estimators=50, learning_rate=0.1, max_depth=3)
# 分割数据
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X_raw, y, test_size=test_size, random_state=42
)
# 训练和预测
predictions = gbm.gradient_boosting_with_fuzzy(X_train, y_train, X_test)
# 评估
accuracy = accuracy_score(y_test, predictions)
print(f"Accuracy: {accuracy:.4f}")
print("\nClassification Report:")
print(classification_report(y_test, predictions))
return predictions, y_test
使用示例
# 使用示例
if __name__ == "__main__":
# 创建处理器
processor = FileFuzzyRoughProcessor("./documents")
# 处理文件
predictions, true_labels = processor.apply_fuzzy_rough_gbm(test_size=0.2)
# 或者使用增强版本
enhanced_gbm = EnhancedFuzzyRoughGBM(fuzzy_level=0.6, rough_granularity='fine')
texts, labels = processor.process_file_batch()
results = enhanced_gbm.gradient_boosting_with_roughness(texts, labels)
这个实现包含了:
- 模糊处理:文本模糊化、拼写容忍、模糊分词
- 粗糙集近似:计算上下近似、处理不确定性
- 梯度提升:多阶段训练、残差学习
- 文件处理:批量文件加载、自动标签提取
根据需要可以调整参数和实现细节。