def build_temp_calendar(temps, year, month, day): day_of_year = np.zeros(temps.size) for i_row in range(temps.size): day_of_year[i_row] = tools.find_day_of_year(year[i_row], month[i_row], day[i_row]) median_temp_calendar = np.zeros(366) ten_day_medians = np.zeros(temps.size) for i_day in range(0, 365): # create 10-day window low_day = i_day - 5 high_day = i_day + 4 if low_day < 0: low_day += 365 if high_day > 365: high_day += -365 if low_day < high_day: i_window_days = np.where( np.logical_and(day_of_year >= low_day, day_of_year <= high_day)) else: i_window_days = np.where( np.logical_or(day_of_year >= low_day, day_of_year <= high_day)) ten_day_median = np.median(temps[i_window_days]) median_temp_calendar[i_day] = ten_day_median ten_day_medians[np.where(day_of_year == i_day)] = ten_day_median #Handle '364' years, to be handled the same as normal years, to make things easier if i_day == 364: ten_day_medians[np.where(day_of_year == 365)] = ten_day_median median_temp_calendar[365] = ten_day_median return median_temp_calendar
def build_temp_calendar(temps, year, month, day): """ Create an arrary of typical temperatures by day-of-year. Day 0 = Jan 1, etc. Parameters ---------- temps: arrary of floats year, month, day: arrary of ints Returns ------- median_temp_calendar: array of floats length 366 """ # start with an array of zero's according to the proper temp. size day_of_year = np.zeros(temps.size) for i_row in range(temps.size): day_of_year[i_row] = tools.find_day_of_year(year[i_row], month[i_row], day[i_row]) # Create 10-day medians for each day of the year #we use median instead of mean to avoid outliers skewing our calcs median_temp_calendar = np.zeros(366) ten_day_medians = np.zeros(temps.size) for i_day in range(0, 365): # create 10-day window low_day = i_day - 5 high_day = i_day + 4 if low_day < 0: low_day += 365 if high_day > 365: high_day += -365 if low_day < high_day: i_window_days = np.where( np.logical_and(day_of_year >= low_day, day_of_year <= high_day)) else: i_window_days = np.where( np.logical_or(day_of_year >= low_day, day_of_year <= high_day)) ten_day_median = np.median(temps[i_window_days]) median_temp_calendar[i_day] = ten_day_median ten_day_medians[np.where(day_of_year == i_day)] = ten_day_median #Handle '364' years, to be handled the same as normal years, to make things easier if i_day == 364: ten_day_medians[np.where(day_of_year == 365)] = ten_day_median median_temp_calendar[365] = ten_day_median #print(i_day, low_day, high_day, i_window_days[0].size) #print(ten_day_medians.size, np.unique(ten_day_medians), ten_day_medians) return median_temp_calendar
def find_seasonal_temp(self, year, month, day): """ For a given day, month, and year, find the seasonal high temperature for Fort Lauderdale Beach. Parameters ---------- year, month, day: int The date of interest Returns ------- seasonal_temp: float """ doy = tools.find_day_of_year(year, month, day) seasonal_temp = self.temp_calendar[doy] return seasonal_temp
def build_temp_calendar(self, temps, year, month, day): """ Create an array of typical temperatures by day-of-year. Day 0 = Jan 1, etc. Parameters ---------- temps: array of floats year, month, day: array of ints Returns ------- median_temp_calendar: array of floats of length 366 """ day_of_year = np.zeros(temps.size) for i_row in range(temps.size): day_of_year[i_row] = tools.find_day_of_year( year[i_row], month[i_row], day[i_row]) ## Create 10-day medians for each day of the year. median_temp_calendar = np.zeros(366) for i_day in range(0, 365): low_day = i_day - 5 high_day = i_day + 4 if low_day < 0: low_day += 365 if high_day > 365: high_day += -365 if low_day < high_day: i_window_days = np.where( np.logical_and(day_of_year >= low_day, day_of_year <= high_day)) else: i_window_days = np.where( np.logical_or(day_of_year >= low_day, day_of_year <= high_day)) ten_day_median = np.median(temps[i_window_days]) median_temp_calendar[i_day] = ten_day_median if i_day == 364: median_temp_calendar[365] = ten_day_median return median_temp_calendar
def test(): """ Run through the data history, calculating the prediction error for each day. """ # Create and initialize a new Predictor predictor = Predictor() temps, year, month, day = predictor.get_data() deseasonalized_temps = np.zeros(temps.size) doy = np.zeros(temps.size, dtype=np.int) for i, temp in enumerate(temps): seasonal_temp = predictor.find_seasonal_temp(year[i], month[i], day[i]) deseasonalized_temps[i] = temp - seasonal_temp doy[i] = tools.find_day_of_year(year[i], month[i], day[i]) # Make predictions for three days into the future. deseasonalized_predictions = np.zeros(temps.size) for i, temp in enumerate(deseasonalized_temps): deseasonalized_predictions[i] = predictor.predict_deseasonalized(temp) predictions = np.zeros(temps.size - 3) for i, temp in enumerate(deseasonalized_predictions[:-3]): predictions[i] = predictor.reseasonalize(temp, doy[i + 3]) residuals = temps[3:] - predictions print('MEA:', np.mean(np.abs(residuals))) actuals = temps[3:] # Compare predictions three days away vs. actuals for above/below 85. sensitivity = [] targets = np.arange(84, 90) for target in targets: i_warm = np.where(actuals > 85)[0] i_warm_predictions = np.where(predictions > target)[0] n_true_positives = np.intersect1d(i_warm, i_warm_predictions).size n_false_negatives = np.setdiff1d(i_warm, i_warm_predictions).size n_false_positives = np.setdiff1d(i_warm_predictions, i_warm).size n_true_negatives = (actuals.size - n_true_positives - n_false_positives - n_false_negatives) sensitivity.append(n_true_positives / (n_true_positives + n_false_positives))
def predict(self, year, month, day, past_temp): """ Parameters ---------- year, month, day: ints past_temp: float The temperature from 3 days before the date of interest. """ # Make a prediction for a single day. doy = tools.find_day_of_year(year, month, day) doy_past = doy - 3 if doy_past < 0: doy_past += 365 deseasonalized_temp = self.deseasonalize(past_temp, doy_past) # Make predictions for three days into the future. deseasonalized_prediction = self.predict_deseasonalized( deseasonalized_temp) prediction = self.reseasonalize(deseasonalized_prediction, doy) return prediction
def predict(year, month, day, temperature_calendar): """ For a given day, month, and year, predict the high temperature for Fort Lauderdale Beach. Parameters ---------- year, month, day: int The date of interest temperature_calendar: arrary of floats The typical temperature for each day of the year Jan 1 = 0,etc. Returns ------- prediction: float """ day = tools.find_day_of_year(year, month, day) # Looks up the associated temperature, and returns that as a prediction prediction = temperature_calendar[day] return prediction
def predict(year, month, day, temperature_calendar): day = tools.find_day_of_year(year, month, day) prediction = temperature_calendar[day] return prediction