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

# 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 info
url='http://api.openweathermap.org/data/2.5/weather?'
units='imperial'

# Build partial query URL:
query_url=f'{url}appid={weather_api_key}&units={units}&q='
city_name=[]       # list for city name
cloudiness=[]      # list for cloudines
country=[]         # list for country
date=[]            # list for date
humidity=[]        # list for humidity
latitude=[]        # list for latitude
longitude=[]       # list for longitude
max_temp=[]        # list for max temperature
wind_speed=[]      # list for wind speed

count_start = 1

print('Beginning Data Retrieval')     
print('-----------------------------')
for city in cities:
        response=requests.get(query_url + city).json()
        
        print('Processing Record ' + str(count_start) + ' of ' + str(len(cities)) + ' | ' + city)
        
        try:                                            
            city_name.append(response['name'])
            cloudiness.append(response['clouds']['all'])
            country.append(response['sys']['country'])
            date.append(response['dt'])
            humidity.append(response['main']['humidity'])             
            latitude.append(response['coord']['lat'])
            longitude.append(response['coord']['lon'])             
            max_temp.append(response['main']['temp_max'])
            wind_speed.append(response['wind']['speed'])   
            
        except KeyError:
            # In case one of the responses comes back with error "KeyError"
            print(city + ' city not found. Skipping...') 
                                                              
        count_start=count_start+1
        
        # Print Ending Log Statement
print(f"-------------------------------")
print(f"Data Retrieval Complete")
print(f"-------------------------------")<jupyter_output>Beginning Data Retrieval
-----------------------------
Processing Record 1 of 606 | yellowknife
Processing Record 2 of 606 | sur
Processing Record 3 of 606 | leningradskiy
Processing Record 4 of 606 | port hardy
Processing Record 5 of 606 | pakpattan
Processing Record 6 of 606 | new norfolk
Processing Record 7 of 606 | albany
Processing Record 8 of 606 | bluff
Processing Record 9 of 606 | mataura
Processing Record 10 of 606 | castro
Processing Record 11 of 606 | maldonado
Processing Record 12 of 606 | hermanus
Processing Record 13 of 606 | mackay
Processing Record 14 of 606 | korla
Processing Record 15 of 606 | saint-philippe
Processing Record 16 of 606 | alice town
Processing Record 17 of 606 | sao joao da barra
Processing Record 18 of 606 | avera
Processing Record 19 of 606 | petropavlovka
Processing Record 20 of 606 | carnarvon
Processing Record 21 of 606 | kadoma
Processing Record 22 of 606 | bur gabo
bur gabo city not found. Skipping...
Processing Record 23 of 606 | jamestown
Proc[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code># Create a DataFrame from Cities, Latitude, Longitude, Temperature, Humidity, Cloudiness & Wind Speed
weather_dict = {
    "City": city_name,
    "Country": country,
    "Date": date,
    "Latitude": latitude,
    "Longitude": longitude,
    "Max Temperature (F)": max_temp,
    "Humidity (%)": humidity,
    "Cloudiness (%)": cloudiness,
    "Wind Speed (mph)": wind_speed
}

weather_data = pd.DataFrame(weather_dict)
weather_data.isnull().sum()
weather_data.dropna() 
weather_data.count()
weather_data.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>#  Get the indices of cities that have humidity over 100%.
weather_data.loc[weather_data['Humidity (%)'] > 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_data["Latitude"], weather_data["Max Temperature (F)"], facecolors="red", marker="o", edgecolor="black")

plt.title("City Latitude vs. Max Temperature (02/11/2021)")
plt.ylabel("Max Temperature (°F)")
plt.xlabel("Latitude")
plt.grid(True)

# Save the figure:
#plt.savefig("output_data/City Latitude vs. MaxTemp.png")

plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code>plt.scatter(weather_data["Latitude"], weather_data["Humidity (%)"], facecolors="m", marker="o", edgecolor="black")

plt.title("City Latitude vs. Humidity (02/11/2021)")
plt.ylabel("Humidity (%)")
plt.xlabel("Latitude")
plt.grid(True)

plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness Plot<jupyter_code>plt.scatter(weather_data["Latitude"], weather_data["Cloudiness (%)"], facecolors="darkblue", marker="o", edgecolor="black")

plt.title("City Latitude vs. Cloudiness (02/11/2021)")
plt.ylabel("Cloudiness (%)")
plt.xlabel("Latitude")
plt.grid(True)

plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed Plot<jupyter_code>plt.scatter(weather_data["Latitude"], weather_data["Wind Speed (mph)"], facecolors="mediumblue", marker="o", edgecolor="black")

plt.title("City Latitude vs. Wind Speed (02/11/2021")
plt.ylabel("Wind Speed (mph)")
plt.xlabel("Latitude")
plt.grid(True)

plt.show()<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code>weather_data_north = weather_data[weather_data['Latitude'] >= 0]
weather_data_south = weather_data[weather_data['Latitude'] < 0]<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code># Build scatter plot for each data type:
plt.figure(figsize=(9,6))
x_values = weather_data_north['Latitude']
y_values = weather_data_north['Max Temperature (F)']

# Perform a linear regression for temperature vs. latitude:
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)

# Get regression values:
regress_values = x_values * slope + intercept

# Create line equation string:
line_eq = 'y = ' + str(round(slope,2)) + 'x + ' + str(round(intercept,2))

# Create plot:
plt.scatter(x_values,y_values, marker='o', color='m', s=[70], edgecolors='black')
plt.plot(x_values,regress_values, 'darkred', linewidth=2)
plt.title('Northern Hemisphere - Max Temp vs. Latitude', fontsize=20)
plt.ylabel('Max Temperature (F)', fontsize=16, color='black')
plt.xlabel('Latitude', fontsize=16, color='black')
plt.annotate(line_eq,(1, 1), fontsize=18, color='darkred')

# Set the limits 
plt.ylim(min(weather_data['Max Temperature (F)'])-10, max(weather_data['Max Temperature (F)'])+10)
plt.xlim(-3, max(weather_data['Latitude'])+3)

# Print r-squared value:
print(f'The r-squared is: {round(rvalue**2,2)}')

plt.show()<jupyter_output>The r-squared is: 0.78<jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code># Build scatter plot for each data type:
plt.figure(figsize=(9,6))
x_values = weather_data_south['Latitude']
y_values = weather_data_south['Max Temperature (F)']

# Perform a linear regression for temperature vs. latitude:
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)

# Get regression values:
regress_values = x_values * slope + intercept

# Create line equation string:
line_eq2 = 'y = ' + str(round(slope,2)) + 'x + ' + str(round(intercept,2))

# Create plot:
plt.scatter(x_values,y_values, marker='o', color='m', s=[70], edgecolors='black')
plt.plot(x_values,regress_values, 'darkred', linewidth=2)
plt.title('Southern Hemisphere - Max Temp vs. Latitude', fontsize=20)
plt.ylabel('Max Temperature (F)', fontsize=16, color='black')
plt.xlabel('Latitude', fontsize=16, color='black')
plt.annotate(line_eq2,(-25,43),fontsize=18, color='darkred')


# Set the limits 
plt.ylim(30, 100)
plt.xlim(min(weather_data['Latitude'])-3, +1)

# Print r-squared value:
print(f'The r-squared is: {round(rvalue**2,2)}')

plt.show()<jupyter_output>The r-squared is: 0.31<jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code># Build scatter plot for each data type:
plt.figure(figsize=(9,6))
x_values = weather_data_north['Latitude']
y_values = weather_data_north['Humidity (%)']

# Perform a linear regression for humidity vs. latitude:
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)

# Get regression values:
regress_values = x_values * slope + intercept

# Create line equation string:
line_eq = 'y = ' + str(round(slope,2)) + 'x + ' + str(round(intercept,2))

# Create plot:
plt.scatter(x_values,y_values, marker='o', color='m', s=[70], edgecolors='black')
plt.plot(x_values,regress_values,'darkred', linewidth=2)
plt.title('Northern Hemisphere - Humidity (%) vs. Latitude', fontsize=20)
plt.ylabel('Humidity (%)', fontsize=16, color='black')
plt.xlabel('Latitude', fontsize=16, color='black')
plt.annotate(line_eq,(51,6),fontsize=18, color='darkred')

# Set the limits 
plt.ylim(-6, 110)
plt.xlim(-4, 82)

# Print r-squared value:
print(f'The r-squared is: {round(rvalue**2,2)}')

plt.show()<jupyter_output>The r-squared is: 0.19<jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code># Build scatter plot for each data type:
plt.figure(figsize=(9,6))
x_values = weather_data_north['Latitude']
y_values = weather_data_north['Humidity (%)']

# Perform a linear regression for cloudiness vs. latitude:
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)

# Get regression values:
regress_values = x_values * slope + intercept

# Create line equation string:
line_eq = 'y = ' + str(round(slope,2)) + 'x + ' + str(round(intercept,2))

# Create plot:
plt.scatter(x_values,y_values, marker='o', color='m', s=[70], edgecolors='black')
plt.plot(x_values,regress_values,'darkred', linewidth=2)
plt.title('Southern Hemisphere - Humidity (%) vs. Latitude', fontsize=20)
plt.ylabel('Humidity (%)', fontsize=16, color='black')
plt.xlabel('Latitude', fontsize=16, color='black')
plt.annotate(line_eq,(35,25), fontsize=18, color='darkred')

# Set the limits :
plt.ylim(0, 110)
plt.xlim(-1, 80)

# Print r-squared value:
print(f'The r-squared is: {round(rvalue**2,2)}')

plt.show()<jupyter_output>The r-squared is: 0.19<jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code># Build scatter plot for each data type:
plt.figure(figsize=(9,6))
x_values = weather_data_north['Latitude']
y_values = weather_data_north['Cloudiness (%)']

# Perform a linear regression for cloudiness vs. latitude:
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)

# Get regression values:
regress_values = x_values * slope + intercept

# Create line equation string:
line_eq = 'y = ' + str(round(slope,2)) + 'x + ' + str(round(intercept,2))

# Create plot:
plt.scatter(x_values,y_values, marker='o', color='m', s=[70], edgecolors='black')
plt.plot(x_values,regress_values,'darkred', linewidth=2)
plt.title('Northern Hemisphere - Cloudiness (%) vs. Latitude', fontsize=20)
plt.ylabel('Cloudiness (%)', fontsize=16, color='black')
plt.xlabel('Latitude', fontsize=16, color='black')
plt.annotate(line_eq,(35,25), fontsize=18, color='darkred')

# Set the limits 
plt.ylim(-8, 110)
plt.xlim(-3, 82)

# Print r-squared value:
print(f'The r-squared is: {round(rvalue**2,2)}')

plt.show()<jupyter_output>The r-squared is: 0.04<jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code># Build scatter plot for each data type:
plt.figure(figsize=(9,6))
x_values = weather_data_south['Latitude']
y_values = weather_data_south['Cloudiness (%)']

# Perform a linear regression for cloudiness vs. latitude:
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)

# Get regression values:
regress_values = x_values * slope + intercept

# Create line equation string:
line_eq = 'y = ' + str(round(slope,2)) + 'x + ' + str(round(intercept,2))

# Create plot:
plt.scatter(x_values,y_values, marker='o', color='m', s=[70], edgecolors='black')
plt.plot(x_values,regress_values,'darkred', linewidth=2)
plt.title('Southern Hemisphere - Cloudiness (%) vs. Latitude', fontsize=20)
plt.ylabel('Cloudiness (%)', fontsize=16, color='black')
plt.xlabel('Latitude', fontsize=16, color='black')
plt.annotate(line_eq,(35,25), fontsize=18, color='darkred')


# Print r-squared value:
print(f'The r-squared is: {round(rvalue**2,2)}')

plt.show()<jupyter_output>The r-squared is: 0.17<jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code># Build scatter plot for each data type:
plt.figure(figsize=(9,6))
x_values = weather_data_north['Latitude']
y_values = weather_data_north['Wind Speed (mph)']

# Perform a linear regression for wind speed vs. latitude:
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)

# Get regression values:
regress_values = x_values * slope + intercept

# Create line equation string:
line_eq = 'y = ' + str(round(slope,2)) + 'x + ' + str(round(intercept,2))

# Create plot:
plt.scatter(x_values,y_values, marker='o', color='b', s=[70], edgecolors='black')
plt.plot(x_values,regress_values,'darkred', linewidth=2)
plt.title('Northern Hemisphere - Wind Speed vs. Latitude', fontsize=20)
plt.ylabel('Wind Speed (mph)', fontsize=16, color='black')
plt.xlabel('Latitude', fontsize=16, color='black')
plt.annotate(line_eq,(2,42), fontsize=18, color='darkred')

# Set the limits 
plt.ylim(-3, 48)
plt.xlim(-3, 82)

# Print r-squared value:
print(f'The r-squared is: {round(rvalue**2,2)}')

# Show plot:
plt.show()<jupyter_output>The r-squared is: 0.0<jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code># Build scatter plot for each data type:
plt.figure(figsize=(9,6))
x_values = weather_data_south['Latitude']
y_values = weather_data_south['Wind Speed (mph)']

# Perform a linear regression for wind speed vs. latitude:
(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)

# Get regression values:
regress_values = x_values * slope + intercept

# Create line equation string:
line_eq = 'y = ' + str(round(slope,2)) + 'x + ' + str(round(intercept,2))

# Create plot:
plt.scatter(x_values,y_values, marker='o', color='b', s=[70], edgecolors='black')
plt.plot(x_values,regress_values,'darkred', linewidth=2)
plt.title('Southern Hemisphere - Wind Speed vs. Latitude', fontsize=20)
plt.ylabel('Wind Speed (mph)', fontsize=16, color='black')
plt.xlabel('Latitude', fontsize=16, color='black')
plt.annotate(line_eq,(2,42), fontsize=18, color='darkred')

# Set the limits 
plt.ylim(0, 80)
plt.xlim(-30, 10)

# Print r-squared value:
print(f'The r-squared is: {round(rvalue**2,2)}')

# Show plot:
plt.show()<jupyter_output>The r-squared is: 0.05
{'fraction_non_alphanumeric': 0.1454075498918009, 'fraction_numerical': 0.024945900456840586, 'mean_word_length': 5.1664195700518905, 'pattern_counts': {'":': 9, '<': 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': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '18', 'dir': 'jupyter-structured-clean-dedup', 'id': '227031', 'license': 'ADSL', 'n_tokens_mistral': 6175, 'n_tokens_neox': 5542, 'n_words': 1442, 'path': 'WeatherPy/WeatherPy.ipynb', 'repo_name': 'Vlvasquez23/python-api-challenge', 'size': '16636'}