<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)<jupyter_output><empty_output><jupyter_text>Generate Cities List<jupyter_code># List for holding lat_lngs and cities
lat_lngs = []
cities = []
#adding country
country = []
# Create a set of random lat and lng combinations
lats = np.random.uniform(lat_range[0], lat_range[1], size=1500)
lngs = np.random.uniform(lng_range[0], lng_range[1], 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 up DF for response 

city_weatherDF = pd.DataFrame({'City': cities, 'Cloudiness' : "", 'Date': "", 'Humidity' : "", 'Lat' : "",
                              'Lng' : "", 'Max Temp' : "", 'Windspeed': ""})

#variables for response
record_number = 1
set_number = 1

#Setting Base URL for Call
base_url = 'http://api.openweathermap.org/data/2.5/weather?units=imperial&q=' 


#API Call 
print('''
---------------------------
Beginning Data Retrieval
---------------------------''')

for index, row in city_weatherDF.iterrows():
    #Query Url
    weather_query = base_url + row['City'] + '&APPID=' + weather_api_key
    
# #Response Call
    response = requests.get(weather_query).json()
    
    #Exception error for City Not Found .. 
    try: 
        print(f'Processing Weather Data for Record {record_number} of Set {set_number} | {response["name"]}')
        
        #Input data into the dataframe
        city_weatherDF.loc[index, 'Cloudiness'] = response['clouds']['all']
        city_weatherDF.loc[index, 'Date'] = response['dt']
        city_weatherDF.loc[index, 'Humidity'] = response['main']['humidity']
        city_weatherDF.loc[index, 'Lat'] = response['coord']['lat']
        city_weatherDF.loc[index, 'Lng'] = response['coord']['lon']
        city_weatherDF.loc[index, 'Max Temp'] = response['main']['temp_max']
        city_weatherDF.loc[index, 'Windspeed'] = response['wind']['speed']
        city_weatherDF.loc[index, 'Country'] = response['sys']['country']
        
        #Increase the record count
        record_number += 1
        
    except (KeyError, IndexError):
        print("City not found....Skipping............")
        
    #API Limit
    if record_number == 101:
        set_number += 1
        record_number = 1
        time.sleep(15)

        print('''
-----------------------------
Data Retrieval Complete
-----------------------------''')<jupyter_output>---------------------------
Beginning Data Retrieval
---------------------------
Processing Weather Data for Record 1 of Set 1 | Kruisfontein
Processing Weather Data for Record 2 of Set 1 | Bilma
Processing Weather Data for Record 3 of Set 1 | Airai
Processing Weather Data for Record 4 of Set 1 | Ajdabiya
Processing Weather Data for Record 5 of Set 1 | Bredasdorp
Processing Weather Data for Record 6 of Set 1 | Avarua
Processing Weather Data for Record 7 of Set 1 | Tiruchchendur
Processing Weather Data for Record 8 of Set 1 | Coquimbo
Processing Weather Data for Record 9 of Set 1 | Mataura
Processing Weather Data for Record 10 of Set 1 | Tarauacá
City not found....Skipping............
City not found....Skipping............
Processing Weather Data for Record 11 of Set 1 | Pemberton
Processing Weather Data for Record 12 of Set 1 | Rikitea
Processing Weather Data for Record 13 of Set 1 | Dikson
Processing Weather Data for Record 14 of Set 1 | Ternate
Processing Weather Data for Record 15 [...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code>#DF set up and clean up
city_weatherDF['Max Temp']  = city_weatherDF['Max Temp'].replace("", np.nan)
city_weatherDF = city_weatherDF.dropna()

#exporting
city_weatherDF.to_csv("../output_data/CityWeather.csv", index=False, header=True)

city_weatherDF.head()<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 over 100%
#  Get the indices of cities that have humidity over 100%.
#No Cities over 100%
# Make a new DataFrame equal to the city data to drop all humidity outliers by index.
# Passing "inplace=False" will make a copy of the city_data DataFrame, which we call "clean_city_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>plt.scatter(city_weatherDF['Lat'], city_weatherDF['Max Temp'], marker = "o")

#Plot details
plt.title(f'City Latitude vs. Max Temperature {time.strftime("%m/%d/%Y")}')
plt.ylabel("Max Temperature (F)")
plt.xlabel("Latitude")
plt.grid(True)

#saving
plt.savefig("../output_data/Lat vs. Max Temp.png")

plt.show()<jupyter_output><empty_output><jupyter_text>This is analyzing the max temperature of each city amongst the latitudes in which they are located. You would hope to find the largest temperatures near 0 latitude.  Latitude vs. Humidity Plot<jupyter_code>#Plotting
plt.scatter(city_weatherDF['Lat'], city_weatherDF['Humidity'], marker = "o")

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

#saving
plt.savefig("../output_data/Lat vs. Humidity.png")

plt.show()<jupyter_output><empty_output><jupyter_text>This is analyzing the Cities latitude against the Humidity %.  As we go north and south of the equator (0 latitude) we can see if there are any correlations with humidity %.  Latitude vs. Cloudiness Plot<jupyter_code>#Plotting
plt.scatter(city_weatherDF['Lat'], city_weatherDF['Cloudiness'], marker = "o")

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

#saving
plt.savefig("../output_data/Lat vs. Cloudiness.png")

plt.show()<jupyter_output><empty_output><jupyter_text>This is plotting the cloudiness of each city against the cities latitude.  We can see if latitude effects how cloudiness levels.  Latitude vs. Wind Speed Plot<jupyter_code>#Plotting
plt.scatter(city_weatherDF['Lat'], city_weatherDF['Windspeed'], marker = "o")

#Plot details
plt.title(f'City Latitude vs. Windspeed {time.strftime("%m/%d/%Y")}')
plt.ylabel("Windspeed (mph)")
plt.xlabel("Latitude")
plt.grid(True)

#saving
plt.savefig("../output_data/Lat vs. Windspeed.png")

plt.show()<jupyter_output><empty_output><jupyter_text>This is plotting windspeed against latitude.  We can see if windspeed would be affected based on a cities latitude.   Linear Regression<jupyter_code>#Northern Hemisphere locations
north = city_weatherDF.loc[city_weatherDF["Lat"].astype(float) >= 0.0]
north.reset_index(inplace=True)

#Southern Hemisphere Locations

south = city_weatherDF.loc[city_weatherDF["Lat"].astype(float) < 0.0]
south.reset_index(inplace=True)<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x_values = pd.to_numeric(north['Lat']).astype(float)
y_values = pd.to_numeric(north['Max Temp']).astype(float)
(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))
print(f"Regression line equation is: {line_eq}")
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.title('Northern Hemisphere - Max Temp vs. Latitude Linear Regression')
print(f"The r-squared is: {rvalue}")
print("The regression displays a negative correlation.")
print("In the nothern hemisphere, as latitude increases = temperature decreases.")
plt.savefig("../output_data/Northern Hemisphere Lat vs. Max Temp.png")
plt.show()<jupyter_output>Regression line equation is: y = -1.44x + 95.45
The r-squared is: -0.8491686218693599
The regression displays a negative correlation.
In the nothern hemisphere, as latitude increases = temperature decreases.<jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x_values = pd.to_numeric(south['Lat']).astype(float)
y_values = pd.to_numeric(south['Max Temp']).astype(float)
(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))
print(f"Regression line equation is: {line_eq}")
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.title('Southern Hemisphere - Max Temp vs. Latitude Linear Regression')
print(f"The r-squared is: {rvalue}")
print("The regression displays a positive correlation.")
print("Southern hemisphere, as latitude approaches 0 = temperature increases.")
plt.savefig("../output_data/Southern Hemisphere Lat vs. Max Temp.png")
plt.show()<jupyter_output>Regression line equation is: y = 0.44x + 85.14
The r-squared is: 0.5673007946804979
The regression displays a positive correlation.
Southern hemisphere, as latitude approaches 0 = temperature increases.<jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>x_values = pd.to_numeric(north['Lat']).astype(float)
y_values = pd.to_numeric(north['Humidity']).astype(float)
(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))
print(f"Regression line equation is: {line_eq}")
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Humidity %')
plt.title('Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression')
print(f"The r-squared is: {rvalue}")
print("The regression displays a somehwat poor positive correlation.")
print("In the nothern hemisphere, as latitude as latitiude increases there is a somewhat positive correlation with an increase in humidity ")
plt.savefig("../output_data/Northern Hemisphere Lat vs. Humidity.png")
plt.show()<jupyter_output>Regression line equation is: y = 0.42x + 55.91
The r-squared is: 0.42029894676316437
The regression displays a somehwat poor positive correlation.
In the nothern hemisphere, as latitude as latitiude increases there is a somewhat positive correlation with an increase in humidity<jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>x_values = pd.to_numeric(south['Lat']).astype(float)
y_values = pd.to_numeric(south['Humidity']).astype(float)
(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))
print(f"Regression line equation is: {line_eq}")
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.title('Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression')
print(f"The r-squared is: {rvalue}")
print("The regression is very poor.")
print("There is a very poor positive correlation with humidity as latitude approaches 0 in the Southern Hemisphere.")
plt.savefig("../output_data/Southern Hemisphere Lat vs. Humidity.png")
plt.show()<jupyter_output>Regression line equation is: y = 0.48x + 79.86
The r-squared is: 0.34684614641963485
The regression is very poor.
There is a very poor positive correlation with humidity as latitude approaches 0 in the Southern Hemisphere.<jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x_values = pd.to_numeric(north['Lat']).astype(float)
y_values = pd.to_numeric(north['Cloudiness']).astype(float)
(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))
print(f"Regression line equation is: {line_eq}")
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.title('Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression')
print(f"The r-squared is: {rvalue}")
print("The regression displays a poor correlation.")
print("In the nothern hemisphere, as latitude increases there is a poor positive correlation with an increase in cloudiness")
plt.savefig("../output_data/Northern Hemisphere Lat vs. Cloudiness.png")
plt.show()<jupyter_output>Regression line equation is: y = 0.53x + 31.08
The r-squared is: 0.26489615105125697
The regression displays a poor correlation.
In the nothern hemisphere, as latitude increases there is a poor positive correlation with an increase in cloudiness<jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x_values = pd.to_numeric(south['Lat']).astype(float)
y_values = pd.to_numeric(south['Cloudiness']).astype(float)
(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))
print(f"Regression line equation is: {line_eq}")
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(10,15),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.title('Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression')
print(f"The r-squared is: {rvalue}")
print("The regression displays a poor correlation")
print("Southern hemisphere, as latitude approaches 0 cloudiness has a poor positive correlation.")
plt.savefig("../output_data/Southern Hemisphere Lat vs. Cloudiness.png")
plt.show()<jupyter_output>Regression line equation is: y = 1.05x + 77.12
The r-squared is: 0.36957421131987417
The regression displays a poor correlation
Southern hemisphere, as latitude approaches 0 cloudiness has a poor positive correlation.<jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x_values = pd.to_numeric(north['Lat']).astype(float)
y_values = pd.to_numeric(north['Windspeed']).astype(float)
(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))
print(f"Regression line equation is: {line_eq}")
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Windspeed (mph)')
plt.title('Northern Hemisphere - Windspeed (mph) vs. Latitude Linear Regression')
print(f"The r-squared is: {rvalue}")
print("The regression displays a poor correlation.")
print("In the nothern hemisphere, there is a poor correlation with Wyuindspeed vs. Latitude")
plt.savefig("../output_data/Northern Hemisphere Lat vs. Windspeed.png")
plt.show()<jupyter_output>Regression line equation is: y = -0.0x + 8.44
The r-squared is: -0.0102445319979722
The regression displays a poor correlation.
In the nothern hemisphere, there is a poor correlation with Wyuindspeed vs. Latitude<jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x_values = pd.to_numeric(south['Lat']).astype(float)
y_values = pd.to_numeric(south['Windspeed']).astype(float)
(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))
print(f"Regression line equation is: {line_eq}")
plt.scatter(x_values,y_values)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="red")
plt.xlabel('Latitude')
plt.ylabel('Windspeed (mph)')
plt.title('Southern Hemisphere - Windspeed (mph) vs. Latitude Linear Regression')
print(f"The r-squared is: {rvalue}")
print("The regression displays a poor correlation.")
print("Southern hemisphere, there is a poor correlation with windspeed and latitude.")
plt.savefig("../output_data/Southern Hemisphere Lat vs. Windspeed.png")
plt.show()<jupyter_output>Regression line equation is: y = -0.08x + 7.0
The r-squared is: -0.22697700541902294
The regression displays a poor correlation.
Southern hemisphere, there is a poor correlation with windspeed and latitude.
{'fraction_non_alphanumeric': 0.13323252688172044, 'fraction_numerical': 0.019489247311827957, 'mean_word_length': 5.298765432098765, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 67, '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': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '202235', 'license': 'ADSL', 'n_tokens_mistral': 6357, 'n_tokens_neox': 5690, 'n_words': 1638, 'path': 'WeatherPy/WeatherPy.ipynb', 'repo_name': 'rclassit/python-api-challenge', 'size': '17856'}