You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

64 lines
1.7 KiB

6 years ago
6 years ago
  1. # coding=utf-8
  2. import sys
  3. reload(sys)
  4. sys.setdefaultencoding('utf8')
  5. import numpy as np
  6. import pandas as pd
  7. import matplotlib.pyplot as plt
  8. from datetime import datetime, date, timedelta
  9. shops = pd.read_csv('datasets/fairMarket-shops.csv')
  10. print shops.tail(1)
  11. #generate the full set of days from the first date to the last in the dataset
  12. months = []
  13. days = []
  14. dInit = date(2015, 5, 04)
  15. dEnd = date(2017, 8, 31)
  16. delta = dEnd - dInit
  17. for i in range(delta.days+1):
  18. day = dInit + timedelta(days=i)
  19. dayString = day.strftime("%d/%m/%y")
  20. dayDatetime = datetime.strptime(dayString, '%d/%m/%y')
  21. days.append(dayDatetime)
  22. #add the dates of shops creation to the days array
  23. for shopDate in shops['Created on']:
  24. if isinstance(shopDate, basestring):
  25. shopDay = str.split(shopDate)[0]
  26. shopDayDatetime = datetime.strptime(shopDay, '%d/%m/%y')
  27. days.append(shopDayDatetime)
  28. #count days frequency in days array
  29. unique, counts = np.unique(days, return_counts=True)
  30. countDays = dict(zip(unique, counts))
  31. realCounts = []
  32. for count in counts:
  33. realCounts.append(count-1)
  34. #count the total acumulation of shops created in each days
  35. totalCount = 0
  36. globalCount = []
  37. for k in realCounts:
  38. totalCount = totalCount + k
  39. globalCount.append(totalCount)
  40. dates = countDays.values()
  41. counts = countDays.values()
  42. #plot the data
  43. plt.title("New shops opened each day")
  44. plt.plot(unique, realCounts)
  45. plt.show()
  46. plt.title("Total shops each day")
  47. plt.plot(unique, globalCount)
  48. plt.show()
  49. plt.title("New shops and total shops each day")
  50. plt.plot(unique, realCounts, label="new shops opened each day")
  51. plt.plot(unique, globalCount, label="total shops each day")
  52. plt.legend(loc='upper left')
  53. plt.show()