Skip to content

Commit 67be0ed

Browse files
author
刘宇
committed
up
1 parent 0ede8a1 commit 67be0ed

File tree

5 files changed

+994
-1
lines changed

5 files changed

+994
-1
lines changed

ML基础/上_下采样.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
from sklearn.datasets import make_classification
2+
from collections import Counter
3+
4+
X, y = make_classification(
5+
n_samples=5000, # 样本数
6+
n_features=2, # 特征数
7+
n_informative=2,
8+
n_redundant=0,
9+
n_repeated=0,
10+
n_classes=3, # 三分类
11+
n_clusters_per_class=1,
12+
weights=[0.01, 0.05, 0.94], # 三个分类的数据比例,数据不均衡
13+
class_sep=0.8,
14+
random_state=0,
15+
)
16+
print(Counter(y))
17+
from imblearn.over_sampling import RandomOverSampler
18+
from imblearn.under_sampling import RandomUnderSampler
19+
20+
# 上采样/过采样: 增加正样本
21+
ros = RandomUnderSampler(random_state=0)
22+
X_resampled, y_resampled = ros.fit_resample(X, y)
23+
24+
sort_y = sorted(Counter(y_resampled).items())
25+
print(sort_y)

pyproject.toml

+4
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,9 @@ description = "Add your description here"
55
readme = "README.md"
66
requires-python = ">=3.11"
77
dependencies = [
8+
"imblearn>=0.0",
9+
"matplotlib>=3.10.3",
10+
"pandas>=2.2.3",
811
"scikit-learn>=1.6.1",
12+
"torch>=2.7.0",
913
]

0 commit comments

Comments
 (0)