<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.Observable Trends1. Cities in between 40-75 latitudes are the most humid, with humidity percentages over 80.2. The Northern Hemisphere has a larger number of cities with a high humidity percentage than the Southern Hemisphere.3. For a majority of the cities, as the latitude increases, the max temperature decreases.<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 datetime import date 

# Import API key
#from api_keys import weather_api_key
my_api_source = open('/Users/bridg/OneDrive/Desktop/weather_api_key.txt')
weather_api_key = my_api_source.read()

# Import todays date in mm/dd/yyyy format
today = date.today()
today = today.strftime("%m/%d/%Y")

# Incorporated citipy to determine city based on latitude and longitude
from citipy import citipy

# Output File (CSV)
output_data_file = "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 information 
url = "http://api.openweathermap.org/data/2.5/weather?units=Imperial&APPID=" + weather_api_key
   


# Create empty lists to populate weather data 
city_id = []
city_name = []
cloudiness = []
country = []
humidity = []
latitude = []
longitude = []
max_temperature = []
wind_speed = []
date = []
counter = 0
sets = 1
error = 0 


# Loop through each city
for city in cities:
    
     # Create search query, make request and store in json
    query = url + "&q=" + city
    response = requests.get(query)
    response_json = response.json()
   
    # Create loop to fill lists 
    try:

        city_name.append(response_json['name'])
        country.append(response_json['sys']['country'])
        max_temperature.append(response_json ['main']['temp_max'])
        humidity.append(response_json['main']['humidity'])
        cloudiness.append(response_json['clouds']['all'])
        wind_speed.append(response_json['wind']['speed'])
        latitude.append(response_json['coord']['lat'])
        longitude.append(response_json['coord']['lon'])
        date.append(response_json['dt'])
        
        if counter >= 50: 
            counter = 1
            sets += 1
            time.sleep(1.00)
        else:
            counter += 1
            
        print(f"Processing Record {counter} of set {sets} | {city}")
       
       
    except:
        # Append null values
        print("City not found. Next...")
        
# Once search is complete, print 'Processing complete'
print(f"Processing complete")<jupyter_output>Processing Record 1 of set 1 | acapulco
Processing Record 2 of set 1 | katsuura
Processing Record 3 of set 1 | vardo
Processing Record 4 of set 1 | mataura
Processing Record 5 of set 1 | arman
City not found. Next...
City not found. Next...
Processing Record 6 of set 1 | busselton
Processing Record 7 of set 1 | tambopata
Processing Record 8 of set 1 | torbay
Processing Record 9 of set 1 | lensk
Processing Record 10 of set 1 | hilo
Processing Record 11 of set 1 | salalah
Processing Record 12 of set 1 | chuy
Processing Record 13 of set 1 | sao filipe
Processing Record 14 of set 1 | ushuaia
Processing Record 15 of set 1 | nikolskoye
Processing Record 16 of set 1 | anadyr
Processing Record 17 of set 1 | lompoc
Processing Record 18 of set 1 | mahebourg
City not found. Next...
Processing Record 19 of set 1 | laguna
Processing Record 20 of set 1 | kulu
Processing Record 21 of set 1 | shingu
Processing Record 22 of set 1 | guerrero negro
Processing Record 23 of set 1 | grindavik
Processing Rec[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code>weather_dict = {
    "city": city_name,
    "lat": latitude,
    "lon": longitude,
    "max temp": max_temperature,
    "humidity": humidity,
    "cloudiness": cloudiness,
    "wind speed": wind_speed,
    "country": country,
     "date": date}

city_data = pd.DataFrame(weather_dict)
city_data.head()


# Export city data into a .csv
city_data.to_csv(output_data_file, index = False, header = True)<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>city_data.describe()
#  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>x_values = city_data['lat']
y_values = city_data['max temp']
plt.scatter(x_values, y_values, alpha=0.5, edgecolor='black')
plt.title(f'City Latitude vs. Max Temperature ({today})')
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.grid()
plt.savefig("output_data/LatitudeVsTemperaturePlot.png")
plt.show()
# This code analyzes the max temperature recorded with the latitude of city recorded.<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code>x_values = city_data['lat']
y_values = city_data['humidity']
plt.scatter(x_values, y_values, alpha=0.5, edgecolor='black')
plt.title(f'City Latitude vs. Humidity ({today})')
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.grid()
plt.savefig("output_data/LatitudeVsHumidityPlot.png")
plt.show()
# This code analyzes the percentage of humidity as it relates to the latitude of the city recorded.
# This graph shows the vast majority of the cities in between 40-75 latitudes have humidity percentages over 80.<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness Plot<jupyter_code>x_values = city_data['lat']
y_values = city_data['cloudiness']
plt.scatter(x_values, y_values, alpha=0.5, edgecolor='black')
plt.title(f'City Latitude vs. Cloudiness ({today})')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.grid()
plt.savefig("output_data/LatitudeVsCloudinessPlot.png")
plt.show()
# This code analyzes the percentage of cloudiness as it relates to the latitude of the city recorded.<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed Plot<jupyter_code>x_values = city_data['lat']
y_values = city_data['wind speed']
plt.scatter(x_values, y_values, alpha=0.5, edgecolor='black')
plt.title(f'City Latitude vs. Wind Speed ({today})')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.grid()
plt.savefig("output_data/LatitudeVsWindSpeedPlot.png")
plt.show()
# This code analyzes wind speed (mph) as it relates to the latitude of the city recorded.<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code># Find index for hemispheres
index_northern = city_data[city_data['lat']>=0].index
index_southern = city_data[city_data['lat']<=0].index

# Create data frames for hemispheres
northern_df = city_data.drop(index_southern)
southern_df = city_data.drop(index_northern)<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x_values = northern_df['lat']
y_values = northern_df['max temp']

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.scatter(x_values, y_values, alpha=0.5, edgecolor='black')
plt.plot(x_values,regress_values,"r-")

plt.title(f'City Latitude vs. Max Temperature ({today})')
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.grid()
plt.savefig("output_data/Northern_LatitudeVsTemperaturePlot.png")
print(f"The r-value is: {rvalue**2}")
print(f"The line equation is: {line_eq}")
plt.show()<jupyter_output>The r-value is: 0.703637378599811
The line equation is: y = -1.4x + 92.75<jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x_values = southern_df['lat']
y_values = southern_df['max temp']

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.scatter(x_values, y_values, alpha=0.5, edgecolor='black')
plt.plot(x_values,regress_values,"r-")

plt.title(f'City Latitude vs. Max Temperature ({today})')
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.grid()
plt.savefig("output_data/Southern_LatitudeVsTemperaturePlot.png")
print(f"The r-value is: {rvalue**2}")
print(f"The line equation is: {line_eq}")
plt.show()
# In the Northern Hemisphere, the max temperature and city latitude has a negative linear relationship, while the Southern Hemisphere has a positive linear relationship.<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>x_values = northern_df['lat']
y_values = northern_df['humidity']

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.scatter(x_values, y_values, alpha=0.5, edgecolor='black')
plt.plot(x_values,regress_values,"r-")
plt.title(f'City Latitude vs. Humidity ({today})')
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.grid()
plt.savefig("output_data/Northern_LatitudeVsHumidityPlot.png")
print(f"The r-value is: {rvalue**2}")
print(f"The line equation is: {line_eq}")
plt.show()<jupyter_output>The r-value is: 0.2196200754323125
The line equation is: y = 0.44x + 57.81<jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>x_values = southern_df['lat']
y_values = southern_df['humidity']

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.scatter(x_values, y_values, alpha=0.5, edgecolor='black')
plt.plot(x_values,regress_values,"r-")
plt.title(f'City Latitude vs. Humidity ({today})')
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.grid()
plt.savefig("output_data/Southern_LatitudeVsHumidityPlot.png")
print(f"The r-value is: {rvalue**2}")
print(f"The line equation is: {line_eq}")
plt.show()
# For city latitude and humidity, both the Northern and Southern Hemispheres have a postive linear relationship.<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x_values = northern_df['lat']
y_values = northern_df['cloudiness']

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.scatter(x_values, y_values, alpha=0.5, edgecolor='black')
plt.plot(x_values,regress_values,"r-")
plt.title(f'City Latitude vs. Cloudiness ({today})')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.grid()
plt.savefig("output_data/Northern_LatitudeVsCloudiness Plot.png")
print(f"The r-value is: {rvalue**2}")
print(f"The line equation is: {line_eq}")
plt.show()<jupyter_output>The r-value is: 0.0718715598747644
The line equation is: y = 0.54x + 33.64<jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x_values = southern_df['lat']
y_values = southern_df['cloudiness']

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.scatter(x_values, y_values, alpha=0.5, edgecolor='black')
plt.plot(x_values,regress_values,"r-")
plt.title(f'City Latitude vs. Cloudiness ({today})')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.grid()
plt.savefig("output_data/Southern_LatitudeVsCloudiness Plot.png")
print(f"The r-value is: {rvalue**2}")
print(f"The line equation is: {line_eq}")
plt.show()
# For city latitude and cloudiness, both the Northern and Southern Hemispheres have a postive linear relationship.<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x_values = northern_df['lat']
y_values = northern_df['wind speed']

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.scatter(x_values, y_values, alpha=0.5, edgecolor='black')
plt.plot(x_values,regress_values,"r-")
plt.title(f'City Latitude vs. Wind Speed ({today})')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.grid()
plt.savefig("output_data/Northern_LatitudeVsWindSpeed Plot.png")
print(f"The r-value is: {rvalue**2}")
print(f"The line equation is: {line_eq}")
plt.show()<jupyter_output>The r-value is: 0.002881228628711455
The line equation is: y = 0.01x + 7.48<jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x_values = southern_df['lat']
y_values = southern_df['wind speed']

(slope, intercept, rvalue, pvalue, stderr) = linregress(x_values, y_values)
regress_values = x_values * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

plt.scatter(x_values, y_values, alpha=0.5, edgecolor='black')
plt.plot(x_values,regress_values,"r-")
plt.title(f'City Latitude vs. Wind Speed ({today})')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.grid()
plt.savefig("output_data/Southern_LatitudeVsWindSpeed Plot.png")
print(f"The r-value is: {rvalue**2}")
print(f"The line equation is: {line_eq}")
plt.show()
# In the Northern Hemisphere, the city latitude and wind speed has a positive linear relationship. In the SOuthern Hemisphere, it has a negative linear relationship.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1241670515273471, 'fraction_numerical': 0.017549647027775946, 'mean_word_length': 5.527993109388458, 'pattern_counts': {'":': 9, '<': 69, '<?xml version=': 0, '>': 72, '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': '107386', 'license': 'ADSL', 'n_tokens_mistral': 5526, 'n_tokens_neox': 5065, 'n_words': 1401, 'path': 'WeatherPy/WeatherPy.ipynb', 'repo_name': 'bareardon/python-api-challenge', 'size': '15157'}