小编npm*_*npm的帖子

进行“离开一个组出去”交叉验证时如何应用过采样?

我正在处理用于分类的不平衡数据,并且之前尝试使用综合少数族裔过采样技术(SMOTE)对培训数据进行过采样。但是,这一次我认为我还需要使用“离开一个组出去”(LOGO)交叉验证,因为我想在每个简历上都留出一个主题。

我不确定我能否很好地解释它,但是据我所知,要使用SMOTE进行k折CV,我们可以在每一折上循环进行SMOTE,正如我在另一篇文章中的代码中所看到的那样。以下是在K折CV上实施SMOTE的示例。

from sklearn.model_selection import KFold
from imblearn.over_sampling import SMOTE
from sklearn.metrics import f1_score

kf = KFold(n_splits=5)

for fold, (train_index, test_index) in enumerate(kf.split(X), 1):
    X_train = X[train_index]
    y_train = y[train_index]  
    X_test = X[test_index]
    y_test = y[test_index]  
    sm = SMOTE()
    X_train_oversampled, y_train_oversampled = sm.fit_sample(X_train, y_train)
    model = ...  # classification model example
    model.fit(X_train, y_train)  
    y_pred = model.predict(X_test)
    print(f'For fold {fold}:')
    print(f'Accuracy: {model.score(X_test, y_test)}')
    print(f'f-score: {f1_score(y_test, y_pred)}')
Run Code Online (Sandbox Code Playgroud)

没有SMOTE,我试图这样做来做LOGO CV。但是通过这样做,我将使用超不平衡数据集。

X = X
y = np.array(df.loc[:, df.columns == 'label'])
groups = …
Run Code Online (Sandbox Code Playgroud)

python machine-learning pandas scikit-learn cross-validation

6
推荐指数
1
解决办法
235
查看次数

如何为数据帧中最后几行的熊猫列定义条件?

假设我有一个数据框,其中这些行是最后8行。

time                a       b       b           d           e           f 
2018-03-04 10:00:00 86.0    194.0   1.084830    1.088466    196.000000  84.333333
2018-03-04 10:30:00 37.0    59.0    1.082257    1.091397    203.000000  87.833333
2018-03-04 11:00:00 65.0    117.0   1.068825    1.091043    220.166667  96.666667
2018-03-04 11:30:00 10.0    9.0     1.070807    1.087203    183.666667  82.333333
2018-03-04 12:00:00 94.0    157.0   1.083382    1.077549    112.833333  61.666667
2018-03-04 12:30:00 66.0    68.0    1.075636    1.077623    100.666667  59.666667
2018-03-04 13:00:00 224.0   607.0   1.152262    1.088861    169.500000  82.666667
2018-03-04 13:30:00 112.0   279.0   1.119430    1.095057    206.166667  95.166667
Run Code Online (Sandbox Code Playgroud)

如何使用此条件在Pandas上创建新列“ g”:如果该行是最后一行,则值为100%;如果该行是最后第二行,则值为95%..直到达到70%,否则将为0?

python pandas

3
推荐指数
1
解决办法
45
查看次数

为seaborn lmplot添加文本注释

我正在尝试为聚类结果创建seaborn lmplot,数据示例如下所示:

    ID   CA     IP  clusters
    38  10.3    5.6   1
    59  10.4    6.1   0
    64  10.0    6.6   1
    35  10.6    5.6   1
    54  10.6    5.6   1
    60  10.2    8.2   1
Run Code Online (Sandbox Code Playgroud)

有两个集群(集群 0 和集群 1),我想在每个 scatter 上显示基于“ID”列的“ID”尝试了在seaborn regplot中添加文本的功能,但出现错误,提示“FacetGrid没有文本功能”。

seaborn 情节的代码:

ax = sns.lmplot('CA', 'IP', 
     data=df_tr, 
     fit_reg=False, 
     hue="clusters",  palette="Set1",
     scatter_kws={"marker": "D", "s": 50})

plt.title('Calcium vs Phosporus')
plt.xlabel('CA')
plt.ylabel('IP')
Run Code Online (Sandbox Code Playgroud)

和情节:

在此输入图像描述

python plot seaborn

2
推荐指数
1
解决办法
6041
查看次数