File size: 2,488 Bytes
aaf4dbc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ea3b37
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
36bf081
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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()