<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
city_left_count = len(cities)
city_left_count<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).> **HINT:** The OpenWeatherMap API only allows 60 calls per minute under their free plan. Try using `time.sleep(60)` after each set of 50 cities to avoid API errors due to large calls.<jupyter_code>#Setting the lists for our Data Frame
sets = [cities[x:x+50] for x in range(0,len(cities),50)]
lat = []
lng = []
max_temp = []
humidity=[]
cloudiness = []
wind_speed = []
country = []
date = []
cities2 = []
# Saving config information.
url = "http://api.openweathermap.org/data/2.5/weather?"
units = "metric"
# Building partial query URL
query_url = f"{url}appid={weather_api_key}&units={units}&q="
print(f"Beginning Data Retrieval")
# Creating a loop through each rown in the dataframe to gather each city's weather info
for i in range(10):
    print(f"--------------------------------")
    time.sleep(10)
    for x in range(50):
        response = requests.get(query_url + sets[i][x]).json()
        # using try/except construct to skip over missing cities or missing data
        try:
            lat.append(response['coord']['lat'])
            lng.append(response['coord']['lon'])
            max_temp.append(response['main']['temp_max'])
            humidity.append(response['main']['humidity'])
            cloudiness.append(response['clouds']['all'])
            wind_speed.append(response['wind']['speed'])
            country.append(response['sys']['country'])
            date.append(response['dt'])
            cities2.append(sets[i][x])
            print(f" Processing Record {x+1} of Set {i+1} | {sets[i][x]}")
        except:
            print(f"City not found. Skipping...")
           
            pass
print("------------------------------\nData Retrieval Complete\n------------------------------")<jupyter_output>Beginning Data Retrieval
--------------------------------
 Processing Record 1 of Set 1 | chuy
 Processing Record 2 of Set 1 | lata
 Processing Record 3 of Set 1 | shubarshi
 Processing Record 4 of Set 1 | barrow
 Processing Record 5 of Set 1 | hasaki
City not found. Skipping...
 Processing Record 7 of Set 1 | ca mau
 Processing Record 8 of Set 1 | ilulissat
 Processing Record 9 of Set 1 | fairbanks
 Processing Record 10 of Set 1 | georgetown
 Processing Record 11 of Set 1 | jamestown
 Processing Record 12 of Set 1 | upernavik
 Processing Record 13 of Set 1 | cowra
 Processing Record 14 of Set 1 | waingapu
 Processing Record 15 of Set 1 | jiuquan
 Processing Record 16 of Set 1 | bafq
 Processing Record 17 of Set 1 | tocopilla
City not found. Skipping...
 Processing Record 19 of Set 1 | rikitea
 Processing Record 20 of Set 1 | port elizabeth
 Processing Record 21 of Set 1 | hermanus
 Processing Record 22 of Set 1 | gbadolite
 Processing Record 23 of Set 1 | shingu
 Processing Record 24 [...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code># Creating a dictionary to establish the dataframe
weather_data = ({
                        "City" : cities2,
                        "Lat" : lat,
                        "Lng" : lng,
                        "Max Temp" : max_temp,
                        "Humidity" : humidity,
                        "Cloudiness" : cloudiness,
                        "Wind Speed" : wind_speed,
                        "Country" : country,
                        "Date" : date
                })
weather_data
# Creating a dataframe to hold the variables.
weather_data_df = pd.DataFrame(weather_data)
weather_data_df
# Exporting the city data into a csv and Showing the top of the data frame
weather_data_df.to_csv("output_data_file")
weather_data_df.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># Inspecting the data for any cities with humidity >100%
humidity_check = weather_data_df["Humidity"].describe(include = "all")
print("By inspection, the maximum humidity value is 100%, so there are no values greater than 100% in our data!")
humidity_check

#  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># Generating a scatter plot of Latitude vs. Temperature 
plt.scatter(weather_data_df["Lat"],weather_data_df["Max Temp"],edgecolors="black",facecolors="skyblue")
plt.title("City Latitude vs. Max Temperature")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (F)")
plt.grid (b=True,which="major",axis="both",linestyle="-",color="lightgrey")
plt.savefig("fig1.png")
plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code># Generating a scatter plot of Latitude vs. Humidity Plot
plt.scatter(weather_data_df["Lat"],weather_data_df["Humidity"],edgecolors="black",facecolors="skyblue")
plt.title("City Latitude vs. Humidity")
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")
plt.ylim(15,105)
plt.grid (b=True,which="major",axis="both",linestyle="-",color="lightgrey")
plt.savefig("fig2.png")
plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness Plot<jupyter_code># Generating a scatter plot of Latitude vs. Cloudiness
plt.scatter(weather_data_df["Lat"],weather_data_df["Cloudiness"],edgecolors="black",facecolors="skyblue")
plt.title("City Latitude vs. Cloudiness")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")
plt.grid (b=True,which="major",axis="both",linestyle="-",color="lightgrey")
plt.savefig("fig3.png")
plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed Plot<jupyter_code># Generating a scatter plot of Latitude vs. Wind Speed
plt.scatter(weather_data_df["Lat"],weather_data_df["Wind Speed"],edgecolors="black",facecolors="skyblue")
plt.title("City Latitude vs. Wind Speed")
plt.xlabel("Latitude")
plt.ylabel("Wind Speed (mph)")
plt.ylim(-2,34)
plt.grid (b=True,which="major",axis="both",linestyle="-",color="lightgrey")
plt.savefig("fig4.png")
plt.show()<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code># Create Northern and Southern Hemisphere DataFrames

# Generating the Northern Hemisphere DataFrame
northern_hemisphere = weather_data_df.loc[weather_data_df["Lat"] >= 0]
northern_cities_count = len(northern_hemisphere)
print("There are {} cities in the northern hemisphere out of {} cities total, representing {:.1%} of all cities in the data set."
     .format(northern_cities_count, city_left_count, (northern_cities_count/city_left_count)))

# Generating the Southern Hemisphere DataFrame
southern_hemisphere = weather_data_df.loc[weather_data_df["Lat"] < 0]
southern_cities_count = len(southern_hemisphere)
print("There are {} cities in the southern hemisphere out of {} cities total, representing {:.1%} of all cities in the data set."
     .format(southern_cities_count, city_left_count, (southern_cities_count/city_left_count)))<jupyter_output>There are 310 cities in the northern hemisphere out of 608 cities total, representing 51.0% of all cities in the data set.
There are 147 cities in the southern hemisphere out of 608 cities total, representing 24.2% of all cities in the data set.<jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code># Generating a linear regression model of Max Temp vs. Latitude on the Northern Hemisphere
x_values = northern_hemisphere["Lat"]
y_values = northern_hemisphere["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(northern_hemisphere["Lat"],northern_hemisphere["Max Temp"],edgecolors="black",facecolors="skyblue")
plt.title("Northern Hemisphere - Max Temp vs. Latitude")
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(6,10),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (F)")
plt.grid (b=True,which="major",axis="both",linestyle="-",color="lightgrey")
print(f"The r-squared is: {rvalue**2}")
plt.savefig("fig5.png")
plt.show()<jupyter_output>The r-squared is: 0.719094050502069<jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code># Generating a linear regression model of Max Temp vs. Latitude on the Southern Hemisphere
x_values = southern_hemisphere["Lat"]
y_values = southern_hemisphere["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(southern_hemisphere["Lat"],southern_hemisphere["Max Temp"],edgecolors="black",facecolors="skyblue")
plt.title("Southern Hemisphere - Max Temp vs. Latitude")
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(-25,20),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (F)")
plt.grid (b=True,which="major",axis="both",linestyle="-",color="lightgrey")
print(f"The r-squared is: {rvalue**2}")
plt.savefig("fig6.png")
plt.show()<jupyter_output>The r-squared is: 0.2041630379044996<jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code># Generating a linear regression model of Humidity vs. Latitude on the Northern Hemisphere
x_values = northern_hemisphere["Lat"]
y_values = northern_hemisphere["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(northern_hemisphere["Lat"],northern_hemisphere["Max Temp"],edgecolors="black",facecolors="skyblue")
plt.title("Northern Hemisphere - Humidity(%) vs. Latitude")
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(10,50),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (F)")
plt.grid (b=True,which="major",axis="both",linestyle="-",color="lightgrey")
print(f"The r-squared is: {rvalue**2}")
plt.savefig("fig7.png")
plt.show()<jupyter_output>The r-squared is: 0.13141352554170868<jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code># Generating a linear regression model of Humidity vs. Latitude on the Southern Hemisphere
x_values = southern_hemisphere["Lat"]
y_values = southern_hemisphere["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(southern_hemisphere["Lat"],southern_hemisphere["Max Temp"],edgecolors="black",facecolors="skyblue")
plt.title("Southern Hemisphere - Humidity (%) vs. Latitude")
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(-25,40),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (F)")
plt.grid (b=True,which="major",axis="both",linestyle="-",color="lightgrey")
print(f"The r-squared is: {rvalue**2}")
plt.savefig("fig8.png")
plt.show()<jupyter_output>The r-squared is: 0.16566869937724474<jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code># Generating a linear regression model of Cloudiness vs. Latitude on the Northern Hemisphere
x_values = northern_hemisphere["Lat"]
y_values = northern_hemisphere["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(northern_hemisphere["Lat"],northern_hemisphere["Max Temp"],edgecolors="black",facecolors="skyblue")
plt.title("Northern Hemisphere - Cloudiness(%) vs. Latitude")
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(10,50),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (F)")
plt.grid (b=True,which="major",axis="both",linestyle="-",color="lightgrey")
print(f"The r-squared is: {rvalue**2}")
plt.savefig("fig9.png")
plt.show()<jupyter_output>The r-squared is: 0.029734845809741887<jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code># Generating a linear regression model of Cloudiness vs. Latitude on the Southern Hemisphere
x_values = southern_hemisphere["Lat"]
y_values = southern_hemisphere["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(southern_hemisphere["Lat"],southern_hemisphere["Max Temp"],edgecolors="black",facecolors="skyblue")
plt.title("Southern Hemisphere - Cloudiness (%) vs. Latitude")
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(-25,40),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (F)")
plt.grid (b=True,which="major",axis="both",linestyle="-",color="lightgrey")
print(f"The r-squared is: {rvalue**2}")
plt.savefig("fig10.png")
plt.show()<jupyter_output>The r-squared is: 0.016857311168146604<jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code># Generating a linear regression model of Wind Speed vs. Latitude on the Northern Hemisphere
x_values = northern_hemisphere["Lat"]
y_values = northern_hemisphere["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(northern_hemisphere["Lat"],northern_hemisphere["Max Temp"],edgecolors="black",facecolors="skyblue")
plt.title("Northern Hemisphere - Wind Speed(mph) vs. Latitude")
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(10,10),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (F)")
plt.grid (b=True,which="major",axis="both",linestyle="-",color="lightgrey")
print(f"The r-squared is: {rvalue**2}")
plt.savefig("fig11.png")
plt.show()<jupyter_output>The r-squared is: 0.024761841620446985<jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code># Generating a linear regression model of Wind Speed vs. Latitude on the Southern Hemisphere
x_values = southern_hemisphere["Lat"]
y_values = southern_hemisphere["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(southern_hemisphere["Lat"],southern_hemisphere["Max Temp"],edgecolors="black",facecolors="skyblue")
plt.title("Southern Hemisphere - Wind Speed(mph) vs. Latitude")
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(-25,10),fontsize=15,color="red")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (F)")
plt.grid (b=True,which="major",axis="both",linestyle="-",color="lightgrey")
print(f"The r-squared is: {rvalue**2}")
plt.savefig("fig12.png")
plt.show()<jupyter_output>The r-squared is: 0.1575626716507163
{'fraction_non_alphanumeric': 0.1384886798000588, 'fraction_numerical': 0.022463981182005294, 'mean_word_length': 5.5407692307692304, 'pattern_counts': {'":': 0, '<': 64, '<?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': '59312', 'license': 'ADSL', 'n_tokens_mistral': 6135, 'n_tokens_neox': 5540, 'n_words': 1420, 'path': 'starter_code/WeatherPy.ipynb', 'repo_name': 'abotsi1080/Python_API_Challenge', 'size': '17005'}