| import os | |
| import pandas as pd | |
| from sklearn.ensemble import IsolationForest | |
| import numpy as np | |
| from sklearn.model_selection import train_test_split | |
| import gradio as gr | |
| #Data preparation | |
| n_samples, n_outliers = 120, 40 | |
| rng = np.random.RandomState(0) | |
| covariance = np.array([[0.5, -0.1], [0.7, 0.4]]) | |
| cluster_1 = 0.4 * rng.randn(n_samples, 2) @ covariance + np.array([2, 2]) # general deformed cluster | |
| cluster_2 = 0.3 * rng.randn(n_samples, 2) + np.array([-2, -2]) # spherical cluster | |
| outliers = rng.uniform(low=-4, high=4, size=(n_outliers, 2)) | |
| X = np.concatenate([cluster_1, cluster_2, outliers]) #120+120+40 = 280 with 2D | |
| y = np.concatenate( | |
| [np.ones((2 * n_samples), dtype=int), -np.ones((n_outliers), dtype=int)] | |
| ) | |
| #Visualize the data as a scatter plot | |
| # def predict_survival(passenger_class, is_male, age, company, fare, embark_point): | |
| # if passenger_class is None or embark_point is None: | |
| # return None | |
| # df = pd.DataFrame.from_dict( | |
| # { | |
| # "Pclass": [passenger_class + 1], | |
| # "Sex": [0 if is_male else 1], | |
| # "Age": [age], | |
| # "Fare": [fare], | |
| # "Embarked": [embark_point + 1], | |
| # "Company": [ | |
| # (1 if "Sibling" in company else 0) + (2 if "Child" in company else 0) | |
| # ] | |
| # } | |
| # ) | |
| # df = encode_age(df) | |
| # df = encode_fare(df) | |
| # pred = clf.predict_proba(df)[0] | |
| # return {"Perishes": float(pred[0]), "Survives": float(pred[1])} | |
| # demo = gr.Interface( | |
| # predict_survival, | |
| # [ | |
| # gr.Dropdown(["first", "second", "third"], type="index"), | |
| # "checkbox", | |
| # gr.Slider(0, 80, value=25), | |
| # gr.CheckboxGroup(["Sibling", "Child"], label="Travelling with (select all)"), | |
| # gr.Number(value=20), | |
| # gr.Radio(["S", "C", "Q"], type="index"), | |
| # ], | |
| # "label", | |
| # examples=[ | |
| # ["first", True, 30, [], 50, "S"], | |
| # ["second", False, 40, ["Sibling", "Child"], 10, "Q"], | |
| # ["third", True, 30, ["Child"], 20, "S"], | |
| # ], | |
| # interpretation="default", | |
| # live=True, | |
| # ) | |
| # if __name__ == "__main__": | |
| # demo.launch() |