<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
from pprint import pprint

# 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)<jupyter_output><empty_output><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(lat_range[0], lat_range[1], size=1750) # increased the amount due to my logic below
lngs = np.random.uniform(lng_range[0], lng_range[1], size=1750) # increased the amount due to my logic below
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># Save config information.
url = "http://api.openweathermap.org/data/2.5/weather?"
units = "imperial"

# Build partial query URL
query_url = f"{url}appid={weather_api_key}&units={units}&q="

# Set up lists to hold reponse information 
city_list = []
city_lat = []
city_lng = []
city_temp = []
city_humidity = []
city_clouds = []
city_wind = []
city_country = []
city_date = []
i = 1 
setof = 1 

# Loop through the list of cities and perform a request for data on each

print("""Beginning Data Retrieval\n-----------------------------""")
for city in cities:
    if i < 51:  
        response = requests.get(query_url + city).json() 
        try: 
            if response['name'].casefold() == city: # casefold helps us ignore case
                city_list.append(response['name'])
                city_lat.append(response['coord']['lat'])
                city_lng.append(response['coord']['lon'])
                city_temp.append(response['main']['temp_max'])
                city_humidity.append(response['main']['humidity'])
                city_clouds.append(response['clouds']['all'])
                city_wind.append(response['wind']['speed'])
                city_country.append(response['sys']['country'])
                city_date.append(response['dt'])
                print(f"Processing Record {i} of Set {setof} | {city}")
            else: 
                print("City not found. Skipping...") # filtering these out to be extra sure everything matches
        except KeyError:
            print("City not found. Skipping...") 
        i += 1
    else: 
        time.sleep(60) #https://docs.python.org/3/library/time.html#time.sleep
        setof += 1 # add one to the set 
        i = 1 # reset the ticker 
        response = requests.get(query_url + city).json()   
        try: 
            if response['name'].casefold() == city: # casefold helps us ignore case
                city_list.append(response['name'])
                city_lat.append(response['coord']['lat'])
                city_lng.append(response['coord']['lon'])
                city_temp.append(response['main']['temp_max'])
                city_humidity.append(response['main']['humidity'])
                city_clouds.append(response['clouds']['all'])
                city_wind.append(response['wind']['speed'])
                city_country.append(response['sys']['country'])
                city_date.append(response['dt'])
                print(f"Processing Record {i} of Set {setof} | {city}")
            else: 
                print("City not found. Skipping...") # filtering these out to be extra sure everything matches
        except KeyError:
            print("City not found. Skipping...") 
        i += 1
print("""-----------------------------\nData Retrieval Complete\n-----------------------------""")<jupyter_output>Beginning Data Retrieval
-----------------------------
Processing Record 1 of Set 1 | norman wells
Processing Record 2 of Set 1 | touros
City not found. Skipping...
Processing Record 4 of Set 1 | qaanaaq
City not found. Skipping...
Processing Record 6 of Set 1 | punta arenas
Processing Record 7 of Set 1 | ushuaia
Processing Record 8 of Set 1 | vaini
Processing Record 9 of Set 1 | busselton
Processing Record 10 of Set 1 | poum
Processing Record 11 of Set 1 | sheregesh
Processing Record 12 of Set 1 | mataura
Processing Record 13 of Set 1 | khatanga
Processing Record 14 of Set 1 | saint-augustin
Processing Record 15 of Set 1 | kavieng
Processing Record 16 of Set 1 | richards bay
Processing Record 17 of Set 1 | kahului
City not found. Skipping...
Processing Record 19 of Set 1 | pisco
City not found. Skipping...
Processing Record 21 of Set 1 | dalvik
Processing Record 22 of Set 1 | port hardy
Processing Record 23 of Set 1 | vostok
Processing Record 24 of Set 1 | airai
City not found. Skippi[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code># Create a data frame from cities info 
weather_dict = {
    "City": city_list,
    "Lat": city_lat,
    "Lng": city_lng,
    "Max Temp": city_temp,
    "Humidity": city_humidity,
    "Cloudiness": city_clouds,
    "Wind Speed": city_wind,
    "Country": city_country,
    "Date": city_date
}
weather_data = pd.DataFrame(weather_dict)
weather_data
# Exporting to csv file 
weather_data.to_csv(output_data_file, index = False)

# Importing file, if needed 
weather_data = pd.read_csv("../output_data/cities.csv")<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># No cities have humidity > 100%, although a few are exactly 100%
humid_weather_data = weather_data.loc[weather_data["Humidity"] >= 100]
humid_weather_data<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># Setting x and y values 
x_values = weather_data["Lat"]
y_values = weather_data["Max Temp"]

# Plot the scatter plot 
plt.scatter(x_values, y_values, marker="o", facecolors="teal", edgecolors="black")
plt.title("City Latitude vs. Max Temperature")
plt.ylabel("Max Temperature (F)", fontsize=12)
plt.xlabel("Latitude", fontsize=12)
plt.grid(alpha=0.2)
plt.show()

# Save the image 
#fig_path = os.path.abspath() # Figures out the absolute path for you in case your working directory moves around.
#fig.savefig(my_path + '/Sub Directory/graph.png')<jupyter_output><empty_output><jupyter_text>ONE-SENTENCE DESCRIPTION: The graph above is displaying the max temperature (y-axis) for all cities in the dataset (the circles) organized by latitude (x-axis). This graph suggests that cities north of the equator (>0 Lat) might have a lower max temp.   Latitude vs. Humidity Plot<jupyter_code># Setting x and y values 
x_values = weather_data["Lat"]
y_values = weather_data["Humidity"]

# Plot the scatter plot 
plt.scatter(x_values, y_values, marker="o", facecolors="teal", edgecolors="black")
plt.title("City Latitude vs. Humidity")
plt.ylabel("Humidity (%)", fontsize=12)
plt.xlabel("Latitude", fontsize=12)
plt.grid(alpha=0.2)
plt.show()<jupyter_output><empty_output><jupyter_text>ONE-SENTENCE DESCRIPTION: The graph above is displaying the recent humidity (%)(y-axis) for all cities in the dataset (the circles) organized by latitude (x-axis). This graph does not have an immediately discernible trend, other than cities tending to cluster at or greater than 60% humidity.   Latitude vs. Cloudiness Plot<jupyter_code># Setting x and y values 
x_values = weather_data["Lat"]
y_values = weather_data["Cloudiness"]

# Plot the scatter plot 
plt.scatter(x_values, y_values, marker="o", facecolors="teal", edgecolors="black")
plt.title("City Latitude vs. Cloudiness")
plt.ylabel("Cloudiness (%)", fontsize=12)
plt.xlabel("Latitude", fontsize=12)
plt.grid(alpha=0.2)
plt.show()<jupyter_output><empty_output><jupyter_text>ONE-SENTENCE DESCRIPTION:The graph above is displaying the recent cloudiness (%)(y-axis) for all cities in the dataset (the circles) organized by latitude (x-axis). This graph also does not have an immediately discernible trend.  Latitude vs. Wind Speed Plot<jupyter_code># Setting x and y values 
x_values = weather_data["Lat"]
y_values = weather_data["Wind Speed"]

# Plot the scatter plot 
plt.scatter(x_values, y_values, marker="o", facecolors="teal", edgecolors="black")
plt.title("City Latitude vs. Wind Speed")
plt.ylabel("Wind Speed (mph)", fontsize=12)
plt.xlabel("Latitude", fontsize=12)
plt.grid(alpha=0.2)
plt.show()<jupyter_output><empty_output><jupyter_text>ONE-SENTENCE DESCRIPTION:The graph above is displaying the recent wind speed (mph)(y-axis) for all cities in the dataset (the circles) organized by latitude (x-axis). Perhaps one observable trend from this graph: it appears that there is greater range in the wind speeds of cities north of equator (>Lat 0).    Linear Regression After each pair of plots, take the time to explain what the linear regression is modeling. For example, describe any relationships you notice and any other analysis you may have.Your final notebook must:* Randomly select **at least** 500 unique (non-repeat) cities based on latitude and longitude.* Perform a weather check on each of the cities using a series of successive API calls.* Include a print log of each city as it's being processed with the city number and city name.* Save a CSV of all retrieved data and a PNG image for each scatter plot.<jupyter_code># Making two separate dfs for north and south hemisphere 
north_hemi = weather_data.loc[weather_data["Lat"] >= 0]
south_hemi = weather_data.loc[weather_data["Lat"] < 0]<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code># Setting x and y values 
x_values = north_hemi["Lat"]
y_values = north_hemi["Max Temp"]

# 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.plot(x_values, regress_values, "r-")
plt.annotate(line_eq, (0,-10), fontsize=15, color="red")
print(f"The r-value is: {rvalue}")

# Plot the scatter plot 
plt.scatter(x_values, y_values, marker="o", facecolors="aqua", edgecolors="black")
plt.title("Northern Hemisphere: Max Temp vs. Latitude")
plt.ylabel("Max Temp (F)", fontsize=12)
plt.xlabel("Latitude", fontsize=12)
plt.grid(alpha=0.2)
plt.show()<jupyter_output>The r-value is: -0.8765697068804407<jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code># Setting x and y values 
x_values = south_hemi["Lat"]
y_values = south_hemi["Max Temp"]

# 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.plot(x_values, regress_values, "r-")
plt.annotate(line_eq, (-30,50), fontsize=15, color="red")
print(f"The r-value is: {rvalue}")

# Plot the scatter plot 
plt.scatter(x_values, y_values, marker="o", facecolors="aqua", edgecolors="black")
plt.title("Southern Hemisphere: Max Temp vs. Latitude")
plt.ylabel("Max Temp (F)", fontsize=12)
plt.xlabel("Latitude", fontsize=12)
plt.grid(alpha=0.2)
plt.show()<jupyter_output>The r-value is: 0.44590906959636567<jupyter_text>ANALYSIS OF NORTH/SOUTH-HEMI MAX TEMP:The graphs above are displaying the max temperature (y-axis) of cities in the dataset arranged by latitude (x-axis) and divided into two groups--the southern hemisphere and the northern hemisphere. What we see from both of these graphs is that max temperatures are higher for cities closer to the equator. This relationship is weakly positively correlated for the southern hemisphere (moving towards the equator), and strongly negatively correlated for the northern hemisphere (moving away from the equator). It is plausible that the seasons may be playing a role, as the northern hemisphere is currently experiencing winter.     Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code># Setting x and y values 
x_values = north_hemi["Lat"]
y_values = north_hemi["Humidity"]

# 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.plot(x_values, regress_values, "r-")
plt.annotate(line_eq, (40,20), fontsize=15, color="red")
print(f"The r-value is: {rvalue}")

# Plot the scatter plot 
plt.scatter(x_values, y_values, marker="o", facecolors="aqua", edgecolors="black")
plt.title("Northern Hemisphere: Humidity vs. Latitude")
plt.ylabel("Humidity (%)", fontsize=12)
plt.xlabel("Latitude", fontsize=12)
plt.grid(alpha=0.2)
plt.show()<jupyter_output>The r-value is: 0.30945504958053144<jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code># Setting x and y values 
x_values = south_hemi["Lat"]
y_values = south_hemi["Humidity"]

# 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.plot(x_values, regress_values, "r-")
plt.annotate(line_eq, (-30,20), fontsize=15, color="red")
print(f"The r-value is: {rvalue}")

# Plot the scatter plot 
plt.scatter(x_values, y_values, marker="o", facecolors="aqua", edgecolors="black")
plt.title("Southern Hemisphere: Humidity vs. Latitude")
plt.ylabel("Humidity (%)", fontsize=12)
plt.xlabel("Latitude", fontsize=12)
plt.grid(alpha=0.2)
plt.show()<jupyter_output>The r-value is: 0.22520675986668628<jupyter_text>ANALYSIS OF NORTH/SOUTH-HEMI HUMIDITY:The graphs above are displaying the humidity (y-axis) of cities in the dataset arranged by latitude (x-axis) and divided into two groups--the southern hemisphere and the northern hemisphere. These graphs reflect a weak positive correlation between humidity and latitude. Rather than being centered around the equator as shown in the previous section, the humidity seems to increase the more north a city is located. As speculated on previously, the seasons may be playing a role, however it is difficult to produce a logical explanation as colder air tends to hold less humidity.    Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code># Setting x and y values 
x_values = north_hemi["Lat"]
y_values = north_hemi["Cloudiness"]

# 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.plot(x_values, regress_values, "r-")
plt.annotate(line_eq, (45,15), fontsize=15, color="red")
print(f"The r-value is: {rvalue}")

# Plot the scatter plot 
plt.scatter(x_values, y_values, marker="o", facecolors="aqua", edgecolors="black")
plt.title("Northern Hemisphere: Cloudiness vs. Latitude")
plt.ylabel("Cloudiness (%)", fontsize=12)
plt.xlabel("Latitude", fontsize=12)
plt.grid(alpha=0.2)
plt.show()<jupyter_output>The r-value is: 0.2989895417151042<jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code># Setting x and y values 
x_values = south_hemi["Lat"]
y_values = south_hemi["Cloudiness"]

# 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.plot(x_values, regress_values, "r-")
plt.annotate(line_eq, (-50,30), fontsize=15, color="red")
print(f"The r-value is: {rvalue}")

# Plot the scatter plot 
plt.scatter(x_values, y_values, marker="o", facecolors="aqua", edgecolors="black")
plt.title("Southern Hemisphere: Cloudiness vs. Latitude")
plt.ylabel("Cloudiness (%)", fontsize=12)
plt.xlabel("Latitude", fontsize=12)
plt.grid(alpha=0.2)
plt.show()<jupyter_output>The r-value is: 0.23659107153505674<jupyter_text>ANALYSIS OF NORTH/SOUTH-HEMI CLOUDINESS:The graphs above are displaying the cloudiness (y-axis) of cities in the dataset arranged by latitude (x-axis) and divided into two groups--the southern hemisphere and the northern hemisphere. These graphs also reflect a very weak positive correlation, if any, between cloudiness and latitude. Cloudiness seems to increase the more north a city is located.   Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code># Setting x and y values 
x_values = north_hemi["Lat"]
y_values = north_hemi["Wind Speed"]

# 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.plot(x_values, regress_values, "r-")
plt.annotate(line_eq, (10,40), fontsize=15, color="red")
print(f"The r-value is: {rvalue}")

# Plot the scatter plot 
plt.scatter(x_values, y_values, marker="o", facecolors="aqua", edgecolors="black")
plt.title("Northern Hemisphere: Wind Speed vs. Latitude")
plt.ylabel("Wind Speed (mph)", fontsize=12)
plt.xlabel("Latitude", fontsize=12)
plt.grid(alpha=0.2)
plt.show()<jupyter_output>The r-value is: 0.04083196500915729<jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code># Setting x and y values 
x_values = south_hemi["Lat"]
y_values = south_hemi["Wind Speed"]

# 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.plot(x_values, regress_values, "r-")
plt.annotate(line_eq, (-55,20), fontsize=15, color="red")
print(f"The r-value is: {rvalue}")

# Plot the scatter plot 
plt.scatter(x_values, y_values, marker="o", facecolors="aqua", edgecolors="black")
plt.title("Southern Hemisphere: Wind Speed vs. Latitude")
plt.ylabel("Wind Speed (mph)", fontsize=12)
plt.xlabel("Latitude", fontsize=12)
plt.grid(alpha=0.2)
plt.show()<jupyter_output>The r-value is: -0.1644284507948641
{'fraction_non_alphanumeric': 0.12262964903090623, 'fraction_numerical': 0.020429544264012573, 'mean_word_length': 4.707324364723468, 'pattern_counts': {'":': 9, '<': 66, '<?xml version=': 0, '>': 71, 'https://': 1, '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': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '667803', 'license': 'ADSL', 'n_tokens_mistral': 6601, 'n_tokens_neox': 6040, 'n_words': 1913, 'path': 'WeatherPy/WeatherPy.ipynb', 'repo_name': 'Kylee-Grant/python-api-challenge', 'size': '19090'}