Edmilson Alexandre
commited on
Commit
·
266d9b4
1
Parent(s):
f4d5c9c
done
Browse files- API_flask_server.py +29 -0
- NASA.ipynb +1 -0
- README.md +2 -10
- model.py +279 -0
- requirements.txt +5 -0
API_flask_server.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
## An python API for our Model
|
| 2 |
+
## Design by edander32 (Edmilson Alexandre) and jjambo(Joaquim Jambo)
|
| 3 |
+
|
| 4 |
+
from flask import Flask, request, jsonify
|
| 5 |
+
import pandas as pd
|
| 6 |
+
import joblib
|
| 7 |
+
|
| 8 |
+
app = Flask(__name__)
|
| 9 |
+
|
| 10 |
+
model = joblib.load("exoplanet_model.pkl")
|
| 11 |
+
|
| 12 |
+
@app.route("/")
|
| 13 |
+
def home():
|
| 14 |
+
return {"message": "API do classificador de exoplanetas online."}
|
| 15 |
+
|
| 16 |
+
@app.route("/predict", methods=["POST"])
|
| 17 |
+
def predict():
|
| 18 |
+
try:
|
| 19 |
+
data = request.get_json()
|
| 20 |
+
df = pd.DataFrame([data])
|
| 21 |
+
pred = model.predict(df)[0]
|
| 22 |
+
prob = model.predict_proba(df)[0][1]
|
| 23 |
+
return jsonify({"prediction": int(pred), "probability": float(prob)})
|
| 24 |
+
except Exception as e:
|
| 25 |
+
return jsonify({"error": str(e)}), 400
|
| 26 |
+
|
| 27 |
+
if __name__ == "__main__":
|
| 28 |
+
app.run(host="0.0.0.0", port=7860)
|
| 29 |
+
|
NASA.ipynb
ADDED
|
@@ -0,0 +1 @@
|
|
|
|
|
|
|
| 1 |
+
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"provenance":[],"authorship_tag":"ABX9TyO5J/RoFrpl8dZBuNzBEwXn"},"kernelspec":{"name":"python3","display_name":"Python 3"},"language_info":{"name":"python"}},"cells":[{"cell_type":"code","source":["!pip install -q lightgbm tsfresh optuna scikit-learn pandas numpy matplotlib seaborn joblib"],"metadata":{"id":"wHEiah4us0eh","executionInfo":{"status":"ok","timestamp":1759609790439,"user_tz":-60,"elapsed":9712,"user":{"displayName":"Edmilson Alexandre","userId":"05182212798533402489"}},"colab":{"base_uri":"https://localhost:8080/"},"outputId":"b24f02b2-de03-4178-aa06-6dfdae9e2094"},"execution_count":2,"outputs":[{"output_type":"stream","name":"stdout","text":["\u001b[?25l \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m0.0/400.9 kB\u001b[0m \u001b[31m?\u001b[0m eta \u001b[36m-:--:--\u001b[0m\r\u001b[2K \u001b[91m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m\u001b[91m╸\u001b[0m\u001b[90m━━━━━\u001b[0m \u001b[32m348.2/400.9 kB\u001b[0m \u001b[31m10.4 MB/s\u001b[0m eta \u001b[36m0:00:01\u001b[0m\r\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m400.9/400.9 kB\u001b[0m \u001b[31m8.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25h"]}]},{"cell_type":"code","source":["import os\n","import numpy\n","import pandas\n","import joblib\n","import optuna\n","import tsfresh\n","import sklearn\n","import seaborn\n","import lightgbm\n","import matplotlib\n","from google.colab import drive\n","\n","\n","def EDA(file):\n"," pandas.set_option('display.max_columns', 200)\n"," target_col = 'target'\n"," print(\"Shape from file: \", file.shape)\n"," # display(file.head())\n"," # print(file.dtypes)\n","\n","def PipelineCreation(file, target_col):\n"," from sklearn.model_selection import train_test_split\n"," from sklearn.pipeline import Pipeline\n"," from sklearn.impute import SimpleImputer\n"," from sklearn.preprocessing import OneHotEncoder\n"," from sklearn.compose import ColumnTransformer\n","\n"," id_cols = [c for c in ['id', 'time', 'index'] if c in file.columns]\n"," x = file.drop(columns=[target_col] + id_cols, errors='ignore')\n"," y = file[target_col]\n","\n"," numeric_features = x.select_dtypes(include=['number']).columns.tolist()\n"," categorical_features = x.select_dtypes(include=['object', 'category', 'bool']).columns.tolist()\n"," # print(\"Numeric: \", len(numeric_features))\n"," # print(\"Categorical: \", len(categorical_features))\n"," # print(\"X: \", x)\n"," # print(\"Y: \", y)\n","\n"," numeric_transformer = Pipeline([('imputer', SimpleImputer(strategy='median')),])\n","\n"," categorical_transformer = Pipeline([('imputer', SimpleImputer(strategy='most_frequent')),\n"," ('ohe', OneHotEncoder(handle_unknown='ignore')),])\n"," print(f\"Num:{len(numeric_features)}\")\n"," preprocessor = ColumnTransformer([\n"," ('num', numeric_transformer, numeric_features),\n"," ('cat', categorical_transformer, categorical_features),\n"," ], remainder='drop')\n"," return x, y, preprocessor\n","\n","def Training(x_train_tr, y_train):\n"," from lightgbm import LGBMClassifier\n"," scale_pos_weight = (y_train==0).sum() / max(1, (y_train==1).sum())\n"," model = LGBMClassifier(\n"," objective='binary',\n"," n_estimators=10000,\n"," learning_rate=0.05,\n"," num_leaves=31,\n"," random_state=42,\n"," scale_pos_weight=scale_pos_weight\n"," )\n"," model.fit(\n"," x_train_tr, y_train,\n"," eval_set=[(x_val_tr, y_val)],\n"," eval_metric='auc',\n"," #my version does not support that method. I need to use callbacks\n"," #early_stopping_rounds=100,\n"," #callbacks=[early_stopping(100), log_evaluation(100)]\n"," )\n"," print(\"Best iteration:\", model.best_iteration_)\n"," print(\"Train AUC:\", model.best_score_['training']['auc'])\n"," print(\"Valid AUC:\", model.best_score_['valid_0']['auc'])\n","\n"," return model\n","\n","\n","\n","from sklearn.model_selection import train_test_split\n","if os.path.exists('/content/drive') == 0:\n"," drive.mount('/content/drive')\n","\n","labels = pandas.read_csv('/content/drive/MyDrive/AI_assets/labels.csv')\n","light_curves = pandas.read_csv('/content/drive/MyDrive/AI_assets/light_curves.csv')\n","metadata = pandas.read_csv('/content/drive/MyDrive/AI_assets/metadata.csv')\n","data = pandas.read_csv('/content/drive/MyDrive/data2.csv')\n","\n","# im gonna change this under cause i nedd more classes. Using binary interpretation insted of 'CONFIRMED' will help a lot\n","#x, y, preprocessor = PipelineCreation(data, target_col='kepoi_name')\n","\n","data['target'] = data['koi_disposition'].map(\n"," lambda v: 1 if v == \"CONFIRMED\" else 0\n",")\n","\n","x, y, preprocessor = PipelineCreation(data, target_col='target')\n","print(\"First step done. 1 -> PIPELINE CREATION\")\n","# print(\"X: \", x)\n","# print(\"Y: \", y)\n","x_train, x_val, y_train, y_val = train_test_split(\n"," x, y, test_size=0.20, stratify=y, random_state=42\n"," )\n","preprocessor.fit(x_train)\n","x_train_tr = preprocessor.transform(x_train)\n","x_val_tr = preprocessor.transform(x_val)\n","EDA(labels)\n","\n","#debug purposes\n","print(\"Second step done. 2 -> EDA DONE\")\n","print(\"X_train shape:\", x_train_tr.shape)\n","print(\"X_val shape:\", x_val_tr.shape)\n","print(\"y_train distribution:\\n\", y_train.value_counts())\n","print(\"y_val distribution:\\n\", y_val.value_counts())\n","print(\"Check for NaNs:\", x_train_tr.isna().sum().sum(), \"in train,\", x_val_tr.isna().sum().sum(), \"in val\")\n","\n","\n","#model = Training(x_train_tr, y_train)\n","#print(\"Finished Training. 3!!\")\n","\n","\n","# A litle degub made by GPT\n","# print(\"Best iteration:\", model.best_iteration_)\n","# print(\"Train AUC:\", model.best_score_['training']['auc'])\n","# print(\"Valid AUC:\", model.best_score_['valid_0']['auc'])\n"],"metadata":{"colab":{"base_uri":"https://localhost:8080/","height":873},"id":"-kgzOBe6QXbJ","executionInfo":{"status":"error","timestamp":1759611069253,"user_tz":-60,"elapsed":20,"user":{"displayName":"Edmilson Alexandre","userId":"05182212798533402489"}},"outputId":"98dc3b31-acee-4a6d-c30e-9f15901a1b25"},"execution_count":8,"outputs":[{"output_type":"stream","name":"stdout","text":["Num:133\n","First step done. 1 -> PIPELINE CREATION\n","Shape from file: (4, 2)\n","Second step done. 2 -> EDA DONE\n","X_train shape: (8, 172)\n","X_val shape: (2, 172)\n","y_train distribution:\n"," target\n","1 6\n","0 2\n","Name: count, dtype: int64\n","y_val distribution:\n"," target\n","0 1\n","1 1\n","Name: count, dtype: int64\n"]},{"output_type":"stream","name":"stderr","text":["/usr/local/lib/python3.12/dist-packages/sklearn/impute/_base.py:635: UserWarning: Skipping features without any observed values: ['koi_gmag_err' 'koi_rmag_err' 'koi_imag_err' 'koi_zmag_err'\n"," 'koi_kepmag_err' 'koi_model_dof' 'koi_model_chisq' 'koi_eccen_err1'\n"," 'koi_eccen_err2' 'koi_longp' 'koi_longp_err1' 'koi_longp_err2'\n"," 'koi_sma_err1' 'koi_sma_err2' 'koi_ingress' 'koi_ingress_err1'\n"," 'koi_ingress_err2' 'koi_incl_err1' 'koi_incl_err2' 'koi_teq_err1'\n"," 'koi_teq_err2' 'koi_sage' 'koi_sage_err1' 'koi_sage_err2']. At least one non-missing value is needed for imputation with strategy='median'.\n"," warnings.warn(\n","/usr/local/lib/python3.12/dist-packages/sklearn/impute/_base.py:635: UserWarning: Skipping features without any observed values: ['koi_gmag_err' 'koi_rmag_err' 'koi_imag_err' 'koi_zmag_err'\n"," 'koi_kepmag_err' 'koi_model_dof' 'koi_model_chisq' 'koi_eccen_err1'\n"," 'koi_eccen_err2' 'koi_longp' 'koi_longp_err1' 'koi_longp_err2'\n"," 'koi_sma_err1' 'koi_sma_err2' 'koi_ingress' 'koi_ingress_err1'\n"," 'koi_ingress_err2' 'koi_incl_err1' 'koi_incl_err2' 'koi_teq_err1'\n"," 'koi_teq_err2' 'koi_sage' 'koi_sage_err1' 'koi_sage_err2']. At least one non-missing value is needed for imputation with strategy='median'.\n"," warnings.warn(\n","/usr/local/lib/python3.12/dist-packages/sklearn/impute/_base.py:635: UserWarning: Skipping features without any observed values: ['koi_gmag_err' 'koi_rmag_err' 'koi_imag_err' 'koi_zmag_err'\n"," 'koi_kepmag_err' 'koi_model_dof' 'koi_model_chisq' 'koi_eccen_err1'\n"," 'koi_eccen_err2' 'koi_longp' 'koi_longp_err1' 'koi_longp_err2'\n"," 'koi_sma_err1' 'koi_sma_err2' 'koi_ingress' 'koi_ingress_err1'\n"," 'koi_ingress_err2' 'koi_incl_err1' 'koi_incl_err2' 'koi_teq_err1'\n"," 'koi_teq_err2' 'koi_sage' 'koi_sage_err1' 'koi_sage_err2']. At least one non-missing value is needed for imputation with strategy='median'.\n"," warnings.warn(\n"]},{"output_type":"error","ename":"AttributeError","evalue":"'numpy.ndarray' object has no attribute 'isna'","traceback":["\u001b[0;31m---------------------------------------------------------------------------\u001b[0m","\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)","\u001b[0;32m/tmp/ipython-input-2064020500.py\u001b[0m in \u001b[0;36m<cell line: 0>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 109\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"y_train distribution:\\n\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_train\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue_counts\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 110\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"y_val distribution:\\n\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0my_val\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mvalue_counts\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 111\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Check for NaNs:\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx_train_tr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misna\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"in train,\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mx_val_tr\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0misna\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msum\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m\"in val\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 112\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 113\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n","\u001b[0;31mAttributeError\u001b[0m: 'numpy.ndarray' object has no attribute 'isna'"]}]},{"cell_type":"code","source":[],"metadata":{"colab":{"base_uri":"https://localhost:8080/"},"id":"CAErcbk0yL-d","executionInfo":{"status":"ok","timestamp":1759540476879,"user_tz":-60,"elapsed":10596,"user":{"displayName":"Edmilson Alexandre","userId":"05182212798533402489"}},"outputId":"571ef5ef-7d8f-462c-8449-2d4444e39ed6"},"execution_count":null,"outputs":[{"output_type":"stream","name":"stdout","text":["Collecting tabgan==1.3.3\n"," Downloading tabgan-1.3.3-py2.py3-none-any.whl.metadata (10.0 kB)\n","Requirement already satisfied: pandas in /usr/local/lib/python3.12/dist-packages (from tabgan==1.3.3) (2.2.2)\n","Requirement already satisfied: numpy in /usr/local/lib/python3.12/dist-packages (from tabgan==1.3.3) (2.0.2)\n","Collecting category-encoders (from tabgan==1.3.3)\n"," Downloading category_encoders-2.8.1-py3-none-any.whl.metadata (7.9 kB)\n","Requirement already satisfied: torch>=1.0 in /usr/local/lib/python3.12/dist-packages (from tabgan==1.3.3) (2.8.0+cu126)\n","Requirement already satisfied: lightgbm>=2.2.3 in /usr/local/lib/python3.12/dist-packages (from tabgan==1.3.3) (4.6.0)\n","Requirement already satisfied: scikit-learn>=1.0.2 in /usr/local/lib/python3.12/dist-packages (from tabgan==1.3.3) (1.6.1)\n","Requirement already satisfied: torchvision in /usr/local/lib/python3.12/dist-packages (from tabgan==1.3.3) (0.23.0+cu126)\n","Requirement already satisfied: python-dateutil in /usr/local/lib/python3.12/dist-packages (from tabgan==1.3.3) (2.9.0.post0)\n","Requirement already satisfied: tqdm in /usr/local/lib/python3.12/dist-packages (from tabgan==1.3.3) (4.67.1)\n","Requirement already satisfied: scipy in /usr/local/lib/python3.12/dist-packages (from lightgbm>=2.2.3->tabgan==1.3.3) (1.16.2)\n","Requirement already satisfied: joblib>=1.2.0 in /usr/local/lib/python3.12/dist-packages (from scikit-learn>=1.0.2->tabgan==1.3.3) (1.5.2)\n","Requirement already satisfied: threadpoolctl>=3.1.0 in /usr/local/lib/python3.12/dist-packages (from scikit-learn>=1.0.2->tabgan==1.3.3) (3.6.0)\n","Requirement already satisfied: filelock in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (3.19.1)\n","Requirement already satisfied: typing-extensions>=4.10.0 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (4.15.0)\n","Requirement already satisfied: setuptools in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (75.2.0)\n","Requirement already satisfied: sympy>=1.13.3 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (1.13.3)\n","Requirement already satisfied: networkx in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (3.5)\n","Requirement already satisfied: jinja2 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (3.1.6)\n","Requirement already satisfied: fsspec in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (2025.3.0)\n","Requirement already satisfied: nvidia-cuda-nvrtc-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (12.6.77)\n","Requirement already satisfied: nvidia-cuda-runtime-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (12.6.77)\n","Requirement already satisfied: nvidia-cuda-cupti-cu12==12.6.80 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (12.6.80)\n","Requirement already satisfied: nvidia-cudnn-cu12==9.10.2.21 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (9.10.2.21)\n","Requirement already satisfied: nvidia-cublas-cu12==12.6.4.1 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (12.6.4.1)\n","Requirement already satisfied: nvidia-cufft-cu12==11.3.0.4 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (11.3.0.4)\n","Requirement already satisfied: nvidia-curand-cu12==10.3.7.77 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (10.3.7.77)\n","Requirement already satisfied: nvidia-cusolver-cu12==11.7.1.2 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (11.7.1.2)\n","Requirement already satisfied: nvidia-cusparse-cu12==12.5.4.2 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (12.5.4.2)\n","Requirement already satisfied: nvidia-cusparselt-cu12==0.7.1 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (0.7.1)\n","Requirement already satisfied: nvidia-nccl-cu12==2.27.3 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (2.27.3)\n","Requirement already satisfied: nvidia-nvtx-cu12==12.6.77 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (12.6.77)\n","Requirement already satisfied: nvidia-nvjitlink-cu12==12.6.85 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (12.6.85)\n","Requirement already satisfied: nvidia-cufile-cu12==1.11.1.6 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (1.11.1.6)\n","Requirement already satisfied: triton==3.4.0 in /usr/local/lib/python3.12/dist-packages (from torch>=1.0->tabgan==1.3.3) (3.4.0)\n","Requirement already satisfied: patsy>=0.5.1 in /usr/local/lib/python3.12/dist-packages (from category-encoders->tabgan==1.3.3) (1.0.1)\n","Requirement already satisfied: statsmodels>=0.9.0 in /usr/local/lib/python3.12/dist-packages (from category-encoders->tabgan==1.3.3) (0.14.5)\n","Requirement already satisfied: pytz>=2020.1 in /usr/local/lib/python3.12/dist-packages (from pandas->tabgan==1.3.3) (2025.2)\n","Requirement already satisfied: tzdata>=2022.7 in /usr/local/lib/python3.12/dist-packages (from pandas->tabgan==1.3.3) (2025.2)\n","Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.12/dist-packages (from python-dateutil->tabgan==1.3.3) (1.17.0)\n","Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in /usr/local/lib/python3.12/dist-packages (from torchvision->tabgan==1.3.3) (11.3.0)\n","Requirement already satisfied: packaging>=21.3 in /usr/local/lib/python3.12/dist-packages (from statsmodels>=0.9.0->category-encoders->tabgan==1.3.3) (25.0)\n","Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.12/dist-packages (from sympy>=1.13.3->torch>=1.0->tabgan==1.3.3) (1.3.0)\n","Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.12/dist-packages (from jinja2->torch>=1.0->tabgan==1.3.3) (3.0.3)\n","Downloading tabgan-1.3.3-py2.py3-none-any.whl (28 kB)\n","Downloading category_encoders-2.8.1-py3-none-any.whl (85 kB)\n","\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m85.7/85.7 kB\u001b[0m \u001b[31m2.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n","\u001b[?25hInstalling collected packages: category-encoders, tabgan\n","Successfully installed category-encoders-2.8.1 tabgan-1.3.3\n"]}]}]}
|
README.md
CHANGED
|
@@ -1,10 +1,2 @@
|
|
| 1 |
-
|
| 2 |
-
|
| 3 |
-
emoji: 😻
|
| 4 |
-
colorFrom: gray
|
| 5 |
-
colorTo: indigo
|
| 6 |
-
sdk: docker
|
| 7 |
-
pinned: false
|
| 8 |
-
---
|
| 9 |
-
|
| 10 |
-
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
| 1 |
+
# AI-Exo-Hunter
|
| 2 |
+
Hunting exoplanets using AI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
model.py
ADDED
|
@@ -0,0 +1,279 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import numpy
|
| 3 |
+
import pandas
|
| 4 |
+
import joblib
|
| 5 |
+
import optuna
|
| 6 |
+
import tsfresh
|
| 7 |
+
import sklearn
|
| 8 |
+
import seaborn
|
| 9 |
+
import lightgbm
|
| 10 |
+
import matplotlib
|
| 11 |
+
from google.colab import drive
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
def EDA(file):
|
| 15 |
+
pandas.set_option('display.max_columns', 200)
|
| 16 |
+
target_col = 'target'
|
| 17 |
+
print("Shape from file: ", file.shape)
|
| 18 |
+
# display(file.head())
|
| 19 |
+
# print(file.dtypes)
|
| 20 |
+
|
| 21 |
+
def PipelineCreation(file, target_col):
|
| 22 |
+
from sklearn.model_selection import train_test_split
|
| 23 |
+
from sklearn.pipeline import Pipeline
|
| 24 |
+
from sklearn.impute import SimpleImputer
|
| 25 |
+
from sklearn.preprocessing import OneHotEncoder
|
| 26 |
+
from sklearn.compose import ColumnTransformer
|
| 27 |
+
|
| 28 |
+
id_cols = [c for c in ['id', 'time', 'index'] if c in file.columns]
|
| 29 |
+
x = file.drop(columns=[target_col] + id_cols, errors='ignore')
|
| 30 |
+
y = file[target_col]
|
| 31 |
+
|
| 32 |
+
numeric_features = x.select_dtypes(include=['number']).columns.tolist()
|
| 33 |
+
categorical_features = x.select_dtypes(include=['object', 'category', 'bool']).columns.tolist()
|
| 34 |
+
# print("Numeric: ", len(numeric_features))
|
| 35 |
+
# print("Categorical: ", len(categorical_features))
|
| 36 |
+
# print("X: ", x)
|
| 37 |
+
# print("Y: ", y)
|
| 38 |
+
|
| 39 |
+
numeric_transformer = Pipeline([('imputer', SimpleImputer(strategy='median')),])
|
| 40 |
+
|
| 41 |
+
#categorical_transformer = Pipeline([('imputer', SimpleImputer(strategy='most_frequent')),
|
| 42 |
+
# ('ohe', OneHotEncoder(handle_unknown='ignore')),])
|
| 43 |
+
categorical_transformer = Pipeline([
|
| 44 |
+
('imputer', SimpleImputer(strategy='most_frequent')),
|
| 45 |
+
('ohe', OneHotEncoder(handle_unknown='ignore', sparse_output=False)),
|
| 46 |
+
])
|
| 47 |
+
|
| 48 |
+
print(f"Num:{len(numeric_features)}")
|
| 49 |
+
preprocessor = ColumnTransformer([
|
| 50 |
+
('num', numeric_transformer, numeric_features),
|
| 51 |
+
('cat', categorical_transformer, categorical_features),
|
| 52 |
+
], remainder='drop')
|
| 53 |
+
return x, y, preprocessor
|
| 54 |
+
|
| 55 |
+
def Training(x_train_clean, y_train, x_val_clean, y_val):
|
| 56 |
+
# from lightgbm import LGBMClassifier
|
| 57 |
+
# scale_pos_weight = (y_train==0).sum() / max(1, (y_train==1).sum())
|
| 58 |
+
# model = LGBMClassifier(
|
| 59 |
+
# objective='binary',
|
| 60 |
+
# n_estimators=10000,
|
| 61 |
+
# learning_rate=0.05,
|
| 62 |
+
# num_leaves=31,
|
| 63 |
+
# random_state=42,
|
| 64 |
+
# scale_pos_weight=scale_pos_weight
|
| 65 |
+
# )
|
| 66 |
+
# model.fit(
|
| 67 |
+
# x_train_tr, y_train,
|
| 68 |
+
# eval_set=[(x_val_tr, y_val)],
|
| 69 |
+
# eval_metric='auc',
|
| 70 |
+
# #my version does not support that method. I need to use callbacks
|
| 71 |
+
# #early_stopping_rounds=100,
|
| 72 |
+
# #callbacks=[early_stopping(100), log_evaluation(100)]
|
| 73 |
+
# )
|
| 74 |
+
# print("Best iteration:", model.best_iteration_)
|
| 75 |
+
# print("Train AUC:", model.best_score_['training']['auc'])
|
| 76 |
+
# print("Valid AUC:", model.best_score_['valid_0']['auc'])
|
| 77 |
+
from lightgbm import LGBMClassifier
|
| 78 |
+
|
| 79 |
+
# model = LGBMClassifier(
|
| 80 |
+
# objective='binary',
|
| 81 |
+
# n_estimators=2000,
|
| 82 |
+
# learning_rate=0.05,
|
| 83 |
+
# num_leaves=31,
|
| 84 |
+
# min_child_samples=1,
|
| 85 |
+
# random_state=42
|
| 86 |
+
# )
|
| 87 |
+
|
| 88 |
+
model = LGBMClassifier(
|
| 89 |
+
objective='binary',
|
| 90 |
+
n_estimators=2000,
|
| 91 |
+
learning_rate=0.05,
|
| 92 |
+
num_leaves=31,
|
| 93 |
+
min_child_samples=1,
|
| 94 |
+
min_split_gain=0.0, # permite splits mesmo sem ganho positivo aparente
|
| 95 |
+
min_data_in_leaf=1, # remove restrição mínima
|
| 96 |
+
random_state=42
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
model.fit(
|
| 100 |
+
x_train_clean, y_train,
|
| 101 |
+
eval_set=[(x_val_clean, y_val)],
|
| 102 |
+
eval_metric='auc',
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
print("Best iteration:", model.best_iteration_)
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
return model
|
| 109 |
+
|
| 110 |
+
|
| 111 |
+
|
| 112 |
+
from sklearn.model_selection import train_test_split
|
| 113 |
+
if os.path.exists('/content/drive') == 0:
|
| 114 |
+
drive.mount('/content/drive')
|
| 115 |
+
|
| 116 |
+
labels = pandas.read_csv('/content/drive/MyDrive/AI_assets/labels.csv')
|
| 117 |
+
light_curves = pandas.read_csv('/content/drive/MyDrive/AI_assets/light_curves.csv')
|
| 118 |
+
metadata = pandas.read_csv('/content/drive/MyDrive/AI_assets/metadata.csv')
|
| 119 |
+
#data = pandas.read_csv('/content/drive/MyDrive/exoplanets_normalized_data.csv')
|
| 120 |
+
data = pandas.read_csv('/content/drive/MyDrive/data.csv')
|
| 121 |
+
|
| 122 |
+
# im gonna change this under cause i nedd more classes. Using binary interpretation insted of 'CONFIRMED' will help a lot
|
| 123 |
+
#x, y, preprocessor = PipelineCreation(data, target_col='kepoi_name')
|
| 124 |
+
|
| 125 |
+
data['target'] = data['koi_disposition'].map(
|
| 126 |
+
lambda v: 1 if v == "CONFIRMED" else 0
|
| 127 |
+
)
|
| 128 |
+
|
| 129 |
+
x, y, preprocessor = PipelineCreation(data, target_col='target')
|
| 130 |
+
print("First step done. 1 -> PIPELINE CREATION")
|
| 131 |
+
# print("X: ", x)
|
| 132 |
+
# print("Y: ", y)
|
| 133 |
+
x_train, x_val, y_train, y_val = train_test_split(
|
| 134 |
+
x, y, test_size=0.20, stratify=y, random_state=42
|
| 135 |
+
)
|
| 136 |
+
preprocessor.fit(x_train)
|
| 137 |
+
x_train_tr = preprocessor.transform(x_train)
|
| 138 |
+
x_val_tr = preprocessor.transform(x_val)
|
| 139 |
+
EDA(labels)
|
| 140 |
+
|
| 141 |
+
#debug purposes
|
| 142 |
+
#print("Second step done. 2 -> EDA DONE")
|
| 143 |
+
#print("X_train shape:", x_train_tr.shape)
|
| 144 |
+
#print("X_val shape:", x_val_tr.shape)
|
| 145 |
+
#print("y_train distribution:\n", y_train.value_counts())
|
| 146 |
+
#print("y_val distribution:\n", y_val.value_counts())
|
| 147 |
+
#print("Check for NaNs:", x_train_tr.isna().sum().sum(), "in train,", x_val_tr.isna().sum().sum(), "in val")
|
| 148 |
+
import pandas as pd
|
| 149 |
+
|
| 150 |
+
x_train_df = pd.DataFrame(x_train_tr)
|
| 151 |
+
x_val_df = pd.DataFrame(x_val_tr)
|
| 152 |
+
|
| 153 |
+
# Remover colunas sem variância
|
| 154 |
+
valid_cols = x_train_df.columns[x_train_df.var() > 0]
|
| 155 |
+
x_train_clean = x_train_df[valid_cols]
|
| 156 |
+
x_val_clean = x_val_df[valid_cols]
|
| 157 |
+
|
| 158 |
+
import numpy as np
|
| 159 |
+
import pandas as pd
|
| 160 |
+
|
| 161 |
+
# X_train é seu conjunto de treino (DataFrame)
|
| 162 |
+
variancias = x_train_df.var()
|
| 163 |
+
sem_variancia = (variancias == 0).sum()
|
| 164 |
+
|
| 165 |
+
# print(f"Número de colunas com variância zero: {sem_variancia}/{len(variancias)}")
|
| 166 |
+
# print("Exemplo de variâncias não nulas:")
|
| 167 |
+
# print(variancias[variancias > 0].head())
|
| 168 |
+
|
| 169 |
+
# print("Número de colunas após filtragem:", len(valid_cols))
|
| 170 |
+
# print("Shape final de treino:", x_train_clean.shape)
|
| 171 |
+
# print("Distribuição de classes:")
|
| 172 |
+
# print(y_train.value_counts(normalize=True))
|
| 173 |
+
# print("Exemplo de valores:")
|
| 174 |
+
# print(x_train_clean.head())
|
| 175 |
+
|
| 176 |
+
|
| 177 |
+
# print("Tipo de dados:", x_train_clean.dtypes.unique())
|
| 178 |
+
# print("Faixa de valores nas primeiras colunas:")
|
| 179 |
+
# print(x_train_clean.iloc[:, :5].describe())
|
| 180 |
+
# import numpy as np
|
| 181 |
+
|
| 182 |
+
# print("Min:", np.min(x_train_clean.values))
|
| 183 |
+
# print("Max:", np.max(x_train_clean.values))
|
| 184 |
+
# print("Mean:", np.mean(x_train_clean.values))
|
| 185 |
+
# print("Std:", np.std(x_train_clean.values))
|
| 186 |
+
from sklearn.preprocessing import StandardScaler
|
| 187 |
+
from sklearn.feature_selection import VarianceThreshold
|
| 188 |
+
from lightgbm import LGBMClassifier
|
| 189 |
+
|
| 190 |
+
# === 1. Remover colunas quase constantes ===
|
| 191 |
+
var_sel = VarianceThreshold(threshold=1e-4)
|
| 192 |
+
x_train_filtered = var_sel.fit_transform(x_train_clean)
|
| 193 |
+
x_val_filtered = var_sel.transform(x_val_clean)
|
| 194 |
+
|
| 195 |
+
#print("Shape após VarianceThreshold:", x_train_filtered.shape)
|
| 196 |
+
|
| 197 |
+
# === 2. Padronizar ===
|
| 198 |
+
scaler = StandardScaler()
|
| 199 |
+
x_train_scaled = scaler.fit_transform(x_train_filtered)
|
| 200 |
+
x_val_scaled = scaler.transform(x_val_filtered)
|
| 201 |
+
|
| 202 |
+
# === 3. Treinar modelo ===
|
| 203 |
+
# model = LGBMClassifier(
|
| 204 |
+
# objective='binary',
|
| 205 |
+
# n_estimators=2000,
|
| 206 |
+
# learning_rate=0.05,
|
| 207 |
+
# num_leaves=31,
|
| 208 |
+
# random_state=42
|
| 209 |
+
# )
|
| 210 |
+
|
| 211 |
+
# model.fit(
|
| 212 |
+
# x_train_scaled, y_train,
|
| 213 |
+
# eval_set=[(x_val_scaled, y_val)],
|
| 214 |
+
# eval_metric='auc'
|
| 215 |
+
# )
|
| 216 |
+
|
| 217 |
+
# print("Best iteration:", model.best_iteration_)
|
| 218 |
+
|
| 219 |
+
#model = Training(x_train_clean, y_train, x_val_clean, y_val)
|
| 220 |
+
#print("Finished Training. 3!!")
|
| 221 |
+
# import numpy as np
|
| 222 |
+
# import pandas as pd
|
| 223 |
+
|
| 224 |
+
# # Converter se ainda estiver em numpy array
|
| 225 |
+
# x_train_df = pd.DataFrame(x_train_clean)
|
| 226 |
+
|
| 227 |
+
# # 1️⃣ Correlação média entre cada feature e o target
|
| 228 |
+
# corrs = []
|
| 229 |
+
# for col in x_train_df.columns:
|
| 230 |
+
# try:
|
| 231 |
+
# corrs.append(abs(np.corrcoef(x_train_df[col], y_train)[0,1]))
|
| 232 |
+
# except:
|
| 233 |
+
# corrs.append(0)
|
| 234 |
+
# mean_corr = np.nanmean(corrs)
|
| 235 |
+
# print("Correlação média com o target:", mean_corr)
|
| 236 |
+
|
| 237 |
+
# # 2️⃣ Contar quantas colunas têm correlação > 0.05
|
| 238 |
+
# useful = np.sum(np.array(corrs) > 0.05)
|
| 239 |
+
# print("Features com correlação > 0.05:", useful, "/", len(corrs))
|
| 240 |
+
|
| 241 |
+
|
| 242 |
+
# A litle degub made by GPT
|
| 243 |
+
# print("Best iteration:", model.best_iteration_)
|
| 244 |
+
# print("Train AUC:", model.best_score_['training']['auc'])
|
| 245 |
+
# print("Valid AUC:", model.best_score_['valid_0']['auc'])
|
| 246 |
+
import numpy as np
|
| 247 |
+
|
| 248 |
+
if hasattr(x_train_clean, "todense"):
|
| 249 |
+
x_train_clean = np.array(x_train_clean.todense(), dtype=np.float32)
|
| 250 |
+
x_val_clean = np.array(x_val_clean.todense(), dtype=np.float32)
|
| 251 |
+
else:
|
| 252 |
+
x_train_clean = np.array(x_train_clean, dtype=np.float32)
|
| 253 |
+
x_val_clean = np.array(x_val_clean, dtype=np.float32)
|
| 254 |
+
print("Treino - tipo:", type(x_train_clean))
|
| 255 |
+
print("Treino - shape:", x_train_clean.shape)
|
| 256 |
+
print("Treino - dtype:", x_train_clean.dtype)
|
| 257 |
+
print("Valores únicos de y:", np.unique(y_train, return_counts=True))
|
| 258 |
+
|
| 259 |
+
#model = Training(x_train_clean, y_train, x_val_clean, y_val)
|
| 260 |
+
from lightgbm import LGBMClassifier
|
| 261 |
+
|
| 262 |
+
model = LGBMClassifier(
|
| 263 |
+
objective='binary',
|
| 264 |
+
learning_rate=0.01,
|
| 265 |
+
n_estimators=2000,
|
| 266 |
+
random_state=42
|
| 267 |
+
)
|
| 268 |
+
model.fit(x_train_clean, y_train)
|
| 269 |
+
train_score = model.score(x_train_clean, y_train)
|
| 270 |
+
print("Treino score:", train_score)
|
| 271 |
+
|
| 272 |
+
from sklearn.metrics import accuracy_score
|
| 273 |
+
|
| 274 |
+
y_pred = model.predict(x_val_clean)
|
| 275 |
+
acc = accuracy_score(y_val, y_pred)
|
| 276 |
+
print(f"Acurácia: {acc:.4f}")
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask
|
| 2 |
+
pandas
|
| 3 |
+
joblib
|
| 4 |
+
scikit-learn
|
| 5 |
+
lightgbm
|