Spaces:
No application file
No application file
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)) | |
return full_country_stats[["GDP per capita", 'Life satisfaction']].iloc[keep_indices] | |
import gradio as gr | |
import matplotlib.pyplot as plt | |
import numpy as np | |
import pandas as pd | |
import sklearn.linear_model | |
import sklearn.neighbors | |
# define the processing function | |
def my_function(input): | |
# replace with your own function code | |
# Load the data | |
oecd_bli = pd.read_csv("oecd.csv", thousands=',') | |
gdp_per_capita = pd.read_csv("gdp.csv",thousands=',',delimiter='\t', | |
encoding='latin1', na_values="n/a") | |
# Prepare the data | |
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"]] | |
# Visualize the data | |
country_stats.plot(kind='scatter', x="GDP per capita", y='Life satisfaction') | |
plt.show() | |
# Select a linear model | |
model = sklearn.neighbors.KNeighborsRegressor(n_neighbors=3) | |
# Train the model | |
model.fit(X, y) | |
# Make a prediction for Cyprus | |
X_new = [[35710]] # Cyprus' GDP per capita | |
#print(model.predict(X_new)) | |
#reseting country index | |
country_stats = country_stats.reset_index() | |
k = int(input) | |
model = sklearn.neighbors.KNeighborsRegressor(n_neighbors=k) | |
# Train the model | |
model.fit(X, y) | |
X_new = [[22587]] | |
r=model.predict(X_new) | |
v=float(r) | |
value = round(v,1) | |
for i in range(0,29): | |
if country_stats['Life satisfaction'][i]==value: | |
b =country_stats['Country'][i] | |
output = str(b) | |
return output | |
# create a Gradio interface | |
inputs = gr.inputs.Textbox(label="Enter the value of k ") | |
outputs = gr.outputs.Textbox(label="Output") | |
interface = gr.Interface(fn=my_function, inputs=inputs, outputs=outputs, title="My Gradio Interface") | |
# launch the interface | |
interface.launch() | |