cpv2280 commited on
Commit
2b8c7ec
·
verified ·
1 Parent(s): 4d5d5aa

Upload 3 files

Browse files
Files changed (3) hide show
  1. KNN.pkl +3 -0
  2. app.py +25 -6
  3. random_forests.pkl +3 -0
KNN.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:79d2a6ccf9a736551776e3c7539aa31a68bf01d1cdf3dcfd90f0087f43f7eed9
3
+ size 1171
app.py CHANGED
@@ -13,6 +13,8 @@ linear_model = joblib.load("linear_model.pkl")
13
  poly_model = joblib.load("poly_model.pkl")
14
  poly_features = joblib.load("poly_features.pkl")
15
  scaler = joblib.load("scaler.pkl") # Load the saved StandardScaler
 
 
16
 
17
  # Function to load and preview CSV data
18
  def load_data(file):
@@ -33,7 +35,7 @@ def plot_population_trend(file, model_choice):
33
  plt.grid()
34
 
35
  plt.figure(figsize=(8,5))
36
- plt.scatter(df["Year"], df["Total_Population"], label="Actual Population", color="blue", alpha=0.6)
37
 
38
  X = df["Year"].values.reshape(-1, 1) # Extract Year column
39
 
@@ -41,11 +43,25 @@ def plot_population_trend(file, model_choice):
41
  X_scaled = scaler.transform(X)
42
  predictions = linear_model.predict(X_scaled)
43
  plt.plot(df["Year"], predictions, label="Linear Regression", color="red", linestyle="dashed")
44
- else: # Polynomial Regression
45
  X_scaled = scaler.transform(X) # Apply scaling
46
  X_poly = poly_features.transform(X_scaled) # Transform for Polynomial Regression
47
  predictions = poly_model.predict(X_poly)
48
  plt.plot(df["Year"], predictions, label="Polynomial Regression", color="green")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49
 
50
  plt.xlabel("Year")
51
  plt.ylabel("Population")
@@ -76,11 +92,14 @@ def predict_population(file, model_choice):
76
  # Do NOT scale X for Linear Regression
77
  X_scaled = scaler.transform(X)
78
  predictions = linear_model.predict(X_scaled)
79
- else: # Polynomial Regression
80
  X_scaled = scaler.transform(X) # Apply the same scaling as training
81
  X_poly = poly_features.transform(X_scaled) # Transform for Polynomial Regression
82
  predictions = poly_model.predict(X_poly)
83
-
 
 
 
84
 
85
 
86
  df["Predicted Population"] = predictions # Append predictions to DataFrame
@@ -116,7 +135,7 @@ interface = gr.Interface(
116
  fn=gradio_interface, # Use the single wrapper function
117
  inputs=[
118
  gr.File(label="Upload CSV File"),
119
- gr.Radio(["Linear Regression", "Polynomial Regression"], label="Choose Model")
120
  ],
121
  outputs=[
122
  gr.Dataframe(label="Preview Data"),
@@ -125,7 +144,7 @@ interface = gr.Interface(
125
  gr.Textbox(label="Model Performance")
126
  ],
127
  title="Population Prediction Tool",
128
- description="Upload a CSV file with Year and Population data. Choose a model (Linear or Polynomial Regression) to predict future population trends."
129
  )
130
 
131
  # Launch the Gradio App
 
13
  poly_model = joblib.load("poly_model.pkl")
14
  poly_features = joblib.load("poly_features.pkl")
15
  scaler = joblib.load("scaler.pkl") # Load the saved StandardScaler
16
+ knn_model = joblib.load("KNN.pkl")
17
+ randforests_model = joblib.load("random_forests.pkl")
18
 
19
  # Function to load and preview CSV data
20
  def load_data(file):
 
35
  plt.grid()
36
 
37
  plt.figure(figsize=(8,5))
38
+ plt.scatter(df["Year"], df["Population"], label="Actual Population", color="blue", alpha=0.6)
39
 
40
  X = df["Year"].values.reshape(-1, 1) # Extract Year column
41
 
 
43
  X_scaled = scaler.transform(X)
44
  predictions = linear_model.predict(X_scaled)
45
  plt.plot(df["Year"], predictions, label="Linear Regression", color="red", linestyle="dashed")
46
+ elif model_choice == "Polynomial Regression": # Polynomial Regression
47
  X_scaled = scaler.transform(X) # Apply scaling
48
  X_poly = poly_features.transform(X_scaled) # Transform for Polynomial Regression
49
  predictions = poly_model.predict(X_poly)
50
  plt.plot(df["Year"], predictions, label="Polynomial Regression", color="green")
51
+ elif model_choice == "KNN": # K-Nearest Neighbors (KNN)
52
+ predictions = knn_model.predict(X)
53
+ label = "KNN"
54
+ color = "blue"
55
+ linestyle = "dotted"
56
+ plt.plot(df["Year"], predictions, label="KNN", color="blue")
57
+ else: #Random Forests
58
+ predictions = randforests_model.predict(X)
59
+ label = "Random Forests"
60
+ color = "yellow"
61
+ linestyle = "dotted"
62
+ plt.plot(df["Year"], predictions, label="Random Forests", color="yellow")
63
+
64
+
65
 
66
  plt.xlabel("Year")
67
  plt.ylabel("Population")
 
92
  # Do NOT scale X for Linear Regression
93
  X_scaled = scaler.transform(X)
94
  predictions = linear_model.predict(X_scaled)
95
+ elif model_choice== "Polynomial Regression": # Polynomial Regression
96
  X_scaled = scaler.transform(X) # Apply the same scaling as training
97
  X_poly = poly_features.transform(X_scaled) # Transform for Polynomial Regression
98
  predictions = poly_model.predict(X_poly)
99
+ elif model_choice == "KNN":
100
+ predictions = knn_model.predict(X)
101
+ else:#random forests
102
+ predictions = randforests_model.predict(X)
103
 
104
 
105
  df["Predicted Population"] = predictions # Append predictions to DataFrame
 
135
  fn=gradio_interface, # Use the single wrapper function
136
  inputs=[
137
  gr.File(label="Upload CSV File"),
138
+ gr.Radio(["Linear Regression", "Polynomial Regression","KNN", "Random Forests"], label="Choose Model")
139
  ],
140
  outputs=[
141
  gr.Dataframe(label="Preview Data"),
 
144
  gr.Textbox(label="Model Performance")
145
  ],
146
  title="Population Prediction Tool",
147
+ description="Upload a CSV file with Year and Population data. Choose a model (Linear or Polynomial Regression, KNN or Random Forests) to predict future population trends."
148
  )
149
 
150
  # Launch the Gradio App
random_forests.pkl ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:02ede939310eefd2d36ea586f799203e8a6a3ba840b11b0d584389bff51c0b85
3
+ size 9985