import matplotlib.pyplot as plt import numpy as np import pandas as pd import sklearn.linear_model import os import sklearn.neighbors import gradio as gr datapath = os.path.join("datasets", "lifesat", "") # Download the data import urllib.request # DOWNLOAD_ROOT = "https://raw.githubusercontent.com/ageron/handson-ml/master/" # os.makedirs(datapath, exist_ok=True) # for filename in ("oecd_bli_2015.csv", "gdp_per_capita.csv"): # print("Downloading", filename) # url = DOWNLOAD_ROOT + "datasets/lifesat/" + filename # urllib.request.urlretrieve(url, datapath + filename) def prepare_country_stats(oecd_bli, gdp_per_capita): oecd_bli = oecd_bli[oecd_bli["INEQUALITY"]=="TOT"] oecd_bli = oecd_bli.pivot(index="Country", columns="Indicator",values="Value") gdp_per_capita.rename(columns={"2015": "GDP per capita"}, inplace=True) gdp_per_capita.set_index("Country", inplace=True) full_country_stats = pd.merge(left=oecd_bli, right=gdp_per_capita, left_index=True, right_index=True) full_country_stats.sort_values(by="GDP per capita", inplace=True) remove_indices = [0, 1, 6, 8, 33, 34, 35] keep_indices = list(set(range(36)) - set(remove_indices)) # print(full_country_stats.head()) return full_country_stats[["GDP per capita", 'Life satisfaction']].iloc[keep_indices] # oecd_bli = pd.read_csv(datapath + "oecd_bli_2015.csv", thousands=',') # gdp_per_capita = pd.read_csv(datapath + "gdp_per_capita.csv",thousands=',',delimiter='\t', # encoding='latin1', na_values="n/a") oecd_bli = pd.read_csv("oecd_bli_2015.csv", thousands=',') gdp_per_capita = pd.read_csv("gdp_per_capita.csv",thousands=',',delimiter='\t', encoding='latin1', na_values="n/a") country_stats = prepare_country_stats(oecd_bli, gdp_per_capita) X = np.c_[country_stats["GDP per capita"]] y = np.c_[country_stats["Life satisfaction"]] models = [] # Select a linear model model1 = sklearn.neighbors.KNeighborsRegressor(n_neighbors=1) model1.fit(X, y) models.append(model1) model2 = sklearn.neighbors.KNeighborsRegressor(n_neighbors=2) model2.fit(X, y) models.append(model2) model3 = sklearn.neighbors.KNeighborsRegressor(n_neighbors=3) model3.fit(X, y) models.append(model3) model4 = sklearn.neighbors.KNeighborsRegressor(n_neighbors=4) model4.fit(X, y) models.append(model4) model5 = sklearn.neighbors.KNeighborsRegressor(n_neighbors=5) model5.fit(X, y) models.append(model5) model6 = sklearn.neighbors.KNeighborsRegressor(n_neighbors=6) model6.fit(X, y) models.append(model6) model7 = sklearn.neighbors.KNeighborsRegressor(n_neighbors=7) model7.fit(X, y) models.append(model7) model8 = sklearn.neighbors.KNeighborsRegressor(n_neighbors=8) model8.fit(X, y) models.append(model8) model9 = sklearn.neighbors.KNeighborsRegressor(n_neighbors=9) model9.fit(X, y) models.append(model9) import gradio as gr def sentence_builder(gdp, value_of_k): # return f"""The Value of Happiness for {gdp} and K = {value_of_k} is {models[int(value_of_k)].predict([[gdp]])[0][0]}""" return f"""The Value of Happiness for {gdp} and K = {value_of_k} is {models[int(value_of_k) - 1].predict([[gdp]])[0][0]}""" demo = gr.Interface( sentence_builder, [ gr.Slider(0, 99999, value=4), gr.Dropdown([1, 2, 3, 4, 5, 6, 7, 8, 9]), ], "text", examples=[ [2000, 1], [5508, 5], ], ) demo.launch()