marik0 commited on
Commit
ce2800a
·
1 Parent(s): 66f182f

Create the lr example

Browse files
Files changed (2) hide show
  1. app.py +90 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ import matplotlib.pyplot as plt
4
+ import numpy as np
5
+ from sklearn import datasets, linear_model
6
+ from sklearn.metrics import mean_squared_error, r2_score
7
+
8
+ from functools import partial
9
+
10
+ FIGSIZE = (10,10)
11
+
12
+ feature_names = ["age", "body-mass index (BMI)", "blood pressure",
13
+ "total serum cholesterol", "low-density lipoproteins (LDL)",
14
+ "high-density lipoproteins (HDL)", "total cholesterol / HDL",
15
+ "log of serum triglycerides level (possibly)","blood sugar level"]
16
+
17
+ def create_dataset(feature_id=2):
18
+ # Load the diabetes dataset
19
+ diabetes_X, diabetes_y = datasets.load_diabetes(return_X_y=True)
20
+
21
+ # Use only one feature
22
+ diabetes_X = diabetes_X[:, np.newaxis, feature_id]
23
+
24
+ # Split the data into training/testing sets
25
+ diabetes_X_train = diabetes_X[:-20]
26
+ diabetes_X_test = diabetes_X[-20:]
27
+
28
+ # Split the targets into training/testing sets
29
+ diabetes_y_train = diabetes_y[:-20]
30
+ diabetes_y_test = diabetes_y[-20:]
31
+
32
+ return diabetes_X_train, diabetes_X_test, diabetes_y_train, diabetes_y_test
33
+
34
+ def train_model(input_data):
35
+
36
+ # We reomved the sex variable
37
+ if input_data == 'age':
38
+ feature_id = 0
39
+ else:
40
+ feature_id = feature_names.index(input_data) + 1
41
+
42
+ diabetes_X_train, diabetes_X_test, diabetes_y_train, diabetes_y_test = create_dataset(feature_id)
43
+
44
+
45
+ # Create linear regression object
46
+ regr = linear_model.LinearRegression()
47
+
48
+ # Train the model using the training sets
49
+ regr.fit(diabetes_X_train, diabetes_y_train)
50
+
51
+ # Make predictions using the testing set
52
+ diabetes_y_pred = regr.predict(diabetes_X_test)
53
+
54
+ mse = mean_squared_error(diabetes_y_test, diabetes_y_pred)
55
+ r2 = r2_score(diabetes_y_test, diabetes_y_pred)
56
+
57
+ # Plot outputs
58
+ fig = plt.figure(figsize=FIGSIZE)
59
+
60
+ plt.title(input_data)
61
+ plt.scatter(diabetes_X_test, diabetes_y_test, color="black")
62
+ plt.plot(diabetes_X_test, diabetes_y_pred, color="blue", linewidth=3)
63
+
64
+ plt.xticks(())
65
+ plt.yticks(())
66
+
67
+
68
+
69
+ return fig, regr.coef_, mse, r2
70
+
71
+ title = "Linear Regression Example"
72
+ description = "The example shows how linear regression attempts to draw a straight line that will best minimize the residual sum of squares between the observed responses in the dataset"
73
+ with gr.Blocks() as demo:
74
+ gr.Markdown(f"## {title}")
75
+ gr.Markdown(description)
76
+
77
+ with gr.Column():
78
+
79
+ with gr.Row():
80
+ plot = gr.Plot(label="Feature")
81
+ with gr.Column():
82
+ input_data = gr.Dropdown(choices=feature_names, label="Feature", value="body-mass index")
83
+ coef = gr.Textbox(label="Coefficients")
84
+ mse = gr.Textbox(label="MSE")
85
+ r2 = gr.Textbox(label="R2")
86
+
87
+ input_data.change(fn=train_model, inputs=[input_data], outputs=[plot, coef, mse, r2], queue=False)
88
+
89
+
90
+ demo.launch(enable_queue=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ scikit-learn
2
+ matplotlib
3
+ numpy
4
+