<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
import matplotlib.pyplot as plt
import requests
import numpy as np
import pandas as pd
from scipy import stats
from scipy.stats import linregress

# Import API key (Remember to change API keys in api_keys.py)
from api_keys import weather_api_key

# Import citipy
from citipy import citipy

# Import time to account for API call limit
import time<jupyter_output><empty_output><jupyter_text>Generate Cities List<jupyter_code># Creating empty lists for cities, contries and their latitudes and longitudes
lat_lng_ls = []
cities_ls = []
countries_ls = []

# Using np.random to generate random latitude and longitude pairs along the full range
latitude = np.random.uniform(low = -90.0000, high = 90.0000, size = 1400)
longitude = np.random.uniform(low = -180.0000, high = 180.0000, size = 1400)

# Populating lat_lng list with generated latitudes and longitudes
lat_lng_ls = zip(latitude, longitude)

# Using citypy to look for nearest city with latitude and longitude
for lat_lng in lat_lng_ls:
    city_name = citipy.nearest_city(lat_lng[0], lat_lng[1]).city_name
    country_name = citipy.nearest_city(lat_lng[0], lat_lng[1]).country_code
    # for cities not already in cities, append cities_ls and countries_ls
    if city_name not in cities_ls:
        cities_ls.append(city_name)
        countries_ls.append(country_name.upper())

print(f'{len(cities_ls)} unique cities have been selected.')<jupyter_output>592 unique cities have been selected.<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># URL information
url = "http://api.openweathermap.org/data/2.5/weather?"
units = "metric"

# Building partial URL query
query_url = f"{url}appid={weather_api_key}&units={units}&q="
# Set up lists to hold response information
city_name = []
country = []
lat = []
lng = []
date = []
cloudiness = []
humidity = []
max_temp = []
wind_speed = []

# Set initial count for Record and Set number
record_count = 0
set_count = 1

# Begin data retrieval
print(f"Beginning Data Retrieval")
print(f"-------------------------------")

# Loop through each city in cities_ls
for city in cities_ls:
    
    response = requests.get(query_url + city).json()
    record_count = record_count + 1
    # Try to find city in API, if it is found, append information to lists
    try:
        # Log print of processing
        print(f"Processing Record {record_count} of Set {set_count} | {response['name']}")
        
        # Append lists with information requested
        city_name.append(response["name"])
        country.append(response["sys"]["country"])
        lat.append(response['coord']['lat'])
        lng.append(response['coord']['lon'])
        date.append(response['dt'])
        cloudiness.append(response['clouds']['all'])
        humidity.append(response['main']['humidity'])
        max_temp.append(response['main']['temp_max'])
        wind_speed.append(response['wind']['speed'])
        
        # Keep track of record count
#         record_count = record_count + 1
    
    # If city is not found, print not found
    except:
        print("City not found. Skipping...")
        
    # Handling API call limits
    if record_count == 55:
        print("Please wait 10 seconds to allow for API call limit")
        set_count = set_count + 1
        record_count = 0
        time.sleep(10)

print("-----------------------------")
print("Data Retrieval Complete")      
print("-----------------------------")<jupyter_output>Beginning Data Retrieval
-------------------------------
Processing Record 1 of Set 1 | Hobart
Processing Record 2 of Set 1 | Saint-Philippe
Processing Record 3 of Set 1 | Gejiu
Processing Record 4 of Set 1 | <NAME>
Processing Record 5 of Set 1 | Yurgamysh
Processing Record 6 of Set 1 | Lüderitz
Processing Record 7 of Set 1 | Mataura
Processing Record 8 of Set 1 | Bluff
Processing Record 9 of Set 1 | <NAME>
Processing Record 10 of Set 1 | <NAME>
Processing Record 11 of Set 1 | Vaini
Processing Record 12 of Set 1 | Kumul
Processing Record 13 of Set 1 | Tuttlingen
Processing Record 14 of Set 1 | Nome
Processing Record 15 of Set 1 | Albany
Processing Record 16 of Set 1 | Egvekinot
Processing Record 17 of Set 1 | Tasiilaq
Processing Record 18 of Set 1 | <NAME>
Processing Record 19 of Set 1 | Kapaa
Processing Record 20 of Set 1 | Kloulklubed
Processing Record 21 of Set 1 | Rikitea
Processing Record 22 of Set 1 | Gizo Government Station
Processing Record 23 of S[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code># Create Weather Data DataFrame
weather_data = pd.DataFrame({"City": city_name,
                           "Country": country,
                           "Latitude": lat,
                           "Longitude": lng,
                           "Date": date,
                           "Cloudiness (%)": cloudiness,
                           "Humidity (%)": humidity,
                           "Max Temperature (°C)": max_temp,
                           "Wind Speed (m/s)": wind_speed
                            })

# Save DataFrame as CSV
weather_data.to_csv("../output_data/weather_data.csv", index=False, header=True)

# Display DataFrame
weather_data<jupyter_output><empty_output><jupyter_text>Inspect the data and remove the cities where the humidity > 100%.----Skip this step if there are no cities that have humidity > 100%.<jupyter_code>weather_data.loc[weather_data["Humidity (%)"] > 100]
#  Get the indices of cities that have humidity over 100%.
print("There are no cities where humidity > 100%")<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># Build scatter plot
plt.scatter(weather_data["Latitude"], weather_data["Max Temperature (°C)"], marker="o")

# Add graph properties
plt.title(f'City Latitude vs. Max Temperature {time.strftime("%d/%m/%Y")}')
plt.ylabel("Temperature (°C)")
plt.xlabel("Latitude")
plt.grid(True)

# Save the figure
plt.savefig("../output_data/city_latitude_vs_max_temperature.png")

# Show plot
plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code># Build scatter plot
plt.scatter(weather_data["Latitude"], weather_data["Humidity (%)"], marker="o")

# Add graph properties
plt.title(f'City Latitude vs. Humidity {time.strftime("%d/%m/%Y")}')
plt.ylabel("Humidity (%)")
plt.xlabel("Latitude")
plt.grid(True)

# Save the figure
plt.savefig("../output_data/city_latitude_vs_humidity.png")

# Show plot
plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness Plot<jupyter_code># Build scatter plot
plt.scatter(weather_data["Latitude"], weather_data["Cloudiness (%)"], marker="o")

# Add graph properties
plt.title(f'City Latitude vs. Cloudiness {time.strftime("%d/%m/%Y")}')
plt.ylabel("Cloudiness (%)")
plt.xlabel("Latitude")
plt.grid(True)

# Save the figure
plt.savefig("../output_data/city_latitude_vs_cloudiness.png")

# Show plot
plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed Plot<jupyter_code># Build scatter plot
plt.scatter(weather_data["Latitude"], weather_data["Wind Speed (m/s)"], marker="o")

# Incorporate the other graph properties
plt.title(f'City Latitude vs. Wind Speed {time.strftime("%d/%m/%Y")}')
plt.ylabel("Wind Speed (m/s)")
plt.xlabel("Latitude")
plt.grid(True)

# Save the figure
plt.savefig("../output_data/city_latitude_vs_wind_speed.png")

# Show plot
plt.show()<jupyter_output><empty_output><jupyter_text>Linear Regression   Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code># Add the linear regression equation and line to plot
x_values = weather_data["Latitude"].loc[weather_data["Latitude"] >= 0]
y_values = weather_data["Max Temperature (°C)"].loc[weather_data["Latitude"] >= 0]
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
print(f'The r-value is: {rvalue}')
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,(1,1),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (°C)")
plt.show()<jupyter_output>The r-value is: -0.7563558291549849<jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code># Add the linear regression equation and line to plot
x_values = weather_data["Latitude"].loc[weather_data["Latitude"] < 0]
y_values = weather_data["Max Temperature (°C)"].loc[weather_data["Latitude"] < 0]
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
print(f'The r-value is: {rvalue}')
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,(-55,33),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (°C)")
plt.show()<jupyter_output>The r-value is: 0.7804009383933656<jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code># Add the linear regression equation and line to plot
x_values = weather_data["Latitude"].loc[weather_data["Latitude"] >= 0]
y_values = weather_data["Humidity (%)"].loc[weather_data["Latitude"] >= 0]
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
print(f'The r-value is: {rvalue}')
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,(45,1),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")
plt.show()<jupyter_output>The r-value is: 0.06192120612138706<jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code># Add the linear regression equation and line to plot
x_values = weather_data["Latitude"].loc[weather_data["Latitude"] < 0]
y_values = weather_data["Humidity (%)"].loc[weather_data["Latitude"] < 0]
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
print(f'The r-value is: {rvalue}')
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,(-55,14),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")
plt.show()<jupyter_output>The r-value is: -0.03665019936295573<jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code># Add the linear regression equation and line to plot
x_values = weather_data["Latitude"].loc[weather_data["Latitude"] >= 0]
y_values = weather_data["Cloudiness (%)"].loc[weather_data["Latitude"] >= 0]
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
print(f'The r-value is: {rvalue}')
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,(1,1),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")
plt.show()<jupyter_output>The r-value is: -0.07082373590957451<jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code># Add the linear regression equation and line to plot
x_values = weather_data["Latitude"].loc[weather_data["Latitude"] < 0]
y_values = weather_data["Cloudiness (%)"].loc[weather_data["Latitude"] < 0]
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
print(f'The r-value is: {rvalue}')
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,(-55,10),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")
plt.show()<jupyter_output>The r-value is: 0.04651572793064384<jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code># Add the linear regression equation and line to plot
x_values = weather_data["Latitude"].loc[weather_data["Latitude"] >= 0]
y_values = weather_data["Wind Speed (m/s)"].loc[weather_data["Latitude"] >= 0]
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
print(f'The r-value is: {rvalue}')
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,(0,11),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Wind Speed (m/s)")
plt.show()<jupyter_output>The r-value is: -0.015444666035325764<jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code># Add the linear regression equation and line to plot
x_values = weather_data["Latitude"].loc[weather_data["Latitude"] < 0]
y_values = weather_data["Wind Speed (m/s)"].loc[weather_data["Latitude"] < 0]
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
print(f'The r-value is: {rvalue}')
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,(-55,11),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Wind Speed (m/s)")
plt.show()<jupyter_output>The r-value is: -0.025561562958072086
{'fraction_non_alphanumeric': 0.1409919818349535, 'fraction_numerical': 0.024622152841836373, 'mean_word_length': 4.902010050251256, 'pattern_counts': {'":': 9, '<': 71, '<?xml version=': 0, '>': 75, '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': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '390548', 'license': 'ADSL', 'n_tokens_mistral': 5162, 'n_tokens_neox': 4698, 'n_words': 1195, 'path': 'starter_code/WeatherPy.ipynb', 'repo_name': 'KenyanBoy/Python-API-Challenge', 'size': '14123'}