<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_keys

# Import API key
from api_keys import weather_api_key

# Incorporated citipy to determine city based on latitude and longitude --> do this for the second section of the HW
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>#URL
url = "http://api.openweathermap.org/data/2.5/weather?"

#units
units = "imperial"

#build partial query URL
#query_url = f"{url}appid={api_key}&units={units}&q="

query_url = f"{url}appid={api_key}&units={units}&q="

#api_key = weather_api_key

#api key
#api_key = "a32ced28507e51a58b1e21425d1f6817"

#lists to hold response info
city_name = []
cloudiness = []
country = []
date = []
humidity = []
lat = []
lng = []
max_temp = []
wind_speed = []

#log print statement
print(f"Beginning Data Retrieval")
print(f"-------------------------------")

# Start the counter 
counter = 1

#loop through the list of cities and perform a request for data on each
#Loop through the cities in the city list 
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"])
        city_record = response["name"]  
        
        print(f"Processing Record {counter}| {city}")
        counter = counter +1

    except:
        print("City not found. Skipping...")
    continue<jupyter_output>Beginning Data Retrieval
-------------------------------
Processing Record 1| thompson
Processing Record 2| kapaa
Processing Record 3| dinar
Processing Record 4| tuktoyaktuk
Processing Record 5| horta
Processing Record 6| saint-philippe
Processing Record 7| anloga
Processing Record 8| ushuaia
Processing Record 9| vila franca do campo
Processing Record 10| verkhoyansk
Processing Record 11| busselton
Processing Record 12| asyut
Processing Record 13| rikitea
Processing Record 14| shahrud
Processing Record 15| hasaki
Processing Record 16| chokurdakh
Processing Record 17| doha
Processing Record 18| yellowknife
City not found. Skipping...
Processing Record 19| monforte de lemos
Processing Record 20| avarua
Processing Record 21| antalaha
Processing Record 22| kruisfontein
Processing Record 23| new norfolk
Processing Record 24| banyo
Processing Record 25| poltar
Processing Record 26| hilo
Processing Record 27| anito
Processing Record 28| hithadhoo
Processing Record 29| albany
Processing Record[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code>#display the lists into a dataframe
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

#export city data into csv
city_df.to_csv('citydata.csv')

#get statistical information about the dataframe
city_df.describe()<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>#remove cites where humidity is > 100 --> create new df with cities less than 100
#based on the describe method results, the max is 100 so there isnt any city to be removed
filtered_city_df = city_df[city_df['Humidity'] < 100]
filtered_city_df
    
#Get the indices of cities that have humidity over 100%.
#Based on the describe method result there arent any cities with humidity over 100

humidity_over_100_city_df = city_df[city_df['Humidity']>100].index.tolist()
print(humidity_over_100_city_df)
# 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".

#filtered_city_df(inpalce=False)
clean_city_data = filtered_city_df
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 PlotAnalysis: As latitude increases, temperature tends to drop.<jupyter_code>#Latitude vs Temperature Scatter Plot - Matplotlib, 
#figure out how to put the date in the title

#plot style

#Scatter plot x labels, y labels, title
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (F)")
plt.title("City Latitude vs Max Temperature (2/10/21)")

plt.grid(linestyle='-', linewidth=1, alpha = 0.5)

lat_vs_temp_splot = plt.scatter(clean_city_data['Lat'], clean_city_data['Max Temp'])
plt.show()

#save plot as png
plt.savefig("../Images/City Latitude vs Max Temperature.png")<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity PlotAnalysis: As latitude gets higher, so does the humidity. Latitudes at the extremes have lower humidity.<jupyter_code>#Scatter plot x labels, y labels, title
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")
plt.title("City Latitude vs Humidity(2/10/21)")

plt.grid(linestyle='-', linewidth=1, alpha = 0.5)


lat_vs_humidity_splot = plt.scatter(clean_city_data['Lat'], clean_city_data['Humidity'])
plt.show()

#save plot as png
plt.savefig("../Images/City Latitude vs Humidity.png")<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness PlotAnalysis: Cloudiness data doest have a clear and obvious relationship to latitude. The data points are widely spread across the latitude with no real pattern.<jupyter_code>#Scatter plot x labels, y labels, title
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")
plt.title("City Latitude vs Cloudiness(2/10/21)")

plt.grid(linestyle='-', linewidth=1, alpha = 0.5)


lat_vs_cloud_splot = plt.scatter(clean_city_data['Lat'], clean_city_data['Cloudiness'])
plt.show()

#save plot as png
plt.savefig("../Images/City Latitude vs Cloudiness.png")<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed PlotAnalysis: Wind speed data doest have a clear and obvious relationship to latitude, most of the data points are under 15 mph regardless of latitude.<jupyter_code>#Scatter plot x labels, y labels, title
plt.xlabel("Latitude")
plt.ylabel("Wind Speed (mph)")
plt.title("City Latitude vs Wind Speed(2/10/21)")

plt.grid(linestyle='-', linewidth=1, alpha = 0.5)

#create scatter plot
lat_vs_wind_speed_splot = plt.scatter(clean_city_data['Lat'], clean_city_data['Wind Speed'])
plt.show()

#save plot as png
plt.savefig("../Images/City Latitude vs Wind Speed.png")<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code>#linear regress

#northern Hemisphere - Max Temp vs Latitude Linear Regression
#1. create northern hemi DF
#Northern Hemisphere (greater than or equal to 0 degrees latitude)
#2. obtain slops and b (y-intercept)
#3. add linear regression line to scatterplot

#Northern Hemi DF
north_hemi_df = city_df[city_df['Lat']>=0]
north_hemi_df

#x and y values
x = np.array(north_hemi_df['Lat'])
y = np.array(north_hemi_df['Max Temp'])<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear RegressionAnalysis: There is a weak correlation between max temperature and latitude in the northern hemisphere with the R square value being 0.17. The higher the latitude, the higher the max temperature tends to be, with enough outliers to affect the strenght of the relationship correlation.<jupyter_code>#northern Hemi Plot

#plot labels and title
plt.xlabel("Latitude")
plt.ylabel("Max Temperature")
plt.title("Northern Hemisphere - Max Temp vs. Latitude Linear Regression")

# Run linear regression
(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))

# Plot scatter plot
plt.scatter(x,y)

# Plot regression line
plt.plot(x,regress_values,"r-")

# Print r square value
print(f"R squared: {rvalue**2}")

print(line_eq)

plt.show()<jupyter_output>R squared: 0.1730115519442677
y = 0.44x + 54.69<jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear RegressionAnalysis: There is a weak correlation between max temperature and latitude in the southern hemisphere regression analysis with an R squared value of 0.177, very similar to the northern hemisphere R square value. Most of the data points are scattered throughout without any real pattern or relationship.<jupyter_code>#linear regress

#southern Hemisphere - Max Temp vs Latitude Linear Regression
#1. create northern hemi DF
#southern Hemisphere (less than 0 degrees latitude)
#2. obtain slops and b (y-intercept)
#3. add linear regression line to scatterplot

#Northern Hemi DF
south_hemi_df = city_df[city_df['Lat']<0]
south_hemi_df

#plot labels and title
plt.xlabel("Latitude")
plt.ylabel("Max Temperature")
plt.title("Southern Hemisphere - Max Temp vs. Latitude Linear Regression")

#Run linear regression
(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


#x and y values
x = np.array(south_hemi_df['Lat'])
y = np.array(south_hemi_df['Max Temp'])


#north_hemi_plot = plt.scatter(north_hemi_df['Lat'],north_hemi_df['Max Temp'])
plt.plot(x, y, "o")

#obtain m and b(intercept)
m, b = np.polyfit(x, y, 1)

#add linear regression line to scatter plot
plt.plot(x, m*x+b)

#Print r square value
print(f"R squared: {rvalue**2}")

print(line_eq)

plt.show()<jupyter_output>R squared: 0.17728936780623547
y = 0.27x + 80.11<jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear RegressionAnalysis: There is a weak relationship between humidity and latitude in the Northern hemisphere. The lowest humidity values are closer to 0* latitude as well.<jupyter_code>#linear regress

#northern Hemisphere - Humidity vs Latitude Linear Regression
#1. create northern hemi DF
#Northern Hemisphere (greater than or equal to 0 degrees latitude)
#2. obtain slops and b (y-intercept)
#3. add linear regression line to scatterplot

#x and y values
x = np.array(north_hemi_df['Lat'])
y = np.array(north_hemi_df['Humidity'])

#Run linear regression
(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


#plot labels and title
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")
plt.title("Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression")

plt.plot(x, y, "o")

#obtain m and b(intercept)
m, b = np.polyfit(x, y, 1)

#add linear regression line to scatter plot
plt.plot(x, m*x+b)

#Print r square value
print(f"R squared: {rvalue**2}")

print(line_eq)

plt.show()<jupyter_output>R squared: 0.1730115519442677
y = 0.44x + 54.69<jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear RegressionAnalysis: There is no real correlation between humidity and latitude in the Southern Hemisphere. Most of the data is pretty spread out, with a slight trend of higher humidity as latitude increases.<jupyter_code>#linear regress

#southern Hemisphere - Humidity vs Latitude Linear Regression
#1. create northern hemi DF
#southern Hemisphere (less than 0 degrees latitude)
#2. obtain slops and b (y-intercept)
#3. add linear regression line to scatterplot

#Northern Hemi DF
south_hemi_df = city_df[city_df['Lat']<0]
south_hemi_df

#plot labels and title
plt.xlabel("Latitude")
plt.ylabel("Humidity(%)")
plt.title("Southern Hemisphere - Humidity(%) vs. Latitude Linear Regression")

#Run linear regression
(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


#x and y values
x = np.array(south_hemi_df['Lat'])
y = np.array(south_hemi_df['Humidity'])


plt.plot(x, y, "o")

#obtain m and b(intercept)
m, b = np.polyfit(x, y, 1)

#add linear regression line to scatter plot
plt.plot(x, m*x+b)

#Print r square value
print(f"R squared: {rvalue**2}")

print(line_eq)

plt.show()<jupyter_output>R squared: 0.1254906271772586
y = 0.46x + 85.63<jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear RegressionAnalysis: There is a weak correlation between cloudiness and latitude in the Northern Hemisphere. Most of the data points are at the extremes of the cloudiness axis regardless of the latitude value.<jupyter_code>#linear regress

#northern Hemisphere - Cloudiness (%)  vs Latitude Linear Regression
#1. create northern hemi DF
#Northern Hemisphere (greater than or equal to 0 degrees latitude)
#2. obtain slops and b (y-intercept)
#3. add linear regression line to scatterplot

#x and y values
x = np.array(north_hemi_df['Lat'])
y = np.array(north_hemi_df['Cloudiness'])

#plot labels and title
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")
plt.title("Northern Hemisphere - Cloudiness(%) vs. Latitude Linear Regression")

#Run linear regression
(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


#north_hemi_plot = plt.scatter(north_hemi_df['Lat'],north_hemi_df['Max Temp'])
plt.plot(x, y, "o")

#obtain m and b(intercept)
m, b = np.polyfit(x, y, 1)

#add linear regression line to scatter plot
plt.plot(x, m*x+b)

#Print r square value
print(f"R squared: {rvalue**2}")

print(line_eq)

plt.show()<jupyter_output>R squared: 0.02515288574804646
y = 0.32x + 42.34<jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear RegressionAnalysis: There isnt a strong relationship between cloudiness and latitude in the Southern hemisphere. The data points are scattered thoughout.<jupyter_code>#southern Hemisphere - Cloudiness vs Latitude Linear Regression
#1. create northern hemi DF
#southern Hemisphere (less than 0 degrees latitude)
#2. obtain slops and b (y-intercept)
#3. add linear regression line to scatterplot

#Southern Hemi DF
south_hemi_df = city_df[city_df['Lat']<0]
south_hemi_df

#plot labels and title
plt.xlabel("Latitude")
plt.ylabel("Cloudiness(%)")
plt.title("Southern Hemisphere - Cloudiness(%) vs. Latitude Linear Regression")

#Run linear regression
(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


#x and y values
x = np.array(south_hemi_df['Lat'])
y = np.array(south_hemi_df['Cloudiness'])


#north_hemi_plot = plt.scatter(north_hemi_df['Lat'],north_hemi_df['Max Temp'])
plt.plot(x, y, "o")

#obtain m and b(intercept)
m, b = np.polyfit(x, y, 1)

#add linear regression line to scatter plot
plt.plot(x, m*x+b)

#Print r square value
print(f"R squared: {rvalue**2}")

print(line_eq)

plt.show()<jupyter_output>R squared: 0.20614546388537183
y = 1.3x + 83.77<jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear RegressionAnalysis: There is a very strong neutral relationship between wind speed and latitude in the northern hemisphere.<jupyter_code>#linear regress

#northern Hemisphere - Wind Speed (%)  vs Latitude Linear Regression
#1. create northern hemi DF
#Northern Hemisphere (greater than or equal to 0 degrees latitude)
#2. obtain slops and b (y-intercept)
#3. add linear regression line to scatterplot

#x and y values
x = np.array(north_hemi_df['Lat'])
y = np.array(north_hemi_df['Wind Speed'])

#Run linear regression
(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


#plot labels and title
plt.xlabel("Latitude")
plt.ylabel("Wind Speed (mph)")
plt.title("Northern Hemisphere - Wind Speed(mph) vs. Latitude Linear Regression")

#north_hemi_plot = plt.scatter(north_hemi_df['Lat'],north_hemi_df['Max Temp'])
plt.plot(x, y, "o")

#obtain m and b(intercept)
m, b = np.polyfit(x, y, 1)

#add linear regression line to scatter plot
plt.plot(x, m*x+b)

#Print r square value
print(f"R squared: {rvalue**2}")

print(line_eq)

plt.show()<jupyter_output>R squared: 4.029617385018137e-05
y = -0.0x + 3.71<jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear RegressionAnalysis: There is a weak negative correlation between wind speed and latitude in the Southern hemisphere.<jupyter_code>#southern Hemisphere - Wind Speed vs Latitude Linear Regression
#1. create southern hemi DF
#southern Hemisphere (less than 0 degrees latitude)
#2. obtain slops and b (y-intercept)
#3. add linear regression line to scatterplot

#Southern Hemi DF
south_hemi_df = city_df[city_df['Lat']<0]
south_hemi_df

#plot labels and title
plt.xlabel("Latitude")
plt.ylabel("Wind Speed(mph)")
plt.title("Southern Hemisphere - Wind Speed(mph) vs. Latitude Linear Regression")

#Run linear regression
(slope, intercept, rvalue, pvalue, stderr) = linregress(x, y)
regress_values = x * slope + intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))


#x and y values
x = np.array(south_hemi_df['Lat'])
y = np.array(south_hemi_df['Wind Speed'])


plt.plot(x, y, "o")

#obtain m and b(intercept)
m, b = np.polyfit(x, y, 1)

#add linear regression line to scatter plot
plt.plot(x, m*x+b)

#Print r square value
print(f"R squared: {rvalue**2}")

print(line_eq)

plt.show()<jupyter_output>R squared: 0.09129219590713976
y = -0.13x + 5.23
{'fraction_non_alphanumeric': 0.11426319936958235, 'fraction_numerical': 0.022852639873916468, 'mean_word_length': 5.268027658873889, 'pattern_counts': {'":': 9, '<': 69, '<?xml version=': 0, '>': 71, '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': '199871', 'license': 'ADSL', 'n_tokens_mistral': 6979, 'n_tokens_neox': 6323, 'n_words': 2056, 'path': 'Instructions/starter_code/WeatherPy.ipynb', 'repo_name': 'zulumanf/Python-API', 'size': '19035'}