<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
import scipy.stats as st
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># 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 = "http://api.openweathermap.org/data/2.5/weather?"
query_url = f"{url}appid={weather_api_key}&units=imperial&q="

found_cities = []
latitudes = []
longitudes = []
max_temp = []
humidity=[]
clouds =[]
wind_speeds=[]
country= []
dates = [] 

random_cities = ['jamestown', "butaritari","yerbogachen", "cape town",'touba']
count = 0
sets = 1
print("Beginning Data Retrieval ")
print("-----------------------------")
for city in cities:
    try:
        response = requests.get(query_url+ city).json()
        latitudes.append(response['coord']['lat'])
        longitudes.append(response['coord']['lon'])
        max_temp.append(response['main']['temp_max'])
        humidity.append(response['main']['humidity'])
        clouds.append(response['clouds']['all'])
        wind_speeds.append(response['wind']['speed'])
        country.append(response['sys']['country'])
        dates.append(response['dt'])
        found_cities.append(city)
        if count >49:
            count = 1
            sets +=1
            
        else:
            count +=1
        print(f" Processing Record {count} of Set {sets} | {city}")
    except:
        print(f"City not found. Skipping...")

print("-----------------------------")
print("Data Retrieval Complete  ")
print("-----------------------------")<jupyter_output>Beginning Data Retrieval 
-----------------------------
 Processing Record 1 of Set 1 | tondano
 Processing Record 2 of Set 1 | barrow
 Processing Record 3 of Set 1 | rikitea
 Processing Record 4 of Set 1 | severo-kurilsk
 Processing Record 5 of Set 1 | kabakovo
 Processing Record 6 of Set 1 | karratha
 Processing Record 7 of Set 1 | arroio grande
 Processing Record 8 of Set 1 | isangel
 Processing Record 9 of Set 1 | port alfred
 Processing Record 10 of Set 1 | zemetchino
 Processing Record 11 of Set 1 | hithadhoo
 Processing Record 12 of Set 1 | kahului
 Processing Record 13 of Set 1 | ushuaia
 Processing Record 14 of Set 1 | punta arenas
 Processing Record 15 of Set 1 | hilo
 Processing Record 16 of Set 1 | mahebourg
 Processing Record 17 of Set 1 | dikson
 Processing Record 18 of Set 1 | kapaa
 Processing Record 19 of Set 1 | werda
 Processing Record 20 of Set 1 | codrington
 Processing Record 21 of Set 1 | san buenaventura
 Processing Record 22 of Set 1 | mataura
 Processing Recor[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code>city_data = pd.DataFrame({"City": found_cities, "Lat": latitudes, "Lng": longitudes, "Max Temp":max_temp, "Humidity":humidity, "Cloudiness":clouds, "Wind Speed":wind_speeds, "Country":country, "Date":dates})
city_data.head()
city_data.to_csv(output_data_file, index=False, header=True)
city_data.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>#find any cities that have greater then 100% humidity
more_100_humidity = city_data.loc[city_data['Humidity']>100]
print(more_100_humidity)
#no cities have greater than 100% humidity<jupyter_output>Empty DataFrame
Columns: [City, Lat, Lng, Max Temp, Humidity, Cloudiness, Wind Speed, Country, Date]
Index: []<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(city_data['Lat'], city_data['Max Temp'], alpha=0.7, edgecolors = 'black')
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
plt.title('City Latitude vs. Max Temperature (07/30/2021)')
plt.grid()
plt.savefig('output_data/fig1.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code>plt.scatter(city_data['Lat'], city_data['Humidity'], alpha=0.7, edgecolors ="black")
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.grid()
plt.title('City Latitude vs. Humidity (07/30/2021)')
plt.yticks([ 0, 20,40,60,80,100])
plt.savefig('output_data/fig2.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness Plot<jupyter_code>plt.scatter(city_data['Lat'], city_data['Cloudiness'], alpha=0.7, edgecolor ='black')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.title("City Latitude vs. Cloudiness (07/30/2021)")
plt.grid()
plt.savefig('output_data/fig3.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed Plot<jupyter_code>plt.scatter(city_data['Lat'], city_data['Wind Speed'], alpha=0.7, edgecolor ='black')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.title("City Latitude vs. Wind Speed (07/30/2021)")
plt.grid()
plt.savefig('output_data/fig4.png')
plt.show()<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code>#make northern and southern hemisphere variables
north= city_data['Lat']>0
south = city_data['Lat']<0
north_hemisphere=city_data[north]
south_hemisphere=city_data[south]<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x_values_north = north_hemisphere['Lat']
y_values_north_temp = north_hemisphere['Max Temp']

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(x_values_north, y_values_north_temp)
regress_values = x_values_north *slope +intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values_north, y_values_north_temp)
plt.plot(x_values_north, regress_values, "r-")
plt.xlabel('Latitude')
plt.ylabel('Max Temp (F)')
plt.annotate(line_eq, (10,40), fontsize=15, color = 'red')
plt.show()
print(f"The r-value is : {rvalue**2}")<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x_values_south = south_hemisphere['Lat']
y_values_south_temp = south_hemisphere['Max Temp']

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(x_values_south , y_values_south_temp)
regress_values = x_values_south  *slope +intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values_south , y_values_south_temp)
plt.plot(x_values_south , regress_values, "r-")
plt.annotate(line_eq, (-50,70), fontsize=15, color = 'red')
plt.xlabel('Latitude')
plt.ylabel('Max Temp (F)')
plt.show()
print(f"The r-value is : {rvalue**2}")<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>y_values_north_humid = north_hemisphere['Humidity']

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(x_values_north, y_values_north_humid)
regress_values = x_values_north *slope +intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values_north, y_values_north_humid)
plt.plot(x_values_north, regress_values, "r-")
plt.annotate(line_eq, (40, 25), fontsize=15, color = 'red')
plt.xlabel('Latitude')
plt.ylabel('Max Temp (F)')
plt.show()
print(f"The r-value is : {rvalue**2}")<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>y_values_south_humid = south_hemisphere['Humidity']

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(x_values_south , y_values_south_humid)
regress_values = x_values_south  *slope +intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values_south , y_values_south_humid)
plt.plot(x_values_south , regress_values, "r-")
plt.annotate(line_eq, (-50,40), fontsize=15, color = 'red')
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.show()
print(f"The r-value is : {rvalue**2}")<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>y_values_north_cloud = north_hemisphere['Cloudiness']

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(x_values_north, y_values_north_cloud)
regress_values = x_values_north *slope +intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values_north, y_values_north_cloud)
plt.plot(x_values_north, regress_values, "r-")
plt.annotate(line_eq, (40, 15), fontsize=15, color = 'red')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness(%)')
plt.show()
print(f"The r-value is : {rvalue**2}")<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>y_values_south_cloud = south_hemisphere['Cloudiness']

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(x_values_south , y_values_south_cloud)
regress_values = x_values_south  *slope +intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values_south , y_values_south_cloud)
plt.plot(x_values_south , regress_values, "r-")
plt.annotate(line_eq, (-50,32), fontsize=15, color = 'red')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.show()
print(f"The r-value is : {rvalue**2}")<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>y_values_north_wind = north_hemisphere['Wind Speed']

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(x_values_north, y_values_north_wind)
regress_values = x_values_north *slope +intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values_north, y_values_north_wind)
plt.plot(x_values_north, regress_values, "r-")
plt.annotate(line_eq, (40, 25), fontsize=15, color = 'red')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.show()
print(f"The r-value is : {rvalue**2}")<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>y_values_south_wind = south_hemisphere['Wind Speed']

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(x_values_south , y_values_south_wind)
regress_values = x_values_south  *slope +intercept
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.scatter(x_values_south , y_values_south_wind)
plt.plot(x_values_south , regress_values, "r-")
plt.annotate(line_eq, (-30, 15), fontsize=15, color = 'red')
plt.xlabel('Latitude')
plt.ylabel('Wind Speed (mph)')
plt.show()
print(f"The r-value is : {rvalue**2}")<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup