簡體   English   中英

Python通過Jupyter筆記本預測溫度

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM