-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
劉佳婷
authored and
劉佳婷
committed
Apr 15, 2022
1 parent
f506bbb
commit 1cdeff5
Showing
12 changed files
with
163 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
# Sequential Backward Selection | ||
import argparse | ||
from statistics import mean | ||
import numpy as np | ||
import pandas as pd | ||
|
||
from utils.read_data import read_data | ||
from utils.metrics import metrics | ||
from sklearn.preprocessing import StandardScaler | ||
from sklearn.neighbors import KNeighborsClassifier | ||
from imblearn.over_sampling import SMOTE | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--M', default=4, help='M-fold cross validation') | ||
parser.add_argument('--k', default=5, help='k for kNN') | ||
parser.add_argument('--use_SMOTE', action='store_true') | ||
args = parser.parse_args() | ||
|
||
def main(): | ||
X_tr, y_tr = read_data('datasets/algerian_fires_train.csv') | ||
X_test, y_test = read_data('datasets/algerian_fires_test.csv') | ||
# drop first column ("Date" feature) | ||
X_tr, X_test = X_tr.iloc[:,1:], X_test.iloc[:,1:] | ||
model = KNeighborsClassifier(n_neighbors=int(args.k)) | ||
scaler = StandardScaler() | ||
sm = SMOTE(random_state=42) | ||
while True: | ||
if X_tr.shape[1] == 1: break | ||
SBS_res = dict() | ||
for col in X_tr.columns: | ||
X_tr_SBS = X_tr.drop(columns=col) | ||
F1_result, Acc_result = [0]*args.M, [0]*args.M | ||
for m in range(args.M): | ||
X_val, y_val = X_tr_SBS.iloc[46*m:46*(m+1)], y_tr.iloc[46*m:46*(m+1)] | ||
if m == 0: X_tr_prime, y_tr_prime = X_tr_SBS.iloc[46:], y_tr.iloc[46:] | ||
elif m == 1: | ||
X_tr_prime = pd.concat([X_tr_SBS.iloc[:46], X_tr_SBS.iloc[92:]]) | ||
y_tr_prime = pd.concat([y_tr.iloc[:46], y_tr.iloc[92:]]) | ||
elif m == 2: | ||
X_tr_prime = pd.concat([X_tr_SBS.iloc[:92], X_tr_SBS.iloc[138:]]) | ||
y_tr_prime = pd.concat([y_tr.iloc[:92], y_tr.iloc[138:]]) | ||
else: X_tr_prime, y_tr_prime = X_tr_SBS.iloc[:138], y_tr.iloc[:138] | ||
if args.use_SMOTE: | ||
X_tr_prime, y_tr_prime = sm.fit_resample(X_tr_prime, y_tr_prime) | ||
X_tr_prime = scaler.fit_transform(X_tr_prime) | ||
X_val = scaler.transform(X_val) | ||
model.fit(X_tr_prime, y_tr_prime) | ||
y_val_pred = model.predict(X_val) | ||
F1_result[m], Acc_result[m] = metrics(y_val, y_val_pred, "kNN", work='val') | ||
SBS_res[col] = mean(F1_result)+mean(Acc_result) | ||
SBS_res = sorted(SBS_res.items(), key = lambda kv:(kv[1], kv[0])) | ||
drop_col = SBS_res[0][0] | ||
print("dropped column:", drop_col) | ||
X_tr = X_tr.drop(columns=drop_col) | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import argparse | ||
from statistics import mean | ||
import numpy as np | ||
import pandas as pd | ||
|
||
from utils.read_data import read_data | ||
from utils.metrics import metrics | ||
from sklearn.preprocessing import StandardScaler | ||
from sklearn.neighbors import KNeighborsClassifier | ||
from imblearn.over_sampling import SMOTE | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument('--M', default=4, help='M-fold cross validation') | ||
parser.add_argument('--k', default=5, help='k for kNN') | ||
parser.add_argument('--use_SMOTE', action='store_true') | ||
parser.add_argument ('--feat_reduction', action='store_true', help='drop four least contributing features') | ||
parser.add_argument('--plot_title', default='', help='title for cf_matrix plot') | ||
args = parser.parse_args() | ||
|
||
def main(): | ||
X_tr, y_tr = read_data('datasets/algerian_fires_train.csv') | ||
X_test, y_test = read_data('datasets/algerian_fires_test.csv') | ||
# drop first column ("Date" feature) | ||
X_tr, X_test = X_tr.iloc[:,1:], X_test.iloc[:,1:] | ||
model = KNeighborsClassifier(n_neighbors=int(args.k)) | ||
scaler = StandardScaler() | ||
sm = SMOTE(random_state=42) | ||
if args.feat_reduction: | ||
X_tr = X_tr.drop(columns=['ISI']) | ||
X_test = X_test.drop(columns=['ISI']) | ||
F1_result, Acc_result = [0]*args.M, [0]*args.M | ||
for m in range(args.M): | ||
X_val, y_val = X_tr.iloc[46*m:46*(m+1)], y_tr.iloc[46*m:46*(m+1)] | ||
if m == 0: X_tr_prime, y_tr_prime = X_tr.iloc[46:], y_tr.iloc[46:] | ||
elif m == 1: | ||
X_tr_prime = pd.concat([X_tr.iloc[:46], X_tr.iloc[92:]]) | ||
y_tr_prime = pd.concat([y_tr.iloc[:46], y_tr.iloc[92:]]) | ||
elif m == 2: | ||
X_tr_prime = pd.concat([X_tr.iloc[:92], X_tr.iloc[138:]]) | ||
y_tr_prime = pd.concat([y_tr.iloc[:92], y_tr.iloc[138:]]) | ||
else: X_tr_prime, y_tr_prime = X_tr.iloc[:138], y_tr.iloc[:138] | ||
if args.use_SMOTE: | ||
X_tr_prime, y_tr_prime = sm.fit_resample(X_tr_prime, y_tr_prime) | ||
X_tr_prime = scaler.fit_transform(X_tr_prime) | ||
X_val = scaler.transform(X_val) | ||
model.fit(X_tr_prime, y_tr_prime) | ||
y_val_pred = model.predict(X_val) | ||
F1_result[m], Acc_result[m] = metrics(y_val, y_val_pred, "kNN", work='val') | ||
|
||
print("Val F1_score=", mean(F1_result), "Val Accuracy=", mean(Acc_result)) | ||
print("Training with full dataset!") | ||
if args.use_SMOTE: | ||
X_tr, y_tr = sm.fit_resample(X_tr, y_tr) | ||
X_tr = scaler.fit_transform(X_tr) | ||
X_test = scaler.transform(X_test) | ||
y_test_pred = model.predict(X_test) | ||
F1_score, Accuracy = metrics(y_test, y_test_pred, args.plot_title+'_k='+str(args.k)) | ||
print("Test F1_score=", F1_score, "Test Accuracy=", Accuracy) | ||
|
||
if __name__ == '__main__': | ||
main() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters