<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>!pip3 install citipy
# Dependencies and Setup
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import requests
import time
import json
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>#url = "http://api.openweathermap.org/data/2.5/forecast?id=524901&appid=" + weather_api_key

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

weather_response = requests.get(query_url + city)


weather_json = weather_response.json()
print(json.dumps(weather_json, indent=4))


for i, city in enumerate (cities):
    if (i<10):
        print(f"{i} {city}")
        print(query_url + city)
        
    
    
city_name = []
country = []
date = []
latitude = []
longitude = []
max_temperature = []
humidity = []
cloudiness = []
wind_speed = []

# Processing Record Counter Starting a 1
counter = 1

# Print Starting Log Statement
print(f"Data Retrieval Start")

# Loop Through List of Cities & Perform a Request for Data on Each
for city in cities:

# Exception Handling
    try:
        response = requests.get(query_url + city).json()
        city_name.append(response["name"])
        country.append(response["sys"]["country"])
        date.append(response["dt"])
        latitude.append(response["coord"]["lat"])
        longitude.append(response["coord"]["lon"])
        max_temperature.append(response["main"]["temp_max"])
        humidity.append(response["main"]["humidity"])
        cloudiness.append(response["clouds"]["all"])
        wind_speed.append(response["wind"]["speed"])
        city_record = response["name"]
        print(f"Record {counter} | {city_record}")
        
        # Increase Counter by 1 For Each Loop
        counter += 1
        
    except:
        print("City not found. Skip") 
    continue

# Print Ending Log Statement
print(f"-------------------------------")
print(f"Data Retrieval Complete")

weather_dict = {
    "City": city_name,
    "Country": country,
    "Date": date,
    "Latitude": latitude,
    "Longitude": longitude,
    "Max Temperature": max_temperature,
    "Humidity": humidity,
    "Cloudiness": cloudiness,
    "Wind Speed": wind_speed
}

weather_df = pd.DataFrame(weather_dict)
weather_df<jupyter_output><empty_output><jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code>weather_csv = weather_df.to_csv("./weather_csv")
weather_df<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_df["Humidity"].describe(include = "all")

#  Get the indices of cities that have humidity 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(weather_df["Latitude"], weather_df["Max Temperature"], facecolors="blue", marker="o", edgecolor="black")

plt.title("City Latitude vs. Max Temperature (04/01/2020)")
plt.ylabel("Max Temperature (F)")
plt.xlabel("Latitude")
plt.grid(True)

plt.savefig("./lat_vs_temp.png")

plt.show()

#There appears to be a negative correlation between Latitude and Max temperature. As lattitude increases increases, temperature falls.<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code>plt.scatter(weather_df["Latitude"], weather_df["Humidity"], facecolors="steelblue", marker="o", edgecolor="black")

plt.title("City Latitude vs. Humidity (%)")
plt.ylabel("Humidity (%)")
plt.xlabel("Latitude")
plt.grid(True)

plt.savefig("./lat_vs_humidity.png")

plt.show()

#There appears to be a somewhat positive correlation between latitude and humidity. As latitude increase, humidity appears to increase.<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness Plot<jupyter_code>plt.scatter(weather_df["Latitude"], weather_df["Cloudiness"], facecolors="blue", marker="o", edgecolor="black")

plt.title("City Latitude vs. Cloudiness (04/01/2020)")
plt.ylabel("Cloudiness(%)")
plt.xlabel("Latitude")
plt.grid(True)

plt.savefig("./lat_vs_cloudiness.png")

plt.show()

#There does not appear to be a correlation between latitude vs cloudiness. However, given how clustered the points are in certain cloudiness levels, cloudiness may be correlated to some other factor, such as altitude.<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed Plot<jupyter_code>plt.scatter(weather_df["Latitude"], weather_df["Wind Speed"], facecolors="steelblue", marker="o", edgecolor="black")

plt.title("City Latitude vs. Wind Speed (04/01/2020)")
plt.ylabel("Wind Speed (mph)")
plt.xlabel("Latitude")
plt.grid(True)

plt.savefig("./lat_vs_windspeed.png")

plt.show()

#Overall, latitude wind speed appears to stay below 15 mph across all latitude levels with some outliers.<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code>northdf = weather_df.loc[weather_df['Latitude'] >= 0,:]
northdf.head()
southdf = weather_df.loc[weather_df['Latitude'] < 0,:]
southdf.head()<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>(slope, intercept, rvalue, pvalue, stderr) = linregress(northdf["Latitude"], northdf["Max Temperature"])
print(f'The r values is {rvalue}')
plt.scatter(northdf["Latitude"], northdf["Max Temperature"])
plt.title("Northern Hemisphere - Max Temp vs. Latitude Linear Regression")
plt.xlabel("Latitude")
plt.ylabel("Max Temp")
regress_values = northdf["Latitude"] * slope + intercept
line_eq = "y =" + str(round(slope, 2)) + "x +" + str(round(intercept, 2))
plt.plot(northdf["Latitude"], regress_values, "r-")
plt.annotate(line_eq,(5,-50), fontsize= 16, color = 'red')
plt.savefig("./Nothern Max Temp vs. Latitude Linear Regression.png")

# There appears to be a strong negative correlation between latitude and Max Temp in the nothern hempisphere.<jupyter_output>The r values is -0.8742369919675456<jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>(slope, intercept, rvalue, pvalue, stderr) = linregress(southdf["Latitude"], southdf["Max Temperature"])
print(f'The r values is {rvalue}')
plt.scatter(southdf["Latitude"], southdf["Max Temperature"])
plt.title("Northern Hemisphere - Max Temp vs. Latitude Linear Regression")
plt.xlabel("Latitude")
plt.ylabel("Max Temp")
regress_values = southdf["Latitude"] * slope + intercept
line_eq = "y =" + str(round(slope, 2)) + "x +" + str(round(intercept, 2))
plt.plot(southdf["Latitude"], regress_values, "r-")
plt.annotate(line_eq,(-30,50), fontsize= 16, color = 'red')

plt.savefig("./Southern Max Temp vs. Latitude Linear Regression.png")

#There appears to be a strong positive correlation between max temp and latitude in the Northern Hemisphere.<jupyter_output>The r values is 0.6602524516879907<jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>(slope, intercept, rvalue, pvalue, stderr) = linregress(northdf["Latitude"], northdf["Humidity"])
print(f'The r values is {rvalue}')
plt.scatter(northdf["Latitude"], northdf["Humidity"])
plt.title("Northern Hemisphere -  Humidity vs. Latitude Linear Regression")
plt.xlabel("Latitude")
plt.ylabel("Humidity")
regress_values = northdf["Latitude"] * slope + intercept
line_eq = "y =" + str(round(slope, 2)) + "x +" + str(round(intercept, 2))
plt.plot(northdf["Latitude"], regress_values, "r-")
plt.annotate(line_eq,(40,40), fontsize= 16, color = 'red')

plt.savefig("./Northern Humidity vs. Latitude Linear Regression.png")

#There appears to be a positive correlation between humidity and latitude in the northern hemisphere. However, not as strong of a correlationa as max temp - we may need to take into consideration other factors that may affect humidity levels.<jupyter_output>The r values is 0.45403131790323875<jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>(slope, intercept, rvalue, pvalue, stderr) = linregress(southdf["Latitude"], southdf["Humidity"])
print(f'The r values is {rvalue}')
plt.scatter(southdf["Latitude"], southdf["Humidity"])
plt.title("Southern Hemisphere -  Humidity vs. Latitude Linear Regression")
plt.xlabel("Latitude")
plt.ylabel("Humidity")
regress_values = southdf["Latitude"] * slope + intercept
line_eq = "y =" + str(round(slope, 2)) + "x +" + str(round(intercept, 2))
plt.plot(southdf["Latitude"], regress_values, "r-")
plt.annotate(line_eq,(-50,40), fontsize= 16, color = 'red')

plt.savefig("./Southern Humidity vs. Latitude Linear Regression.png")

#There appears to be a slightly positive correlation between humidity and latitude in the southern hemisphere. However, not as strong of a correlationa as max temp - we may need to take into consideration other factors that may affect humidity levels<jupyter_output>The r values is 0.16836355620204524<jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>(slope, intercept, rvalue, pvalue, stderr) = linregress(northdf["Latitude"], northdf["Cloudiness"])
print(f'The r values is {rvalue}')
plt.scatter(northdf["Latitude"], northdf["Cloudiness"])
plt.title("Northern Hemisphere -  Cloudiness vs. Latitude Linear Regression")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness")
regress_values = northdf["Latitude"] * slope + intercept
line_eq = "y =" + str(round(slope, 2)) + "x +" + str(round(intercept, 2))
plt.plot(northdf["Latitude"], regress_values, "r-")
plt.annotate(line_eq,(40,20), fontsize= 16, color = 'red')

plt.savefig("./Northern Cloudiness vs. Latitude Linear Regression.png")

#There does not appear to be a strong correlation between cloudiness and latitude in the northern hemisphere. However, given how clustered the points are in certain cloudiness levels, cloudiness may be correlated to some other factor, such as altitude.<jupyter_output>The r values is 0.25968650637076124<jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>(slope, intercept, rvalue, pvalue, stderr) = linregress(southdf["Latitude"], southdf["Cloudiness"])
print(f'The r values is {rvalue}')
plt.scatter(southdf["Latitude"], southdf["Cloudiness"])
plt.title("Southern Hemisphere -  Cloudiness vs. Latitude Linear Regression")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness")
regress_values = southdf["Latitude"] * slope + intercept
line_eq = "y =" + str(round(slope, 2)) + "x +" + str(round(intercept, 2))
plt.plot(southdf["Latitude"], regress_values, "r-")
plt.annotate(line_eq,(-50,50), fontsize= 16, color = 'red')

plt.savefig("./Southern Cloudiness vs. Latitude Linear Regression.png")


#There does not appear to be a strong correlation between cloudiness and latitude in the southern hemisphere. However, given how clustered the points are in certain cloudiness levels, cloudiness may be correlated to some other factor, such as altitude.<jupyter_output>The r values is 0.45454100138997344<jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>(slope, intercept, rvalue, pvalue, stderr) = linregress(northdf["Latitude"], northdf["Wind Speed"])
print(f'The r values is {rvalue}')
plt.scatter(northdf["Latitude"], northdf["Cloudiness"])
plt.title("Northern Hemisphere -  Wind Speed vs. Latitude Linear Regression")
plt.xlabel("Latitude")
plt.ylabel("Wind Speed")
regress_values = northdf["Latitude"] * slope + intercept
line_eq = "y =" + str(round(slope, 2)) + "x +" + str(round(intercept, 2))
plt.plot(northdf["Latitude"], regress_values, "r-")
plt.annotate(line_eq,(20,20), fontsize= 16, color = 'red')

plt.savefig("./Northern Wind Speed vs. Latitude Linear Regression.png")

#There does not appear to be a strong correlation between wind speed and latitude in the Northern Hemisphere. However, given the clustered nature of points across each wind speed level, wind speed may be correlated some other factor.<jupyter_output>The r values is -0.02372895060007344<jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>(slope, intercept, rvalue, pvalue, stderr) = linregress(southdf["Latitude"], southdf["Wind Speed"])
print(f'The r values is {rvalue}')
plt.scatter(southdf["Latitude"], southdf["Cloudiness"])
plt.title("Southern Hemisphere -  Wind Speed vs. Latitude Linear Regression")
plt.xlabel("Latitude")
plt.ylabel("Wind Speed")
regress_values = southdf["Latitude"] * slope + intercept
line_eq = "y =" + str(round(slope, 2)) + "x +" + str(round(intercept, 2))
plt.plot(southdf["Latitude"], regress_values, "r-")
plt.annotate(line_eq,(-50,20), fontsize= 16, color = 'red')

plt.savefig("./Southern Wind Speed vs. Latitude Linear Regression.png")

#There does not appear to be a strong correlation between wind speed and latitude in the Southern Hemisphere. However, given the clustered nature of points across each wind speed level, wind speed may be correlated some other factor. 
#Three Observable Trends
#1. Temperature and Latitude had the strongest correlations in the northern and souther correlations. 
#2. There did not appear to be a strong correlation between cloudiness and latitude in both hemispheres. However, given how clustered the points are in certain cloudiness levels (in a relatively straight horizontal line), cloudiness may be correlated to some other factor, such as altitude. 
#3. There was a slightly positive correlation between humidity levels and latitude but not as strong as temp - other factors may be affectimg this.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1249263791636673, 'fraction_numerical': 0.017603559976441332, 'mean_word_length': 5.723273207215134, 'pattern_counts': {'":': 9, '<': 68, '<?xml version=': 0, '>': 69, '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': '363109', 'license': 'MIT', 'n_tokens_mistral': 5285, 'n_tokens_neox': 4752, 'n_words': 1436, 'path': 'WeatherPy/WeatherPy.ipynb', 'repo_name': 'estherkim0998/Python-API', 'size': '15281'}