Attempting to use a machine learning algorithm to predict the weather using openweathermap bulk history API. I have been using https://stackabuse.com/using-machine-learning-to-predict-the-weather-part-1/ as my main resource for completing this. However it keeps giving me a the type error listed in the title specifically for line 27: temperature = data['main']['temp'] ,
from datetime import datetime
from datetime import timedelta
import time
from collections import namedtuple
import pandas as pd
import requests
import matplotlib.pyplot as plt
#Data collection and Organization
url = 'http://history.openweathermap.org//storage/d12a3df743e650ba4035d2c6d42fb68f.json'
target_date = datetime(2018, 4, 22)
features = ["date", "temperature", "pressure", "humidity", "maxtemperature", "mintemperature"]
DailySummary = namedtuple("DailySummary", features)
def extract_weather_data(url, target_date, days):
records = []
for _ in range(days):
response = requests.get(url)
if response.status_code == 200:
data = response.json()
records.append(DailySummary(
date = target_date,
temperature = data['main']['temp'],
pressure = data['main']['pressure'],
humidity = data['main']['humidity'],
maxtemperature = data['main']['temp_max'],
mintemperature = data['main']['temp_min']))
time.sleep(6)
target_date += timedelta(days =1)
return records
records = extract_weather_data(url, target_date, 365)
#Finished data collection now begin to clean and process data using Pandas
df = pd.DataFrame(records, columns=features).set_index('date')
tmp = df[['temperature','pressure','humidty', 'maxtemperature', 'mintemperature']].head(10)
def derive_nth_day_feature(df, feature, N):
rows = df.shape[0]
nth_prior_measurements = [None]*N + [df[feature][i-N] for i in range(N, rows)]
col_name = "{}_{}".format(feature, N)
df[col_name]= nth_prior_measurements
for feature in features:
if feature != 'date':
for N in range (1, 4):
derive_nth_day_feature(df, feature, N)
df.columns
The response you get is a list of dictionaries. So you should iterating the list and then fetch the temp from each item
response = requests.get(url)
data = response.json()
for d in data:
print(d['main']['temp'])
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.