Nahrawy commited on
Commit
ef6ac2d
1 Parent(s): ad3fd13

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +5 -9
app.py CHANGED
@@ -3,14 +3,9 @@ import numpy as np
3
  import matplotlib.pyplot as plt
4
  from sklearn import linear_model
5
 
6
- def plot(seed, num_points):
7
- # Error handling of non-numeric seeds
8
- if seed and not seed.isnumeric():
9
- raise gr.Error("Invalid seed")
10
-
11
  # Setting the seed
12
- if seed:
13
- seed = int(seed)
14
  np.random.seed(seed)
15
  num_points = int(num_points)
16
 
@@ -22,7 +17,7 @@ def plot(seed, num_points):
22
  X = np.r_[np.random.randn(half_num_points, 2) + [1, 1], np.random.randn(half_num_points, 2)]
23
  y = [1] * half_num_points + [-1] * half_num_points
24
  sample_weight = 100 * np.abs(np.random.randn(num_points))
25
- # and assign a bigger weight to the last 10 samples
26
  sample_weight[:half_num_points] *= 10
27
 
28
  # plot the weighted data points
@@ -65,6 +60,7 @@ def plot(seed, num_points):
65
 
66
  info = ''' # SGD: Weighted samples\n
67
  This is a demonstration of a modified version of [SGD](https://scikit-learn.org/stable/modules/sgd.html#id5) that takes into account the weights of the samples. Where the size of points is proportional to its weight.\n
 
68
  Created by [@Nahrawy](https://huggingface.co/Nahrawy) based on [scikit-learn docs](https://scikit-learn.org/stable/auto_examples/linear_model/plot_sgd_weighted_samples.html).
69
  '''
70
 
@@ -72,7 +68,7 @@ with gr.Blocks() as demo:
72
  gr.Markdown(info)
73
  with gr.Row():
74
  with gr.Column():
75
- seed = gr.Textbox(label="Seed", info="Leave empty to generate new random points each run ",value=None)
76
  num_points = gr.Slider(label="Number of Points", value="20", minimum=5, maximum=100, step=2)
77
  btn = gr.Button("Run")
78
  out = gr.Plot()
 
3
  import matplotlib.pyplot as plt
4
  from sklearn import linear_model
5
 
6
+ def plot(seed, num_points):
 
 
 
 
7
  # Setting the seed
8
+ if seed != -1:
 
9
  np.random.seed(seed)
10
  num_points = int(num_points)
11
 
 
17
  X = np.r_[np.random.randn(half_num_points, 2) + [1, 1], np.random.randn(half_num_points, 2)]
18
  y = [1] * half_num_points + [-1] * half_num_points
19
  sample_weight = 100 * np.abs(np.random.randn(num_points))
20
+ # and assign a bigger weight to the second half of samples
21
  sample_weight[:half_num_points] *= 10
22
 
23
  # plot the weighted data points
 
60
 
61
  info = ''' # SGD: Weighted samples\n
62
  This is a demonstration of a modified version of [SGD](https://scikit-learn.org/stable/modules/sgd.html#id5) that takes into account the weights of the samples. Where the size of points is proportional to its weight.\n
63
+ The algorithm is demonstrated using points sampled from the standard normal distribution, where the weighted class has a mean of one while the non-weighted class has a mean of zero.\n
64
  Created by [@Nahrawy](https://huggingface.co/Nahrawy) based on [scikit-learn docs](https://scikit-learn.org/stable/auto_examples/linear_model/plot_sgd_weighted_samples.html).
65
  '''
66
 
 
68
  gr.Markdown(info)
69
  with gr.Row():
70
  with gr.Column():
71
+ seed = gr.Slider(label="Seed", minimum=-1, maximum=10000, step=1,info="Set to -1 to generate new random points each run ",value=-1)
72
  num_points = gr.Slider(label="Number of Points", value="20", minimum=5, maximum=100, step=2)
73
  btn = gr.Button("Run")
74
  out = gr.Plot()