<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
import datetime
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 = []

# 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>city_names = []
lat = []
long = []
maxtemp = []
humidity = []
cloudiness = []
wind_speed = []
country = []
date = []
city_counter = 1
set_counter = 1

print("Beginning Data Retrieval ")
print("-----------------------------")

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

for index, city in enumerate(cities, start = 1):
    try:
        response = requests.get(query_url + city).json()
        city_names.append(response["name"])
        cloudiness.append(response["clouds"]["all"])
        country.append(response["sys"]["country"])
        date = datetime.datetime.fromtimestamp(int(response['dt'])).strftime('%m-%d-%Y')
        humidity.append(response["main"]["humidity"])
        lat.append(response["coord"]["lat"])
        long.append(response["coord"]["lon"])
        maxtemp.append(response["main"]["temp_max"])
        wind_speed.append(response["wind"]["speed"])
        
        if city_counter > 49:
            city_counter = 0
            set_counter = set_counter + 1
    
        else:
            city_counter = city_counter + 1
            
        print(f"Processing Record {city_counter} of Set {set_counter} | {city}") 
  
    except(KeyError, IndexError):
        print("City not found. Skipping...")

print("-----------------------------")
print("Data Retrieval Complete")
print("-----------------------------")<jupyter_output>Beginning Data Retrieval 
-----------------------------
Processing Record 2 of Set 1 | codrington
Processing Record 3 of Set 1 | westerland
Processing Record 4 of Set 1 | cape town
Processing Record 5 of Set 1 | kulhudhuffushi
Processing Record 6 of Set 1 | atuona
City not found. Skipping...
Processing Record 7 of Set 1 | rikitea
Processing Record 8 of Set 1 | mar del plata
Processing Record 9 of Set 1 | ushuaia
City not found. Skipping...
Processing Record 10 of Set 1 | hilo
Processing Record 11 of Set 1 | vaini
Processing Record 12 of Set 1 | kodiak
Processing Record 13 of Set 1 | saint-philippe
Processing Record 14 of Set 1 | severo-kurilsk
Processing Record 15 of Set 1 | kapaa
City not found. Skipping...
Processing Record 16 of Set 1 | yellowknife
Processing Record 17 of Set 1 | busselton
Processing Record 18 of Set 1 | cidreira
Processing Record 19 of Set 1 | slavutych
Processing Record 20 of Set 1 | bluff
Processing Record 21 of Set 1 | ribeira grande
Processing Record 22 of Set [...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code>#dataframe
city_data_df = pd.DataFrame({
    "City": city_names,
    "Lat": lat,
    "Lng": long,
    "Max Temp": maxtemp,
    "Humidity": humidity,
    "Cloudiness": cloudiness,
    "Wind Speed": wind_speed,
    "Country": country,
    "Date": date
})

city_data_df
city_data_df.count()
city_data_df.describe()
# Export the city data into a csv file
city_data_df.to_csv("../output_data/city_data.csv", index=False)<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># Drop any row which has a humidity higher than 100%
cleaned_cities_df = city_data_df.loc[city_data_df['Humidity'] <= 100]
cleaned_cities_df
#  Get the indices of cities that have humidity over 100%.
cleaned_cities_2_df = city_data_df.loc[city_data_df['Humidity'] > 100]
cleaned_cities_2_df
print("There are no cities with Humidity greater than 100. Skipping to next step.")
# 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_data_df["Lat"], city_data_df["Max Temp"])
plt.grid()
plt.xlabel("Latitude")
plt.ylabel("Temperature (°F)")
plt.title(f'Temperature (°F) vs Latitude ({date})')

plt.savefig("../images/tempvslat.png")<jupyter_output><empty_output><jupyter_text>As latitute moves towards 0 or the equator, the temperature increases. Vice versa, as you move away from 0, it is colder.   Latitude vs. Humidity Plot<jupyter_code>plt.scatter(city_data_df["Lat"], city_data_df["Humidity"])
plt.grid()
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")
plt.title(f'Humidity (%) vs Latitude ({date})')

plt.savefig("../images/humidityvslat.png")<jupyter_output><empty_output><jupyter_text>Most citites have humidity above 60%, while others with a latitude between 0-40 varies more. Citites with latitude between 40 - 80 have humidity between 80-100%.   Latitude vs. Cloudiness Plot<jupyter_code>plt.scatter(city_data_df["Lat"], city_data_df["Cloudiness"])
plt.grid()
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")
plt.title(f'Cloudiness (%) vs Latitude ({date})')

plt.savefig("../images/cloudinessvslat.png")<jupyter_output><empty_output><jupyter_text>Cities with latitude 40 - 80 are precieved to have a cloudiness around 80% - 100%. However, most of the data points are realtively distributed. Therefore, there is not much of a correlation between coludiness and latitude.  Latitude vs. Wind Speed Plot<jupyter_code>plt.scatter(city_data_df["Lat"], city_data_df["Wind Speed"])
plt.grid()
plt.xlabel("Latitude")
plt.ylabel("Wind Speed (mph)")
plt.title(f'Wind Speed (mph) vs Latitude ({date})')

plt.savefig("../images/windspeedvslat.png")<jupyter_output><empty_output><jupyter_text>A mjority of the cities have a wind speed of 0MPH to 8MPH, with some cities at a higher(positive and negative) latitude having higher wind speed.  Linear Regression<jupyter_code>northern_cities = city_data_df[city_data_df.loc[:, "Lat"] >= 0]
southern_cities = city_data_df[city_data_df.loc[:, "Lat"] < 0]
#north and south dataframes
northlat_df = city_data_df.loc[city_data_df["Lat"] >= 0,:]
southlat_df = city_data_df.loc[city_data_df["Lat"] < 0,:]<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>plt.scatter(northern_cities["Lat"], northern_cities["Max Temp"])
plt.grid()
plt.xlabel("Latitude")
plt.ylabel("Temperature (°F)")
plt.title(f'Temperature (°F) vs Latitude (Northern Hem.) ({date})')
(slope, intercept, rvalue, pvalue, stderr) = linregress(northern_cities["Lat"], northern_cities["Max Temp"])
regress_values = northern_cities["Lat"] * slope + intercept
line_equation = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_equation,(5,30), fontsize=13,color="red")
plt.plot(northern_cities["Lat"],regress_values,"r-")

plt.savefig("../images/tempvlatnorthernhem.png")<jupyter_output><empty_output><jupyter_text>In the northern hemisphere, the max temperature decreases while the city is away from 0 degree latitude. The high r value inidicates a strong positive correlation.    Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>plt.scatter(southern_cities["Lat"], southern_cities["Max Temp"])
plt.grid()
plt.xlabel("Latitude")
plt.ylabel("Temperature (°F)")
plt.title("Temperature (°F) vs Latitude (Southern Hem.)")
(slope, intercept, rvalue, pvalue, stderr) = linregress(southern_cities["Lat"], southern_cities["Max Temp"])
regress_values = southern_cities["Lat"] * slope + intercept
line_equation = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_equation,(-55,81),fontsize=12,color="red")
plt.plot(southern_cities["Lat"],regress_values,"r-")

plt.savefig("../images/tempvlatsouthernhem.png")<jupyter_output><empty_output><jupyter_text>In the southern hemisphere, the max temperature increases while the city is close to 0 degree latitude. The high r value inidicates a strong positive correlation.   Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>plt.scatter(northern_cities["Lat"], northern_cities["Humidity"])
plt.grid()
plt.xlabel("Latitude")
plt.ylabel("Humidity")
plt.title("Humidity vs Latitude (Northern Hem.)")
(slope, intercept, rvalue, pvalue, stderr) = linregress(northern_cities["Lat"], northern_cities["Humidity"])
regress_values = northern_cities["Lat"] * slope + intercept
line_equation = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_equation,(48,10),fontsize=13,color="red")
plt.plot(northern_cities["Lat"],regress_values,"r-")

plt.savefig("../images/NorthernHemHumidityvLat.png")<jupyter_output><empty_output><jupyter_text>In the northern hemisphere, the humidity increases while the city is further away from 0 degree latitude. The low r value indicates little to no relationship between humidity and latitude.   Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>plt.scatter(southern_cities["Lat"], southern_cities["Humidity"])
plt.grid()
plt.xlabel("Latitude")
plt.ylabel("Humidity")
plt.title("Humidity vs Latitude (Southern Hem.)")
(slope, intercept, rvalue, pvalue, stderr) = linregress(southern_cities["Lat"], southern_cities["Humidity"])
regress_values = southern_cities["Lat"] * slope + intercept
line_equation = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_equation,(-55,25), fontsize=13,color="red")
plt.plot(southern_cities["Lat"],regress_values,"r-")

plt.savefig("../images/SourthernHemHumidityvLat.png")<jupyter_output><empty_output><jupyter_text>In the southern hemisphere, the humidity slightly increases while the city is closer to 0 degree latitude. The low r value indicates little to no correlation between humidity and latitude.   Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>plt.scatter(northern_cities["Lat"], northern_cities["Cloudiness"])
plt.grid()
plt.xlabel("Latitude")
plt.ylabel("Cloudiness")
plt.title("Cloudiness vs Latitude (Northern Hem.)")
(slope, intercept, rvalue, pvalue, stderr) = linregress(northern_cities["Lat"], northern_cities["Cloudiness"])
regress_values = northern_cities["Lat"] * slope + intercept
line_equation = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_equation,(49,10), fontsize=13,color="red")
plt.plot(northern_cities["Lat"],regress_values,"r-")

plt.savefig("../images/NorthernHemCloudinessvLat.png")<jupyter_output><empty_output><jupyter_text>In the northern hemisphere, the cloudiness increases while the city is further away from 0 degree latitude.The low r value indicates little to no relationship between humidity and latitude.   Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>plt.scatter(southern_cities["Lat"], southern_cities["Cloudiness"])
plt.grid()
plt.xlabel("Latitude")
plt.ylabel("Cloudiness")
plt.title("Cloudiness vs Latitude (Southern Hem.)")
(slope, intercept, rvalue, pvalue, stderr) = linregress(southern_cities["Lat"], southern_cities["Cloudiness"])
regress_values = southern_cities["Lat"] * slope + intercept
line_equation = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_equation,(-55,22),fontsize=13,color="red")
plt.plot(southern_cities["Lat"], regress_values,"r-")

plt.savefig('../images/sSouthernHemCloudinessvLat.png')<jupyter_output><empty_output><jupyter_text>In the southern hemisphere, the cloudiness increases while the city is closer to 0 degree latitude. The low r value also indicates a little correlation between latitude and cloudiness.   Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>plt.scatter(northern_cities["Lat"], northern_cities["Wind Speed"])
plt.grid()
plt.xlabel("Latitude")
plt.ylabel("Wind Speed")
plt.title("Wind Speed (mph) vs Latitude (Northern Hem)")
(slope, intercept, rvalue, pvalue, stderr) = linregress(northern_cities["Lat"], northern_cities["Wind Speed"])
regress_values = northern_cities["Lat"] * slope + intercept
line_equation = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_equation,(49,22),fontsize=13,color="red")
plt.plot(northern_cities["Lat"], regress_values,"r-")

plt.savefig('../images/NorthernHemWindvLat.png')<jupyter_output><empty_output><jupyter_text>In the northern hemisphere, the wind speed does not seem to be related to whether the city is close from 0 degree latitude. The low r value also indicates little to no correlation between humidity and latitude.   Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>plt.scatter(southern_cities["Lat"], southern_cities["Wind Speed"])
plt.grid()
plt.xlabel("Latitude")
plt.ylabel("Wind Speed")
plt.title("Wind Speed (mph) vs Latitude (Southern Hem)")
(slope, intercept, rvalue, pvalue, stderr) = linregress(southern_cities["Lat"], southern_cities["Wind Speed"])
regress_values = southern_cities["Lat"] * slope + intercept
line_equation = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_equation,(-55,17),fontsize=13,color="red")
plt.plot(southern_cities["Lat"], regress_values,"r-")

plt.savefig('../images/SouthernHemWindvLat.png')<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13501483679525222, 'fraction_numerical': 0.014297275424871863, 'mean_word_length': 5.670715249662618, 'pattern_counts': {'":': 9, '<': 75, '<?xml version=': 0, '>': 78, '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': '182052', 'license': 'ADSL', 'n_tokens_mistral': 5184, 'n_tokens_neox': 4762, 'n_words': 1292, 'path': 'WeatherPy/WeatherPy.ipynb', 'repo_name': 'melorebecca/python-api-challenge', 'size': '14828'}