<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 csv 
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import requests 
import time
from pprint import pprint
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)

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

# define count of pass throughs
count_one = 0
set_one = 1<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(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># set lists for the dataframe
city_two = []
cloudinesses = []
dates = []
humidities = []
lats = []
lngs = []
max_temps = []
wind_speeds = []
countries = []

# set initial count quantities for organization
count_one = 0
set_one = 1

# loops for creating dataframe columns
for city in cities:
    try:
        response = requests.get(query_url + city.replace(" ","&")).json()
        cloudinesses.append(response['clouds']['all'])
        countries.append(response['sys']['country'])
        dates.append(response['dt'])
        humidities.append(response['main']['humidity'])
        lats.append(response['coord']['lat'])
        lngs.append(response['coord']['lon'])
        max_temps.append(response['main']['temp_max'])
        wind_speeds.append(response['wind']['speed'])
        if count_one > 48:
            count_one = 1
            set_one += 1
            city_two.append(city)
        else:
            count_one += 1
            city_two.append(city)
        print(f"Processing Record {count_one} of Set {set_one} | {city}")
    except Exception:
        print("City not found. Skipping...")
print("------------------------------\nData Retrieval Complete\n------------------------------")<jupyter_output>Processing Record 1 of Set 1 | ilulissat
Processing Record 2 of Set 1 | ziniare
Processing Record 3 of Set 1 | inhambane
Processing Record 4 of Set 1 | vila velha
Processing Record 5 of Set 1 | yellowknife
Processing Record 6 of Set 1 | cuamba
Processing Record 7 of Set 1 | busselton
Processing Record 8 of Set 1 | padang
Processing Record 9 of Set 1 | east london
Processing Record 10 of Set 1 | vaini
Processing Record 11 of Set 1 | butaritari
City not found. Skipping...
Processing Record 12 of Set 1 | severodvinsk
Processing Record 13 of Set 1 | tiksi
City not found. Skipping...
Processing Record 14 of Set 1 | lebu
Processing Record 15 of Set 1 | touros
Processing Record 16 of Set 1 | mataura
Processing Record 17 of Set 1 | hermanus
Processing Record 18 of Set 1 | colares
Processing Record 19 of Set 1 | rikitea
Processing Record 20 of Set 1 | saskylakh
Processing Record 21 of Set 1 | bredasdorp
Processing Record 22 of Set 1 | cidreira
City not found. Skipping...
Processing Record 23 of[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code># create a dictionary for establishing dataframe
weather_dict = {
    "City":city_two,
    "Cloudiness":cloudinesses,
    "Country":countries,
    "Date":dates,
    "Humidity":humidities,
    "Lat":lats,
    "Lng":lngs,
    "Max Temp":max_temps,
    "Wind Speed":wind_speeds
}
#Create dataframe
weather_df = pd.DataFrame(weather_dict)
#display head
weather_df.head()
# be sure there aren't any NaN values 
weather_df.count()
#export to csv 
weather_df.to_csv(output_data_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>#Create Plot
plt.scatter(weather_df['Lat'], weather_df['Max Temp'],facecolors ='blue')
#Labels
plt.title("City Latitude vs. Max Temperature (09/25/19)")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (F)")
plt.grid()

#save plt as png
plt.savefig("figure_output/fig_latvtemp.png")
#show plot
plt.show()
# In the above plot, the correlation between a city's latitude and temperature are displayed, with latitude serving as
# the independent variable. The code that displays it utilizes the matplotlib library to display the data visually.
# The code also saves a .png file of the image to the working directory.<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code>#Create Plot
plt.scatter(weather_df['Lat'], weather_df['Humidity'],facecolors ='purple')
#Labels
plt.title("City Latitude vs. Humidity (09/25/19)")
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")
plt.grid()

#save plt as png
plt.savefig("figure_output/fig_latvhum.png")
#show plot
plt.show()
# In the above plot, the correlation between a city's latitude and humidity are displayed, with latitude serving as
# the independent variable. The code that displays it utilizes the matplotlib library to display the data visually.
# The code also saves a .png file of the image to the working directory.<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness Plot<jupyter_code>#Create Plot
plt.scatter(weather_df['Lat'], weather_df['Cloudiness'],facecolors ='green')
#Labels
plt.title("City Latitude vs. Cloudiness (09/25/19)")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")
plt.grid()

#save plt as png
plt.savefig("figure_output/latvcloud.png")
#show plot
plt.show()
# In the above plot, the correlation between a city's latitude and cloudiness are displayed, with latitude serving as
# the independent variable. The code that displays it utilizes the matplotlib library to display the data visually.
# The code also saves a .png file of the image to the working directory.<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed Plot<jupyter_code>#Create Plot
plt.scatter(weather_df['Lat'], weather_df['Wind Speed'],facecolors ='red')
#Labels
plt.title("City Latitude vs. Wind Speed (09/25/19)")
plt.xlabel("Latitude")
plt.ylabel("Wind Speed (mpg)")
plt.grid()

#save plt as png
plt.savefig("figure_output/latvwind.png")
#show plot
plt.show()
# In the above plot, the correlation between a city's latitude and wind speed are displayed, with latitude serving as
# the independent variable. The code that displays it utilizes the matplotlib library to display the data visually.
# The code also saves a .png file of the image to the working directory.<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code># Create Northern and Southern Hemisphere DataFrames
northern_df = weather_df[weather_df['Lat'] > 0]
northern_df.head()

southern_df = weather_df[weather_df['Lat'] < 0]
southern_df.head()<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x_values = northern_df['Lat']
y_values = northern_df['Max Temp']
(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,(10,20),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
print(f"The r-squared is: {rvalue}")
plt.show()<jupyter_output>The r-squared is: -0.8492315715373303<jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x_values = southern_df['Lat']
y_values = southern_df['Max Temp']
(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,(-30,40),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
print(f"The r-squared is: {rvalue}")
plt.show()
# In the two preceeding plots, the correlation between a city's latitude and temperature are displayed, 
# with latitude serving as the independent variable. This time, the analysis is split between Northern Hemisphere 
# and Southern Hemisphere. It displays a strongly negative relationship in the Northern Hemisphere, and a moderately positive relationship
# in the Southern Hemisphere. The code that displays it utilizes the matplotlib library to display the data visually.
# The code also saves a .png file of the image to the working directory.<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>x_values = northern_df['Lat']
y_values = northern_df['Humidity']
(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,(40,10),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Humidity')
print(f"The r-squared is: {rvalue}")
plt.show()<jupyter_output>The r-squared is: 0.2479217191253293<jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>x_values = southern_df['Lat']
y_values = southern_df['Humidity']
(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,(-50, 20),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Humidity')
print(f"The r-squared is: {rvalue}")
plt.show()
# In the two preceeding plots, the correlation between a city's latitude and humidity are displayed, 
# with latitude serving as the independent variable. This time, the analysis is split between Northern Hemisphere 
# and Southern Hemisphere. It displays a moderately positive relationship in the Northern Hemisphere, and a moderately positive relationship
# in the Southern Hemisphere. The code that displays it utilizes the matplotlib library to display the data visually.
# The code also saves a .png file of the image to the working directory.<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x_values = northern_df['Lat']
y_values = northern_df['Cloudiness']
(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,(40,20),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Cloudiness')
print(f"The r-squared is: {rvalue}")
plt.show()<jupyter_output>The r-squared is: 0.22565868560162172<jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x_values = southern_df['Lat']
y_values = southern_df['Cloudiness']
(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,(-30,20),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Cloudiness')
print(f"The r-squared is: {rvalue}")
plt.show()
# In the two preceeding plots, the correlation between a city's latitude and cloudiness are displayed, 
# with latitude serving as the independent variable. This time, the analysis is split between Northern Hemisphere 
# and Southern Hemisphere. It displays a weakly positive relationship in the Northern Hemisphere, and a weakly positive relationship
# in the Southern Hemisphere. The code that displays it utilizes the matplotlib library to display the data visually.
# The code also saves a .png file of the image to the working directory.<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x_values = northern_df['Lat']
y_values = northern_df['Wind Speed']
(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,(40,25),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Wind Speed')
print(f"The r-squared is: {rvalue}")
plt.show()<jupyter_output>The r-squared is: 0.2876201137126649<jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x_values = southern_df['Lat']
y_values = southern_df['Wind Speed']
(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,(-30,32),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Wind Speed')
print(f"The r-squared is: {rvalue}")
plt.show()
# In the two preceeding plots, the correlation between a city's latitude and wind speed are displayed, 
# with latitude serving as the independent variable. This time, the analysis is split between Northern Hemisphere 
# and Southern Hemisphere. It displays a weakly postive relationship in the Northern Hemisphere, and a weakly negative relationship
# in the Southern Hemisphere. The code that displays it utilizes the matplotlib library to display the data visually.
# The code also saves a .png file of the image to the working directory.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11450687308154277, 'fraction_numerical': 0.018083544641665554, 'mean_word_length': 5.468277945619335, 'pattern_counts': {'":': 9, '<': 65, '<?xml version=': 0, '>': 66, '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': '262689', 'license': 'ADSL', 'n_tokens_mistral': 5225, 'n_tokens_neox': 4787, 'n_words': 1487, 'path': 'WeatherPy_Hissey.ipynb', 'repo_name': 'MFHissey95/python-api-challenge', 'size': '14986'}