<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
from pprint import pprint

# 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># *****Change city lats_lngs to 1500 for final
# 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># Save config information
url = "http://api.openweathermap.org/data/2.5/weather?units=Imperial&"


# Build query URL
i = 1
for city in cities:
    # API call
    query_url = url + "appid=" + weather_api_key + "&q=" + city
    # Get weather data
    weather_response = requests.get(query_url).json()
    
    pprint(weather_response)
    break
# Temperature (F) vs. Latitude
# Humidity (%) vs. Latitude
# Cloudiness (%) vs. Latitude
# Wind Speed (mph) vs. Latitude

# Create empty lists for API call
city_name =[]
lat = []
lng =[]
max_temp = []
humidity = []
cloudiness = []
wind_speed = []
country = []
date = []

for city in cities:
    # API call
    query_url = url + "appid=" + weather_api_key + "&q=" + city
    # Get weather data
    weather_response = requests.get(query_url).json()
    
    try:
        city_name.append(weather_response['name'])
        lat.append(weather_response['coord']['lat'])
        lng.append(weather_response['coord']['lon'])
        max_temp.append(weather_response['main']['temp_max'])
        humidity.append(weather_response['main']['humidity'])
        cloudiness.append(weather_response['clouds']['all'])
        wind_speed.append(weather_response['wind']['speed'])
        country.append(weather_response['sys']['country'])
        date.append(weather_response['dt'])
        print(f'Processing Record {i} | {city}')
        i+=1
    except KeyError:
        print(f'City not found. Skipping...')<jupyter_output>City not found. Skipping...
Processing Record 1 | leningradskiy
Processing Record 2 | san jose
Processing Record 3 | esperance
Processing Record 4 | chokurdakh
Processing Record 5 | santa cruz cabralia
Processing Record 6 | zandvoort
City not found. Skipping...
Processing Record 7 | shitanjing
Processing Record 8 | tuatapere
Processing Record 9 | new norfolk
Processing Record 10 | los llanos de aridane
Processing Record 11 | yeniseysk
Processing Record 12 | vaini
Processing Record 13 | qasigiannguit
Processing Record 14 | nikolskoye
Processing Record 15 | busselton
Processing Record 16 | turayf
City not found. Skipping...
Processing Record 17 | severo-kurilsk
Processing Record 18 | tasiilaq
Processing Record 19 | ushuaia
Processing Record 20 | hilo
Processing Record 21 | hobart
Processing Record 22 | arraial do cabo
Processing Record 23 | bengkulu
Processing Record 24 | rikitea
Processing Record 25 | lazaro cardenas
Processing Record 26 | semporna
Processing Record 27 | awbari
Processi[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code>city_df = pd.DataFrame(
    {'City': city_name,
    'Lat': lat,
    'Lng': lng,
    'Max Temp': max_temp,
    'Humidity': humidity,
    'Cloudiness': cloudiness,
    'Wind Speed': wind_speed,
    'Country': country,
    'Date': date})

city_df.head(10)

# output to csv
city_df.to_csv(output_data_file)<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># create stats summary to see what max humidity is
city_df.describe()
humid_cities = city_df.loc[city_df['Humidity']>100]
len(humid_cities)
#  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.<jupyter_code>import datetime

# change date time to readable format
city_date = datetime.datetime.fromtimestamp(int(city_df["Date"][0])).strftime('%Y-%m-%d')
city_date<jupyter_output><empty_output><jupyter_text>Latitude vs. Temperature Plot<jupyter_code># create scatter plot of Lat v Temp
plt.scatter(city_df['Lat'], city_df['Max Temp'])
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.title(f'City Latitude v Max Temperature ({city_date})')
plt.savefig('../output_data/Latitude v. Temperature plot.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Tempeartures appear to be hottest the closer the city is to the Equator (0 latitude) and go down as you get closer to each pole. Cities near the North pole are coldest.  Latitude vs. Humidity Plot<jupyter_code>plt.scatter(city_df['Lat'], city_df['Humidity'])
plt.xlabel('Latitude')
plt.ylabel('Humidity')
plt.title(f'City Latitude v Max Humidity({city_date})')
plt.savefig('../output_data/Latitude v. Humidity plot.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Humidity is higher concentrated towards the pole and slightly lower as you move towards the Equator and South pole. This makes sense given we are nearing winter in the northern hemisphere.  Latitude vs. Cloudiness Plot<jupyter_code>plt.scatter(city_df['Lat'], city_df['Cloudiness'])
plt.xlabel('Latitude')
plt.ylabel('Cloudiness')
plt.title(f'City Latitude v Cloudiness ({city_date})') 
plt.savefig('../output_data/Latitude v. Cloudiness plot.png')
plt.show()<jupyter_output><empty_output><jupyter_text>There does not seem to be a clear correlation between city location and cloudiness.   Latitude vs. Wind Speed Plot<jupyter_code>plt.scatter(city_df['Lat'], city_df['Wind Speed'])
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.title(f'City Latitude v Wind Speed ({city_date})') 
plt.savefig('../output_data/Latitude v. Wind plot.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Wind speed seems overall consistent regardless of city location. There are a few outliers near the poles that could be due to seasonal storms.   Linear Regression   Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code># create df by north and south hemisphere
north_df = city_df.loc[city_df['Lat']>=0]
south_df = city_df.loc[city_df['Lat']<0]
# define x and y values
x_values = north_df['Lat']
y_values = north_df['Max Temp']

# calculate linear regression, y=mx+b
nh_slope, nh_int, nh_r, nh_p, nh_std_err = linregress(x_values,y_values)
nh_fit = nh_slope * x_values + nh_int
line_eq = 'y =' + str(round(nh_slope,2))+'x +' + str(round(nh_int,2))

print(f'The r-value is:', nh_r)

# plot scatterplot
plt.scatter(x_values, y_values)
plt.plot(x_values,nh_fit,'-',color='red')
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.title(f'Northern Hemisphere Correlation - Max Temp vs. Latitude Linear Regression')
plt.annotate(line_eq, (10,0), fontsize=15, color='red')
plt.savefig('../output_data/Northern Hemisphere Correlation - Max Temp v. Latitude Linear Regression.png')
plt.show()<jupyter_output>The r-value is: -0.8398276075873286<jupyter_text>In the Northern Hemisphere, temperature and city location are very closely correlated. The linear regression shows the temperature drops steadily the closer the city is to the North Pole.   Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code># define x and y values
x_values = south_df['Lat']
y_values = south_df['Max Temp']

# calculate linear regression, y=mx+b
nh_slope, nh_int, nh_r, nh_p, nh_std_err = linregress(x_values,y_values)
nh_fit = nh_slope * x_values + nh_int
line_eq = 'y =' + str(round(nh_slope,2))+'x +' + str(round(nh_int,2))

print(f'The r-value is:', nh_r)

# plot scatterplot
plt.scatter(x_values, y_values)
plt.plot(x_values,nh_fit,'-',color='red')
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.title(f'Southern Hemisphere Correlation - Max Temp vs. Latitude Linear Regression')
plt.annotate(line_eq, (-30,50), fontsize=15,color='red')
plt.savefig('../output_data/Southern Hemisphere Correlation - Max Temp v. Latitude Linear Regression.png')
plt.show()<jupyter_output>The r-value is: 0.5047486789821441<jupyter_text>Temperature and city location appear to be very loosely correlated for cities in the Southern Hemisphere. The closer the city is to the Equator, the hotter the temperature. The loose correlation makes sense given the time of year as the Southern Hemisphere cities are more likely to have overall moderate tempeatures.    Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code># define x and y values
x_values = north_df['Lat']
y_values = north_df['Humidity']

# calculate linear regression, y=mx+b
nh_slope, nh_int, nh_r, nh_p, nh_std_err = linregress(x_values,y_values)
nh_fit = nh_slope * x_values + nh_int
line_eq = 'y =' + str(round(nh_slope,2))+'x +' + str(round(nh_int,2))

print(f'The r-value is:', nh_r)

# plot scatterplot
plt.scatter(x_values, y_values)
plt.plot(x_values,nh_fit,'-',color='red')
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.title(f'Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression')
plt.annotate(line_eq, (45,20), fontsize=15, color='red')
plt.savefig('../output_data/Northern Hemisphere - Humidity v. Latitude Linear Regression.png')
plt.show()<jupyter_output>The r-value is: 0.24847477818611635<jupyter_text>Humidity is loosely correlated to city location in the Northern Hemipshere. We can see the closer the city is to the North pole, the higher the humidity which is what we would imagine as we move closer to winter, rain and snow.    Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code># define x and y values
x_values = south_df['Lat']
y_values = south_df['Humidity']

# calculate linear regression, y=mx+b
nh_slope, nh_int, nh_r, nh_p, nh_std_err = linregress(x_values,y_values)
nh_fit = nh_slope * x_values + nh_int
line_eq = 'y =' + str(round(nh_slope,2))+'x +' + str(round(nh_int,2))

print(f'The r-value is:', nh_r)

# plot scatterplot
plt.scatter(x_values, y_values)
plt.plot(x_values,nh_fit,'-',color='red')
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.title(f'Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression')
plt.annotate(line_eq, (-35,30), fontsize=15, color='red')
plt.savefig('../output_data/Southern Hemisphere - Humidity v. Latitude Linear Regression.png')
plt.show()<jupyter_output>The r-value is: 0.2977224097512074<jupyter_text>Humidity is loosely correlated to city location in the Southern Hemipshere. Humidity is increases slightly as we move towards the Equator.    Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code># define x and y values
x_values = north_df['Lat']
y_values = north_df['Cloudiness']

# calculate linear regression, y=mx+b
nh_slope, nh_int, nh_r, nh_p, nh_std_err = linregress(x_values,y_values)
nh_fit = nh_slope * x_values + nh_int
line_eq = 'y =' + str(round(nh_slope,2))+'x +' + str(round(nh_int,2))

print(f'The r-value is:', nh_r)

# plot scatterplot
plt.scatter(x_values, y_values)
plt.plot(x_values,nh_fit,'-',color='red')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.title(f'Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression')
plt.annotate(line_eq, (20,65),fontsize=15, color='red')
plt.savefig('../output_data/Northern Hemisphere - Cloudiness v. Latitude Linear Regression.png')
plt.show()<jupyter_output>The r-value is: 0.27396453002444854<jupyter_text>There does not seem to be a strong correlation between cloudiness and city location in the Northern Hemisphere.    Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code># define x and y values
x_values = south_df['Lat']
y_values = south_df['Cloudiness']

# calculate linear regression, y=mx+b
nh_slope, nh_int, nh_r, nh_p, nh_std_err = linregress(x_values,y_values)
nh_fit = nh_slope * x_values + nh_int
line_eq = 'y =' + str(round(nh_slope,2))+'x +' + str(round(nh_int,2))

print(f'The r-value is:', nh_r)

# plot scatterplot
plt.scatter(x_values, y_values)
plt.plot(x_values,nh_fit,'-',color='red')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.title(f'Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression')
plt.annotate(line_eq, (-40,60),fontsize=15, color='red')
plt.savefig('../output_data/Southern Hemisphere - Cloudiness v. Latitude Linear Regression.png')
plt.show()<jupyter_output>The r-value is: 0.35789391655755204<jupyter_text>There does not seem to be a strong correlation between cloudiness and city location in the Southern Hemisphere.   Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code># define x and y values
x_values = north_df['Lat']
y_values = north_df['Wind Speed']

# calculate linear regression, y=mx+b
nh_slope, nh_int, nh_r, nh_p, nh_std_err = linregress(x_values,y_values)
nh_fit = nh_slope * x_values + nh_int
line_eq = 'y =' + str(round(nh_slope,2))+'x +' + str(round(nh_int,2))

print(f'The r-value is:', nh_r)

# plot scatterplot
plt.scatter(x_values, y_values)
plt.plot(x_values,nh_fit,'-',color='red')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph))')
plt.title(f'Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression')
plt.annotate(line_eq, (20,40),fontsize=15, color='red')
plt.savefig('../output_data/Northern Hemisphere - Wind Speed v. Latitude Linear Regression.png')
plt.show()<jupyter_output>The r-value is: 0.29356143274324864<jupyter_text>There is a very weak correlation between wind speed and city location for cities in the Northern Hemisphere.   Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code># define x and y values
x_values = south_df['Lat']
y_values = south_df['Wind Speed']

# calculate linear regression, y=mx+b
nh_slope, nh_int, nh_r, nh_p, nh_std_err = linregress(x_values,y_values)
nh_fit = nh_slope * x_values + nh_int
line_eq = 'y =' + str(round(nh_slope,2))+'x +' + str(round(nh_int,2))

print(f'The r-value is:', nh_r)

# plot scatterplot
plt.scatter(x_values, y_values)
plt.plot(x_values,nh_fit,'-',color='red')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.title(f'Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression')
plt.annotate(line_eq, (-40,20), fontsize=15, color='red')
plt.savefig('../output_data/Southern Hemisphere - Wind Speed v. Latitude Linear Regression.png')
plt.show()<jupyter_output>The r-value is: -0.3084443114705898
{'fraction_non_alphanumeric': 0.11867088607594936, 'fraction_numerical': 0.018860759493670887, 'mean_word_length': 5.556431535269709, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 68, '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': '52253', 'license': 'ADSL', 'n_tokens_mistral': 5868, 'n_tokens_neox': 5349, 'n_words': 1528, 'path': 'WeatherPy/WeatherPy.ipynb', 'repo_name': 'staceywai/python-api-challenge', 'size': '15800'}