<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>%matplotlib inline

# 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 json
import csv
import datetime as dt
from pprint import pprint


# Import API key
from config2 import weather_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 an our cities list
    if city not in cities:
        cities.append(city)
       
# Print the city count to confirm sufficient count
len(cities)
city_list = pd.DataFrame ({'City': cities})
city_list.head()<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># api info

url = "http://api.openweathermap.org/data/2.5/weather?"
units = "imperial"

#build query

query_url = f"{url}appid={weather_key}&units={units}&q="
#build lists for holding data

city_name=[]
cloudiness = []
country = []
date= []
humidity = []
lat = []
lng = []
max_temp = []
wind_speed = []

#create counter for city #
count = 1

#create print log
print(f'Beginning Data Retrieval')
print('-' * 50)

#loop through cities for requests(use try/except prevent error out)

for city in cities:
    try:
        response = requests.get(query_url + city).json()
        city_name.append(response['name'])
        cloudiness.append(response['clouds']['all'])
        country.append(response['sys']['country'])
        date.append(response['dt'])
        humidity.append(response['main']['humidity'])
        max_temp.append(response['main']['temp_max'])
        lat.append(response['coord']['lat'])
        lng.append(response['coord']['lon'])
        wind_speed.append(response['wind']['speed'])
        
        print(f'Processing Record {count} | {city}')
        
        count += 1
        
    except:
        print(f' Unable to process city')<jupyter_output>Beginning Data Retrieval
--------------------------------------------------
Processing Record 1 | talnakh
Processing Record 2 | mataura
Processing Record 3 | rikitea
Processing Record 4 | vaini
Processing Record 5 | maldonado
Processing Record 6 | tuatapere
Processing Record 7 | omboue
Processing Record 8 | brae
Processing Record 9 | thompson
Processing Record 10 | dunedin
Processing Record 11 | bouna
 Unable to process city
 Unable to process city
Processing Record 12 | new norfolk
Processing Record 13 | saint-augustin
Processing Record 14 | chokurdakh
Processing Record 15 | ushuaia
Processing Record 16 | jibuti
Processing Record 17 | albany
Processing Record 18 | georgetown
Processing Record 19 | saint-philippe
Processing Record 20 | marawi
Processing Record 21 | cesis
Processing Record 22 | arraial do cabo
Processing Record 23 | punta arenas
Processing Record 24 | palu
Processing Record 25 | busselton
Processing Record 26 | magaria
Processing Record 27 | avarua
Processing Record 28 [...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code># create dict

weather_dict = {
    'City' : city_name, 
    'Cloudiness': cloudiness, 
    'Country': country,
    'Datetime': date, 
    'Humidity' : humidity, 
    'Lat': lat,
    'Long' : lng,
    'Max Temp' : max_temp, 
    'Wind Speed' : wind_speed
    
}

# create df

weather_data = pd.DataFrame (weather_dict)

weather_data.head()
#save df to csv

weather_data.to_csv('weather_data.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>weather_data['Humidity'].unique()
#  Get the indices of cities that have humidity over 100%.
#no cities > 100<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>#today's date
date = dt.date.today().strftime("%m/%d/%Y")

plt.scatter(weather_data['Lat'], weather_data['Max Temp'], marker= 'o', alpha = 1, edgecolors='k')
plt.ylabel( 'Max Temp (F)')
plt.xlabel( 'Latitude')
plt.title(f'Latitude vs. Max Temp (F) {date}')


plt.savefig("Latitude_vs_Max_Temp.png")
plt.show()
# This is analyzing the correlation between temp and latitude. 
#As expected, the temperature generally increases as you approach the Equator (latitude approaches 0).<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code>plt.scatter(weather_data['Lat'], weather_data['Humidity'], marker = 'o', alpha =1, edgecolors = 'k')
plt.ylabel('Humidity')
plt.xlabel('Latitude')
plt.title(f'Latitude vs. Humidty {date}')


plt.savefig("Lat_vs_Humidity.png")
plt.show()
# This is analyzing the correlation between latitude and humidity. 
# There does not appear to be a strong correlation between latitude and humidity.<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness Plot<jupyter_code>plt.scatter(weather_data['Lat'], weather_data['Cloudiness'], marker='o', alpha =1, edgecolors = 'k')
plt.ylabel('Cloudiness')
plt.xlabel('Latitude')
plt.title(f'Latitude vs. Cloudiness {date}')


plt.savefig("Lat_vs_cloud.png")
plt.show()

# This is analyzing the correlation between latitude and cloudiness. 
# There does not appear to be a strong correlation between cloud cover and latitude.<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed Plot<jupyter_code>plt.scatter(weather_data['Lat'], weather_data['Wind Speed'], marker='o', alpha=1, edgecolors='k')
plt.ylabel('Wind Speed (mph)')
plt.xlabel('Latitude')
plt.title(f'Latitude vs. Wind Speed {date}')

plt.savefig("Lat_vs_Windspeed.png")
plt.show()
# This is analyzing the correlation between latitude and wind speed (mph). 
# There does not appear to be a strong correlation between wind speed and latitude.<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code>northern = weather_data.loc[weather_data["Lat"] >= 0.0]
northern.reset_index(inplace=True)

southern = weather_data.loc[weather_data["Lat"] < 0.0]
southern.reset_index(inplace=True)<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x = northern['Lat']
y = northern['Max Temp']



(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept

line_eq = f'y = {str(round(slope,2))}x + {str(round(intercept,2))}'
plt.scatter(x,y)
plt.plot(x,regress_values,"r-")
plt.annotate(line_eq,(10,35),fontsize=15,color="red")
plt.title("Northern Hemisphere-Max Temp vs.Latitude Linear Regression")
plt.ylabel('Max Temp')
plt.xlabel('Latitude')

print(f"The r-squared is: {rvalue}")
print(f"The equation of the regression line is: {line_eq}")


plt.savefig("Lat_vs_tempnh.png")
plt.show()
# There is a negative correlation between temp and latitude for the northern hemisphere. 
# As you increase in latitude, the temp is more likely to decrease.<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x = southern['Lat']
y = southern['Max Temp']



(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept

line_eq = f'y = {str(round(slope,2))}x + {str(round(intercept,2))}'
plt.scatter(x,y)
plt.plot(x,regress_values,"r-")
plt.annotate(line_eq,(-20,35),fontsize=15,color="red")
plt.title("Southern Hemisphere-Max Temp vs.Latitude Linear Regression")
plt.ylabel('Max Temp')
plt.xlabel('Latitude')

print(f"The r-squared is: {rvalue}")
print(f"The equation of the regression line is: {line_eq}")

plt.savefig("Lat_vs_tempsh.png")
plt.show()
# There is a positive correlation between temp and latitude for the southern hemisphere. 
# As you increase in latitude, the temp is likely to also increase.<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>x = northern['Lat']
y = northern['Humidity']



(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept

line_eq = f'y = {str(round(slope,2))}x + {str(round(intercept,2))}'
plt.scatter(x,y)
plt.plot(x,regress_values,"r-")
plt.annotate(line_eq,(50,15),fontsize=15,color="red")
plt.title("Northern Hemisphere-Humidity vs.Latitude Linear Regression")
plt.ylabel('Humidity')
plt.xlabel('Latitude')

print(f"The r-squared is: {rvalue}")
print(f"The equation of the regression line is: {line_eq}")

plt.savefig("Lat_vs_hum_nh.png")
plt.show()
# The correlation between humidity and latitude appears to be slightly negative and very weak. 
# There does not appear to be a significant on humidty based on latitude in the northern hemisphere.<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>x = southern['Lat']
y = southern['Humidity']



(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept

line_eq = f'y = {str(round(slope,2))}x + {str(round(intercept,2))}'
plt.scatter(x,y)
plt.plot(x,regress_values,"r-")
plt.annotate(line_eq,(-55,40),fontsize=15,color="red")
plt.title("Southern Hemisphere-Humidity vs.Latitude Linear Regression")
plt.ylabel('Humidity')
plt.xlabel('Latitude')

print(f"The r-squared is: {rvalue}")
print(f"The equation of the regression line is: {line_eq}")

plt.savefig("Lat_vs_hum_sh.png")
plt.show()
# The correlation between humidity and lat in the southern hemisphere is slightly positive and very weak. 
# There does not appear to be a significant impact on humidity based on latitude in the southern hemisphere.<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x = northern['Lat']
y = northern['Cloudiness']



(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept

line_eq = f'y = {str(round(slope,2))}x + {str(round(intercept,2))}'
plt.scatter(x,y)
plt.plot(x,regress_values,"r-")
plt.annotate(line_eq,(50,15),fontsize=15,color="red")
plt.title("Northern Hemisphere-Cloudiness vs.Latitude Linear Regression")
plt.ylabel('Cloudiness')
plt.xlabel('Latitude')

print(f"The r-squared is: {rvalue}")
print(f"The equation of the regression line is: {line_eq}")

plt.savefig("Lat_vs_cloud_nh.png")
plt.show()
# The corrlation between latitdue and cloudiness in the northern hemisphere is slightly negative and fairly weak. 
# The impact of latitidue on cloudiness appears to be insignificant in the northern hemisphere.<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x = southern['Lat']
y = southern['Cloudiness']



(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept

line_eq = f'y = {str(round(slope,2))}x + {str(round(intercept,2))}'
plt.scatter(x,y)
plt.plot(x,regress_values,"r-")
plt.annotate(line_eq,(-55,20),fontsize=15,color="red")
plt.title("Southern Hemisphere-Cloudiness vs.Latitude Linear Regression")
plt.ylabel('Cloudiness')
plt.xlabel('Latitude')

print(f"The r-squared is: {rvalue}")
print(f"The equation of the regression line is: {line_eq}")

plt.savefig("Lat_vs_cloud_sh.png")
plt.show()
# The correlation between cloudiness and latitude is negative and stronger in the southern hemisphere than it
# was in the northern hemisphere.<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x = northern['Lat']
y = northern['Wind Speed']



(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept

line_eq = f'y = {str(round(slope,2))}x + {str(round(intercept,2))}'
plt.scatter(x,y)
plt.plot(x,regress_values,"r-")
plt.annotate(line_eq,(50,18),fontsize=15,color="red")
plt.title("Northern Hemisphere- Wind Speed vs.Latitude Linear Regression")
plt.ylabel('Wind Speed (mph)')
plt.xlabel('Latitude')

print(f"The r-squared is: {rvalue}")
print(f"The equation of the regression line is: {line_eq}")

plt.savefig("Lat_vs_ws_nh.png")
plt.show()
# The correlation between wind speed and latitude in the northern hemispher is extremely weak and positive. 
# Latitude appears to have no impact on wind speed in the northern hemisphere.<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x = southern['Lat']
y = southern['Wind Speed']



(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept

line_eq = f'y = {str(round(slope,2))}x + {str(round(intercept,2))}'
plt.scatter(x,y)
plt.plot(x,regress_values,"r-")
plt.annotate(line_eq,(-20,25),fontsize=15,color="red")
plt.title("Southern Hemisphere-Wind Speed vs.Latitude Linear Regression")
plt.ylabel('Wind Speed (mph)')
plt.xlabel('Latitude')

print(f"The r-squared is: {rvalue}")
print(f"The equation of the regression line is: {line_eq}")

plt.savefig("Lat_vs_ws_sh.png")
plt.show()
# The correlation between wind speed and latitude is slightly stronger in the southern hemisphere than the northern, 
# but it is negative in the southern hemisphere which is opposite of the correlation between the two variable in the 
# northern hemisphere.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12722345747637576, 'fraction_numerical': 0.01132573652028905, 'mean_word_length': 5.626611418047882, 'pattern_counts': {'":': 0, '<': 73, '<?xml version=': 0, '>': 76, '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': '330536', 'license': 'ADSL', 'n_tokens_mistral': 5138, 'n_tokens_neox': 4703, 'n_words': 1344, 'path': 'python-api-challenge/WeatherPy/WeatherPy.ipynb', 'repo_name': 'EmilyZ9392/python-api-challenge', 'size': '14392'}