美国国家海洋和大气管理局(NOAA)运营着数千个气候观测站(大部分在美国),收集有关当地气候的信息。除此之外,每个监测站每天都会记录观测到的最高和最低温度。这些数据被称为“质量控制的当地气候数据”,是公开提供的here和描述的here。
temperatures.csv包含该数据集的摘录。每一行表示某一天某站的华氏温度读数。(温度实际上是当天在该监测站观测到的最高温度。)。所有的读数都来自2015年和加利福尼亚州的监测站。
假设你今年计划去优胜美地度圣诞假,你想预测12月25日的气温。使用predict_temperature计算当天温度读数的预测值。
我正在使用Python Jupyter Notebook解决这个问题。
import numpy as np
from datascience import *
# These lines do some fancy plotting magic.
import matplotlib
%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('fivethirtyeight')
import warnings
warnings.simplefilter('ignore', FutureWarning)
PREDICTION_RADIUS = 7让我们来解决这个问题。我们将每个日期转换为年初以来的天数。在72年
def get_month(date):
"""The month in the year for a given date.
>>> get_month(315)
3
"""
return int(date / 100)
def get_day_in_month(date):
"""The day in the month for a given date.
>>> get_day_in_month(315)
15
"""
return date % 100
DAYS_IN_MONTHS = Table().with_columns(
"Month", np.arange(1, 12+1),
"Days in Month", make_array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31))
# A table with one row for each month. For each month, we have
# the number of the month (e.g. 3 for March), the number of
# days in that month in 2015 (e.g. 31 for March), and the
# number of days in the year before the first day of that month
# (e.g. 0 for January or 59 for March).
DAYS_SINCE_YEAR_START = DAYS_IN_MONTHS.with_column(
"Days since start of year", np.cumsum(DAYS_IN_MONTHS.column("Days in Month")) - DAYS_IN_MONTHS.column("Days in Month"))
def days_since_year_start(month):
"""The number of days in the year before this month starts.
month should be the number of a month, like 3 for March.
>>> days_since_year_start(3)
59
"""
return DAYS_SINCE_YEAR_START.where("Month", are.equal_to(month))\
.column("Days since start of year")\
.item(0)
# First, extract the month and day for each reading.
with_month_and_day = temperatures.with_columns(
"Month", temperatures.apply(get_month, "Date"),
"Day in month", temperatures.apply(get_day_in_month, "Date"))
# Compute the days-since-year-start for each month and day.
fixed_dates = with_month_and_day.apply(days_since_year_start, "Month") + with_month_and_day.column("Day in month")
# Add those to the table.
with_dates_fixed = with_month_and_day.with_column("Days since start of year", fixed_dates).drop("Month", "Day in month")
with_dates_fixed
def predict_temperature(day):
"""A prediction of the temperature (in Fahrenheit) on a given day at some station.
"""
nearby_readings = with_dates_fixed.where("Days since start of year", are.between_or_equal_to(day - PREDICTION_RADIUS, day + PREDICTION_RADIUS))
return np.average(nearby_readings.column("Temperature"))我试着解决这个bug:
Christmas_prediction = predict_temperature(days_since_year_start(12) + 25)
Christmas_prediction但它给了我一个错误。SyntaxError:无效语法
我是不是漏掉了什么?
发布于 2017-10-01 12:23:28
我能够通过在Jupyter notebook中运行来解决这个问题。
https://stackoverflow.com/questions/41050877
复制相似问题