Nathanotal commited on
Commit
1c8a8bf
·
1 Parent(s): a786f9b
Files changed (1) hide show
  1. app.py +55 -8
app.py CHANGED
@@ -47,6 +47,8 @@ featureToName = {
47
  'streetName' : 'Name of street',
48
  }
49
 
 
 
50
  def downloadAutogluonModel():
51
  # Download saved Autogluon model from Hopsworks
52
  project = hopsworks.login(
@@ -246,19 +248,61 @@ def xgboostPred(df):
246
 
247
  return results
248
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
249
  def autoPred(df):
250
- print(df.head())
251
- print(df.columns)
252
  res = autoModel.predict(df)
253
 
254
  # Convert to a list
255
  res = res.tolist()
256
 
257
- print('________________________________')
258
- print(res, type(res))
259
- print('________________________________')
 
 
 
 
 
260
 
261
- return res
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
262
 
263
  def isValidInput(streetName, number, sqm, rooms, monthlyFee, monthlyCost, floor, yearBuilt):
264
  # Street name is a string, all other values are numbers
@@ -327,8 +371,7 @@ def sthlm(streetName, number, sqm, rooms, monthlyFee, monthlyCost, floor, yearBu
327
 
328
  pricePred = None
329
  if auto:
330
- pricePred = autoPred(df)
331
- '', 'Autogluon is not working right now, please try again later'
332
  else:
333
  df = xgbFix(df)
334
  pricePred = xgboostPred(df)
@@ -347,6 +390,10 @@ def sthlm(streetName, number, sqm, rooms, monthlyFee, monthlyCost, floor, yearBu
347
  result.append(f'If sold {explanation} it would have been worth more: {parsePrice(pred)} (+{parsePrice(diff)})')
348
  else:
349
  result.append(f'If sold {explanation} it would have been worth less: {parsePrice(pred)} ({parsePrice(diff)})')
 
 
 
 
350
 
351
  return '\n'.join(result), ''
352
 
 
47
  'streetName' : 'Name of street',
48
  }
49
 
50
+ topAgencies = ['Fastighetsbyrån','Notar','Svensk Fastighetsförmedling','HusmanHagberg','Länsförsäkringar Fastighetsförmedling','Erik Olsson','SkandiaMäklarna','Svenska Mäklarhuset','Bjurfors','Mäklarhuset','BOSTHLM','Innerstadsspecialisten','MOHV','Mäklarringen','Historiska Hem','Södermäklarna','Karlsson & Uddare','UNIK Fastighetsförmedling','Edward & Partners','Widerlöv']
51
+
52
  def downloadAutogluonModel():
53
  # Download saved Autogluon model from Hopsworks
54
  project = hopsworks.login(
 
248
 
249
  return results
250
 
251
+ def addExtraAgencyFun(df):
252
+ # Make 20 copies of the first row with the 20 different top agencies in Sweden
253
+ # Make a copy of the first row
254
+ firstRow = df.iloc[0]
255
+ # Make a list of the copies
256
+ rows = [firstRow] * len(topAgencies)
257
+ # Make a dataframe from the list
258
+ df2 = pd.DataFrame(rows)
259
+
260
+ # Add the top agencies to the dataframe
261
+ for i, agency in enumerate(topAgencies):
262
+ df2['agency'].iloc[i] = agency
263
+
264
+ # Concatenate the two dataframes
265
+ df = pd.concat([df, df2], ignore_index=True)
266
+
267
+ return df
268
+
269
  def autoPred(df):
270
+ df = addExtraAgencyFun(df)
 
271
  res = autoModel.predict(df)
272
 
273
  # Convert to a list
274
  res = res.tolist()
275
 
276
+ # Get the last 20 values
277
+ agencyResults = res[-20:]
278
+ res = res[:-20]
279
+
280
+ # Get the mean of the agencies
281
+ meanPrice = sum(agencyResults) / len(agencyResults)
282
+
283
+ agencyToResult = {agency:result for agency, result in zip(topAgencies, agencyResults)}
284
 
285
+ # Get the top and bottom 3 agencies with the highest results
286
+ sortedAgencies = sorted(agencyToResult.items(), key=lambda x: x[1])
287
+ top3 = sortedAgencies[-3:]
288
+ bottom3 = sortedAgencies[:3]
289
+
290
+ agencyString = parseAgencyResult(top3, bottom3, meanPrice)
291
+
292
+ return res, agencyString
293
+
294
+ def parseAgencyResult(top3, bottom3, meanPrice):
295
+ toReturn = 'To get the most money for your apartment, you should sell it with the help of one of these agencies:\n'
296
+ toReturn += 'Top 3:\n'
297
+ for agency, result in top3:
298
+ diff = result - meanPrice
299
+ toReturn += f'{agency}: {parsePrice(result)} ({parsePrice(diff)} above mean)\n'
300
+ toReturn += '\n'
301
+ for agency, result in bottom3:
302
+ diff = result - meanPrice
303
+ toReturn += f'{agency}: {parsePrice(result)} ({parsePrice(diff)} below mean)\n'
304
+
305
+ return toReturn
306
 
307
  def isValidInput(streetName, number, sqm, rooms, monthlyFee, monthlyCost, floor, yearBuilt):
308
  # Street name is a string, all other values are numbers
 
371
 
372
  pricePred = None
373
  if auto:
374
+ pricePred, agencyInfo = autoPred(df)
 
375
  else:
376
  df = xgbFix(df)
377
  pricePred = xgboostPred(df)
 
390
  result.append(f'If sold {explanation} it would have been worth more: {parsePrice(pred)} (+{parsePrice(diff)})')
391
  else:
392
  result.append(f'If sold {explanation} it would have been worth less: {parsePrice(pred)} ({parsePrice(diff)})')
393
+ if auto:
394
+ result.append(agencyInfo)
395
+
396
+
397
 
398
  return '\n'.join(result), ''
399