米国の全米大気管理局(NOAA)は、地方の気候に関する情報を収集する何千もの気候観測所(主に米国)を運営しています。とりわけ、各ステーションは毎日最高および最低の観測温度を記録する。これらのデータは、「品質管理された局所気候データ」と呼ばれ、hereと公開されており、hereと記載されています。Python Jupyterノートブック経由の温度予測
temperatures.csv
には、そのデータセットの抜粋が含まれています。各列は、1日に1つのステーションからの温度を華氏で表したものです。 (実際には、その日のその駅で観測された最高気温です)。すべての数値は、2015年とカリフォルニアの駅からのものです。
今年のクリスマス休暇のためにヨセミテへの旅行を計画していて、12月25日に気温を予測したいと仮定します。その日の気温の予測値を予測するためにpredict_temperatureを使用します。
私はPython Jupyterノートブックを使用してこの問題に取り組んでいます。
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
この問題を解決しましょう。各日付は、年の初めからの日数に変換されます。私はバグ解決しようとした
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"))
: で[72]
Christmas_prediction = predict_temperature(days_since_year_start(12) + 25)
Christmas_prediction
をしかし、それは私にエラーを与えます。 SyntaxError:無効な構文
私は逃した何かがありますか?
これは困惑しています。 SyntaxErrorに関連付けられた行番号またはスタックトレースはありますか? –
構文エラーが発生した場合は、スタックトレース全体とコードの関連部分のスニペットを印刷してください。この無関係な情報をすべて削除してください。 –