<jupyter_start><jupyter_text>WeatherPy---- Note* Instructions have been included for each segment. You do not have to follow them exactly, but they are included to help you think through the steps.<jupyter_code># Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import requests
import time
from scipy.stats import linregress

# Import API key
from api_keys import weather_api_key

# Incorporated citipy to determine city based on latitude and longitude
from citipy import citipy

# Output File (CSV)
output_data_file = "../output_data/cities.csv"

# Range of latitudes and longitudes
lat_range = (-90, 90)
lng_range = (-180, 180)

print(weather_api_key)<jupyter_output>2cbb675030f3cb71c590f70ddd559d3d<jupyter_text>Generate Cities List<jupyter_code># List for holding lat_lngs and cities
lat_lngs = []
cities = []

# Create a set of random lat and lng combinations
lats = np.random.uniform(low=-90.000, high=90.000, size=1500)
lngs = np.random.uniform(low=-180.000, high=180.000, size=1500)
lat_lngs = zip(lats, lngs)

# Identify nearest city for each lat, lng combination
for lat_lng in lat_lngs:
    city = citipy.nearest_city(lat_lng[0], lat_lng[1]).city_name
    
    # If the city is unique, then add it to a our cities list
    if city not in cities:
        cities.append(city)

# Print the city count to confirm sufficient count
len(cities)<jupyter_output><empty_output><jupyter_text>Perform API Calls* Perform a weather check on each city using a series of successive API calls.* Include a print log of each city as it'sbeing processed (with the city number and city name).<jupyter_code>units = "imperial"

url = f"http://api.openweathermap.org/data/2.5/weather?appid={weather_api_key}&units={units}&q="

location = []
clouds = []
country = []
date = []
humidity = []
latitude = []
longitude = []
max_temp = []
windspeed = []
record_counter = 0

for city in cities:
    
    response = requests.get(url+city).json()
    try:
        clouds.append(response['clouds']['all'])
        country.append(response['sys']['country'])
        date.append(response['dt'])
        humidity.append(response['main']['humidity'])
        latitude.append(response['coord']['lat'])
        longitude.append(response['coord']['lon'])
        max_temp.append(response['main']['temp_max'])
        windspeed.append(response['wind']['speed'])
        location.append(city)
        record_counter = record_counter+1
        print(f"Processing record {record_counter} | {city}")
    except KeyError:
        print(f"{city} not found... skipping!")
print("-----------------------------------")
print(f"Query complete. {len(location)} imported!")
print("-----------------------------------")     


weather = {
    "city": location,
    "cloudiness": clouds,
    "country": country,
    "date": date,
    "humidity": humidity,
    "lat": latitude,
    "long": longitude,
    "max temp": max_temp,
    "wind speed": windspeed
}<jupyter_output>Processing record 1 | portland
Processing record 2 | ushuaia
Processing record 3 | la paz
Processing record 4 | saskylakh
illoqqortoormiut not found... skipping!
Processing record 5 | mataura
Processing record 6 | bathsheba
Processing record 7 | upernavik
Processing record 8 | khatanga
tsihombe not found... skipping!
Processing record 9 | rikitea
Processing record 10 | cape town
Processing record 11 | flin flon
Processing record 12 | punta arenas
Processing record 13 | les cayes
Processing record 14 | castro
vaitupu not found... skipping!
Processing record 15 | atuona
Processing record 16 | mar del plata
Processing record 17 | bredasdorp
Processing record 18 | airai
Processing record 19 | bambous virieux
Processing record 20 | kapaa
Processing record 21 | albany
Processing record 22 | bakchar
Processing record 23 | busselton
Processing record 24 | madison
Processing record 25 | longyearbyen
taolanaro not found... skipping!
Processing record 26 | tromso
Processing record 27 | constantin[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code>weather_df = pd.DataFrame(weather)
weather_df.head()
csv_file = "weather.csv"
export_csv = weather_df.to_csv(csv_file)<jupyter_output><empty_output><jupyter_text>Plotting the Data* Use proper labeling of the plots using plot titles (including date of analysis) and axes labels.* Save the plotted figures as .pngs.  Latitude vs. Temperature Plot<jupyter_code>plt.scatter(weather_df["lat"],weather_df["max temp"],marker = "o" ,edgecolors="black")
plt.title("latitude vs max temperature",fontsize=13)
plt.xlabel("latitude")
plt.ylabel("max temp")
plt.grid()

plt.savefig("../output_data/temp_lat.png")

plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code>plt.scatter(weather_df["lat"],weather_df["humidity"],marker = "o" ,edgecolors="black")
plt.title("latitude vs humidity",fontsize=13)
plt.xlabel("latitude")
plt.ylabel("humidity")
plt.grid()

plt.savefig("../output_data/humidity_lat.png")

plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness Plot<jupyter_code>plt.scatter(weather_df["lat"],weather_df["cloudiness"],marker = "o" ,edgecolors="black")
plt.title("latitude vs cloudiness",fontsize=13)
plt.xlabel("latitude")
plt.ylabel("cloudiness")
plt.grid()

plt.savefig("../output_data/cloudiness_lat.png")

plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed Plot<jupyter_code>plt.scatter(weather_df["lat"],weather_df["wind speed"],marker = "o" ,edgecolors="black")
plt.title("latitude vs wind speed",fontsize=13)
plt.xlabel("latitude")
plt.ylabel("wind speed")
plt.grid()

plt.savefig("../output_data/wind_lat.png")

plt.show()<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code># OPTIONAL: Create a function to create Linear Regression plots

# Create Northern and Southern Hemisphere DataFrames
n = pd.DataFrame(columns=('city', 'cloudiness', 'country', 'date', 'humidity', 'lat', 'long', 'max temp', 'wind speed'))
s = pd.DataFrame(columns=weather_df.columns)

n = n.append(weather_df.loc[weather_df['lat']>0, :])
s = s.append(weather_df.loc[weather_df['lat']<0, :])

n,s<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x_values = n['lat']
y_values = n['max temp']

plt.title("Northern Hemisphere - Max Temp vs Latitude Linear Regression")

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=10, color="Blue")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature")

plt.savefig("../output_data/north_temp_regression.png")

plt.show()<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x_values = s['lat']
y_values = s['max temp']

plt.title("Southern Hemisphere - Max Temp vs. Latitude Linear Regression")

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=10, color="Blue")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature")

plt.savefig("../output_data/south_temp_regression.png")

plt.show()<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>print(n.dtypes)
n['humidity'] = pd.to_numeric(n['humidity'], errors='coerce')
n['cloudiness'] = pd.to_numeric(n['cloudiness'], errors='coerce')

x_values = n['lat']
y_values = n['humidity']

plt.title("Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression")

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=10, color="Blue")
plt.xlabel("Latitude")
plt.ylabel("humidity")

plt.savefig("../output_data/north_humidity_regression.png")

plt.show()<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>s['humidity'] = pd.to_numeric(s['humidity'], errors='coerce')
s['cloudiness'] = pd.to_numeric(s['cloudiness'], errors='coerce')
print(s.dtypes)
x_values = s['lat']
y_values = s['humidity']

plt.title("Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression")

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=10, color="Blue")
plt.xlabel("Latitude")
plt.ylabel("humidity")

plt.savefig("../output_data/south_humidity_regression.png")

plt.show()<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x_values = n['lat']
y_values = n['cloudiness']

plt.title("Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression")

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=10, color="Blue")
plt.xlabel("Latitude")
plt.ylabel("cloudiness")

plt.savefig("../output_data/north_cloudiness_regression.png")

plt.show()<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x_values = s['lat']
y_values = s['cloudiness']

plt.title("Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression")

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=10, color="Blue")
plt.xlabel("Latitude")
plt.ylabel("cloudiness")

plt.savefig("../output_data/south_cloudiness_regression.png")

plt.show()<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x_values = n['lat']
y_values = n['wind speed']

plt.title("Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression")

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=10, color="Blue")
plt.xlabel("Latitude")
plt.ylabel("wind speed")

plt.savefig("../output_data/north_wind_regression.png")

plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14872971065631616, 'fraction_numerical': 0.014908256880733946, 'mean_word_length': 5.985212569316081, 'pattern_counts': {'":': 9, '<': 64, '<?xml version=': 0, '>': 64, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '249660', 'license': 'ADSL', 'n_tokens_mistral': 4236, 'n_tokens_neox': 3914, 'n_words': 843, 'path': 'starter_code/WeatherPy.ipynb', 'repo_name': 'KrystalWang070/python-api-challenge', 'size': '11336'}