[英]Python Predicting temperature via Jupyter notebook
美国国家海洋与大气管理局(NOAA)运营着数千个气候观测站(大部分在美国),这些观测站收集有关当地气候的信息。 除其他事项外,每个站点每天记录的最高和最低温度。 这些数据称为“质量控制的本地气候数据”,可在此处公开获得并在 此处进行描述。
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"))
我试图解决该错误:
Christmas_prediction = predict_temperature(days_since_year_start(12) + 25)
Christmas_prediction
但这给了我一个错误。 SyntaxError:语法无效
有什么我想念的吗?
通过在Jupyter Notebook中运行,我能够解决此问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.