首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python通过Jupyter notebook预测温度

Python通过Jupyter notebook预测温度
EN

Stack Overflow用户
提问于 2016-12-09 07:50:15
回答 1查看 883关注 0票数 0

美国国家海洋和大气管理局(NOAA)运营着数千个气候观测站(大部分在美国),收集有关当地气候的信息。除此之外,每个监测站每天都会记录观测到的最高和最低温度。这些数据被称为“质量控制的当地气候数据”,是公开提供的here和描述的here

temperatures.csv包含该数据集的摘录。每一行表示某一天某站的华氏温度读数。(温度实际上是当天在该监测站观测到的最高温度。)。所有的读数都来自2015年和加利福尼亚州的监测站。

假设你今年计划去优胜美地度圣诞假,你想预测12月25日的气温。使用predict_temperature计算当天温度读数的预测值。

我正在使用Python Jupyter Notebook解决这个问题。

代码语言:javascript
复制
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年

代码语言:javascript
复制
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:

代码语言:javascript
复制
Christmas_prediction = predict_temperature(days_since_year_start(12) + 25) 
Christmas_prediction

但它给了我一个错误。SyntaxError:无效语法

我是不是漏掉了什么?

EN

回答 1

Stack Overflow用户

发布于 2017-10-01 12:23:28

我能够通过在Jupyter notebook中运行来解决这个问题。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41050877

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档