added UQ_MultClassPipe4.py and UQ_imbalance.py
This commit is contained in:
parent
5779331981
commit
42c8c47e2d
2 changed files with 311 additions and 0 deletions
68
UQ_imbalance.py
Normal file
68
UQ_imbalance.py
Normal file
|
@ -0,0 +1,68 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Thu May 26 05:19:25 2022
|
||||
|
||||
@author: tanu
|
||||
"""
|
||||
#%% https://www.kite.com/blog/python/smote-python-imbalanced-learn-for-oversampling/
|
||||
import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import pandas as pd
|
||||
from sklearn.svm import SVC
|
||||
from imblearn.over_sampling import SMOTE
|
||||
#%%############################################################################
|
||||
|
||||
def train_SVM(df):
|
||||
# select the feature columns
|
||||
X = df.loc[:, df.columns != 'label']
|
||||
# select the label column
|
||||
y = df.label
|
||||
|
||||
# train an SVM with linear kernel
|
||||
clf = SVC(kernel='linear')
|
||||
clf.fit(X, y)
|
||||
|
||||
return clf
|
||||
|
||||
|
||||
def plot_svm_boundary(clf, df, title):
|
||||
fig, ax = plt.subplots()
|
||||
X0, X1 = df.iloc[:, 0], df.iloc[:, 1]
|
||||
|
||||
x_min, x_max = X0.min() - 1, X0.max() + 1
|
||||
y_min, y_max = X1.min() - 1, X1.max() + 1
|
||||
xx, yy = np.meshgrid(np.arange(x_min, x_max, 0.02), np.arange(y_min, y_max, 0.02))
|
||||
|
||||
Z = clf.predict(np.c_[xx.ravel(), yy.ravel()])
|
||||
Z = Z.reshape(xx.shape)
|
||||
out = ax.contourf(xx, yy, Z, cmap=plt.cm.coolwarm, alpha=0.8)
|
||||
|
||||
ax.scatter(X0, X1, c=df.label, cmap=plt.cm.coolwarm, s=20, edgecolors='k')
|
||||
ax.set_ylabel('y')
|
||||
ax.set_xlabel('x')
|
||||
ax.set_title(title)
|
||||
plt.show()
|
||||
#%%############################################################################
|
||||
# SMOTE number of neighbors
|
||||
#k = 1 (pnca, extra trees baseline is 0.49,numerical only)
|
||||
k = 1
|
||||
|
||||
sm = SMOTE(sampling_strategy = 'auto', k_neighbors = k, **rs)
|
||||
X_sm, y_sm = sm.fit_resample(X, y)
|
||||
print(len(X_sm)) #228
|
||||
print(Counter(y))
|
||||
y_sm_df = y_sm.to_frame()
|
||||
y_sm_df.value_counts().plot(kind = 'bar')
|
||||
|
||||
oversample = RandomOverSampler(sampling_strategy='minority')
|
||||
X_ros, y_ros = oversample.fit_resample(X, y)
|
||||
print(len(X_ros)) #228
|
||||
|
||||
undersample = RandomUnderSampler(sampling_strategy='majority')
|
||||
X_rus, y_rus = undersample.fit_resample(X, y)
|
||||
print(len(X_rus)) #142
|
||||
|
||||
sm_enn = SMOTEENN(enn=EditedNearestNeighbours(sampling_strategy='all'))
|
||||
X_enn, y_enn = sm_enn.fit_resample(X, y)
|
||||
print(len(X_enn)) #53
|
Loading…
Add table
Add a link
Reference in a new issue