def median(fieldName): count = hubway.MySQLquery("SELECT COUNT(" + fieldName + ") FROM trips;")[0][0] if count % 2 == 0: medQuery = "SELECT " + fieldName + " FROM trips ORDER BY " + fieldName + " LIMIT " + str( count / 2) + ",1;" median = hubway.MySQLquery(medQuery) else: medQuery = "SELECT " + fieldName + " FROM trips ORDER BY " + fieldName + " LIMIT " + str( int(scipy.floor(count / 2))) + ",2;" median = hubway.MySQLquery(medQuery) return median
def agg_stats(fieldName): minQuery = "SELECT MIN(" + fieldName + ") FROM trips" min = hubway.MySQLquery(minQuery)[0][0] print "Field Mininum:", min maxQuery = "SELECT MAX(" + fieldName + ") FROM trips" max = hubway.MySQLquery(maxQuery)[0][0] print "Field Maximum:", max avgQuery = "SELECT AVG(" + fieldName + ") FROM trips" avg = hubway.MySQLquery(avgQuery)[0][0] print "Field Mean:", avg stdQuery = "SELECT STDDEV_SAMP(" + fieldName + ") FROM trips" std = hubway.MySQLquery(stdQuery)[0][0] print "Field Std:", std return [min, max, avg, std]
def modal_stats(fieldName): freqQuery = "SELECT %s, COUNT(%s) AS frequency FROM trips GROUP BY %s ORDER BY %s;" % ( fieldName, fieldName, fieldName, fieldName) freqs = hubway.MySQLquery(freqQuery) # print "Mode:",freqs[0] print "Unique Items:", len(freqs) return freqs[1:]
def modal_stats(fieldName, filename="output"): freqQuery = "SELECT %s, COUNT(%s) AS frequency FROM trips GROUP BY %s ORDER BY %s;" % ( fieldName, fieldName, fieldName, fieldName) freqs = hubway.MySQLquery(freqQuery) # print "Mode:",freqs[0] print "Unique Items:", len(freqs) #write to file here writeToFile(freqs[1:], filename) return freqs[1:]
def dateQuery(dateField, subField="month"): queryString = "SELECT %s(%s), COUNT(*) AS frequency FROM trips GROUP BY %s(%s) ORDER BY %s;" % ( subField.upper(), dateField, subField.upper(), dateField, dateField) freq = hubway.MySQLquery(queryString) return freq[1:]
def stationQuery(station, filename="output"): queryString = "SELECT %s, COUNT(*) AS frequency FROM trips GROUP BY MONTH(%s), HOUR(%s) ORDER BY %s;" % ( dateField, dateField, dateField, dateField, dateField, dateField) freq = hubway.MySQLquery(queryString) writeToFile(freq[1:], filename) return freq[1:]