Spaces:
Build error
Build error
import warnings | |
warnings.filterwarnings('ignore') | |
import pandas as pd | |
from sklearn.metrics import confusion_matrix | |
from sklearn.metrics import accuracy_score | |
from sklearn.metrics import precision_score | |
from sklearn.metrics import recall_score | |
from sklearn.metrics import f1_score | |
from sklearn.preprocessing import MinMaxScaler | |
from sklearn.utils import shuffle | |
from sklearn.model_selection import train_test_split | |
from sklearn.linear_model import LogisticRegression | |
from sklearn.metrics import classification_report, confusion_matrix | |
from sklearn.model_selection import StratifiedKFold | |
from sklearn import svm | |
import numpy as np | |
from sklearn.inspection import permutation_importance | |
import gradio as gr | |
df = pd.read_csv('flies.csv') | |
replacement = { | |
'a': 0, | |
'x': 1 | |
} | |
df['Type'] = df['Type'].map(replacement) | |
cols_to_use = ['Wing Length (cm)', 'Abdomen Length (cm)', 'Antenna Length (cm)', 'Max Antenna Width (cm)'] | |
# cols_to_use = ['Abdomen Length (cm)'] | |
df_use = df[[*cols_to_use, 'Type']] | |
# Shuffle the dataframe | |
df_use = shuffle(df_use) | |
x = df_use.iloc[:,0:len(df_use.columns)-1] | |
y = df_use.iloc[:, -1] | |
features = x.columns.values | |
scaler = MinMaxScaler(feature_range = (0,1)) | |
scaler.fit(x) | |
x = pd.DataFrame(scaler.transform(x)) | |
x.columns = features | |
# x_train ,x_test , y_train ,y_test = train_test_split(x, y, train_size= 0.8) | |
# print(x) | |
skf = StratifiedKFold(n_splits=4, shuffle=True) | |
kfold = skf.split(x, y) | |
confusions = [] | |
accs = [] | |
precisions = [] | |
recalls = [] | |
f1s = [] | |
importances = [] | |
for i, x in enumerate(kfold): | |
print(f"\n------------------Fold: {i+1}---------------") | |
train, test = df_use.iloc[x[0].tolist()], df_use.iloc[x[1].tolist()] | |
ytrain = train[["Type"]] | |
print(f"Training: {len(train)}") | |
print(f"Testing: {len(test)}") | |
ytest = test[["Type"]] | |
xtrain = train.drop("Type", axis=1) | |
xtest = test.drop("Type", axis=1) | |
model = svm.SVC(kernel='poly') | |
model.fit(xtrain , np.squeeze(ytrain)) | |
ypred = model.predict(xtest) | |
confusions.append(confusion_matrix(ytest, ypred)) | |
accs.append(accuracy_score(ytest, ypred)) | |
precisions.append(precision_score(ytest, ypred)) | |
recalls.append(recall_score(ytest, ypred)) | |
f1s.append(f1_score(ytest, ypred)) | |
perm_importance = permutation_importance(model, xtest, ytest) | |
features = np.array(cols_to_use) | |
sorted_idx = perm_importance.importances_mean.argsort() | |
# print(perm_importance.importances_mean[sorted_idx]) | |
importances.append(perm_importance.importances_mean[sorted_idx]) | |
avg_acc = sum(accs) / len(accs) | |
avg_precision = sum(precisions) / len(precisions) | |
avg_recall = sum(recalls) / len(recalls) | |
avg_f1 = sum(f1s) / len(f1s) | |
print() | |
print("Accuracy:", avg_acc) | |
print("Precision:", avg_precision) | |
print("Recall:", avg_recall) | |
print("F1:", avg_f1) | |
matrix = np.asmatrix(np.array(importances)) | |
# print(matrix) | |
means = matrix.mean(0).A1 # convert back to array | |
test1 = [2.81, 1.80, 1.24, 0.46] | |
test2 = [2.65, 1.84, 1.28, 0.39] | |
test3 = [3.61, 2.04, 1.40, 0.50] | |
matrix = [ | |
test1, test2, test3 | |
] | |
wingL = [] | |
abdL = [] | |
antL = [] | |
antM = [] | |
for row in matrix: | |
wingL.append(row[0]) | |
abdL.append(row[1]) | |
antL.append(row[2]) | |
antM.append(row[3]) | |
user_df = pd.DataFrame({ | |
'Wing Length (cm)': wingL, | |
'Abdomen Length (cm)': abdL, | |
'Antenna Length (cm)': antL, | |
'Max Antenna Width (cm)': antM | |
}) | |
user_df = scaler.transform(user_df) | |
preds = model.predict(user_df) | |
for pred in preds: | |
if pred == 0: | |
print("A", end = " ") | |
else: | |
print("X", end = " ") | |
def main(wingL, abdL, antL, maxAW): | |
# test1 = [2.81, 1.80, 1.24, 0.46] # should predict x | |
matrix = [ | |
[wingL, abdL, antL, maxAW] | |
] | |
wingL = [] | |
abdL = [] | |
antL = [] | |
antM = [] | |
for row in matrix: | |
wingL.append(row[0]) | |
abdL.append(row[1]) | |
antL.append(row[2]) | |
antM.append(row[3]) | |
user_df = pd.DataFrame({ | |
'Wing Length (cm)': wingL, | |
'Abdomen Length (cm)': abdL, | |
'Antenna Length (cm)': antL, | |
'Max Antenna Width (cm)': antM | |
}) | |
user_df = scaler.transform(user_df) | |
preds = model.predict(user_df) | |
if preds[0] == 0: | |
return "A" | |
else: | |
return "X" | |
gr.Interface( | |
fn=main, | |
title="FlyCatcher", | |
examples=[[2.81, 1.80, 1.24, 0.46], [2.65, 1.84, 1.28, 0.39], [3.61, 2.04, 1.40, 0.50]], | |
inputs=[gr.inputs.Number(label="Wing Length (cm)"), | |
gr.inputs.Number(label="Abdomen Length (cm)"), | |
gr.inputs.Number(label="Antenna Length (cm)"), | |
gr.inputs.Number(label="Max Antenna Width (cm)"), | |
], | |
outputs=["text"], | |
theme="huggingface").launch(debug=False, share=False) | |