<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>###Three observations
# 1. Cloudiness seems to be the least correlated to latitude 
# 2. The strongest correlation is max temp proving that it does get hotter towards the equator
# 3. The cities with the perfect tempeture for vacationpy all fall within 30 degrees lng from the equator
# 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 openweathermapy as ow


# 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(low=-90.000, high=90.000, size=1500)
lngs = np.random.uniform(low=-180.000, high=180.000, 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>#base URL and keys to call from data frame
base_url = "http://api.openweathermap.org/data/2.5/weather?units=Imperial&APPID=" + weather_api_key
setting = {'units':'Imperial','appid':weather_api_key}
desired_keys = ['clouds.all','sys.country','dt','main.humidity','main.temp_max','wind.speed','coord.lat','coord.lon']


# Create new lists for data frame
cloudiness = []
country = []
date = []
humidity = []
max_temp = []
wind_speed = []
lat =[]
lng =[]
cities_list = []

#Print beginning and start count
print('Beginning Data Retrieval')
print('---------------------')
count = 1 
err=0
#loop through ciities in list and grab data to append lists
for city in cities:
    city_url = base_url + '&q=' + city.replace(' ','+')
    print(f'Processing record # {count} | {city}')
   # print(city_url)
    try:
        city_data = ow.get_current(city,**setting)
        cl,ct,dt,hm,mt,ws,lt,ln = city_data(*desired_keys)
        cloudiness.append(cl)
        country.append(ct)
        date.append(dt)
        humidity.append(hm)
        max_temp.append(mt)
        wind_speed.append(ws)
        lat.append(lt)
        lng.append(ln)
        cities_list.append(city)
    except Exception as e:
        print('City not found. skipping...')
        err=err+1
    count = count+1
    
    
#print errors and end

print("end of loop")
print(f'Could not get data for {err} cities')<jupyter_output>Beginning Data Retrieval
---------------------
Processing record # 1 | yellowknife
Processing record # 2 | albany
Processing record # 3 | tsihombe
City not found. skipping...
Processing record # 4 | belushya guba
City not found. skipping...
Processing record # 5 | souillac
Processing record # 6 | tasiilaq
Processing record # 7 | cape town
Processing record # 8 | bluff
Processing record # 9 | mount gambier
Processing record # 10 | saint-philippe
Processing record # 11 | lebu
Processing record # 12 | puerto ayora
Processing record # 13 | loiza
Processing record # 14 | busselton
Processing record # 15 | waingapu
Processing record # 16 | ust-nera
Processing record # 17 | port alfred
Processing record # 18 | tuktoyaktuk
Processing record # 19 | tiksi
Processing record # 20 | launceston
Processing record # 21 | mackay
Processing record # 22 | fortuna
Processing record # 23 | faanui
Processing record # 24 | karlstad
Processing record # 25 | codrington
Processing record # 26 | ponta do sol
Pro[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code># create data frane count records in data frame
weather_data = pd.DataFrame({
   'City':cities_list,'Cloudiness':cloudiness,'Country':country, 'Date':date,'Humidity':humidity,'Latitude':lat,
    'Longitude':lng,'Max Temperature':max_temp,'Wind Speed':wind_speed,})
weather_data.count()
#save data frame in a csv file
weather_data.to_csv("weather_data.csv")

#print data frame pandas
weather_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>#scatter plot to compare lat and temp
import datetime

plt.scatter(weather_data["Latitude"], weather_data["Max Temperature"], color='purple', alpha=0.5)
plt.xlabel('Latitude')
plt.ylabel('Max Temperature (F)')
Timestamp = datetime.datetime.now()
plt.title(f'City Latitude vs Max Temperature {Timestamp}')
plt.savefig("LatVsTemp.png")
plt.show()
print('This graph shows the correlation between a latitude and max temperature')<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code>#Scatter plot lat vs humidity
plt.scatter(weather_data["Latitude"], weather_data["Humidity"], color='coral', alpha=0.5)
plt.xlabel('Latitude')
plt.ylabel('Humidity')
plt.title(f'City Latitude vs Humidity {Timestamp}')
plt.savefig("LatVsHum.png")
plt.show()
print('This graph shows the correlation between a latitude and humidity')<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness Plot<jupyter_code>#scatter plot lat vs cloudiness
plt.scatter(weather_data["Latitude"], weather_data["Cloudiness"], color='darkslategray', alpha=0.5)
plt.xlabel('Latitude')
plt.ylabel('Cloudiness')
plt.title(f'City Latitude vs Cloudiness {Timestamp}')
plt.savefig("LatVscloud.png")
plt.show()
print('This graph shows the correlation between a latitude and cloudiness.')<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed Plot<jupyter_code>#Scatter Plot Lat vs wind speed
plt.scatter(weather_data["Latitude"], weather_data["Wind Speed"], color='yellowgreen', alpha=0.5)
plt.xlabel('Latitude')
plt.ylabel('Wind Speed')
plt.title(f'City Latitude vs Wind Speed {Timestamp}')
plt.savefig("LatVsWS.png")
plt.show()
print('This graph shows the correlation between a latitude and wind speed.')<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code># OPTIONAL: Create a function to create Linear Regression plots

# Create Northern Hemisphere DataFrames
Northern = weather_data[weather_data["Latitude"] >= 0]
Northern
#Create Southern DataFrame
Southern = weather_data[weather_data["Latitude"] < 0]
Southern<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>#import scripy set columns for variables lat north and max temp north
import scipy.stats as st
MaxTempNorth = Northern.iloc[:,7]
LatNorth = Northern.iloc[:,5]

#Run statistical analysis on two variables
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(MaxTempNorth, LatNorth)

#Create regress line formula print correlation and plot with regress line and equation
regress_values = MaxTempNorth * slope + intercept
print(f"The r-squared is {rvalue}")
plt.scatter(MaxTempNorth,LatNorth)
plt.plot(MaxTempNorth,regress_values,"r-")
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_eq,(2,1),fontsize=12,color="red")
plt.title("Northern Hemisphere Max Temp vs Latitude")
plt.xlabel('Max Temp (F)')
plt.ylabel('Latitude')
plt.show()<jupyter_output>The r-squared is -0.8650050897677715<jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>MaxTempSouth = Southern.iloc[:,7]
LatSouth = Southern.iloc[:,5]

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(MaxTempSouth, LatSouth)
print(f"The r-squared is {rvalue}")
regress_values = MaxTempSouth * slope + intercept
plt.scatter(MaxTempSouth,LatSouth)
plt.plot(MaxTempSouth,regress_values,"r-")
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_eq,(80,-50),fontsize=12,color="red")
plt.title("Southern Hemisphere Max Temp vs Latitude")
plt.xlabel('Max Temp (F)')
plt.ylabel('Latitude')
#plt.show()<jupyter_output>The r-squared is 0.5748908892067903<jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>#loc new variable 
HumidityNorth = Northern.iloc[:,4]
#Create plot details and perform analysis 
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(HumidityNorth, LatNorth)
print(f"The r-squared is {rvalue}")
regress_values = HumidityNorth * slope + intercept
plt.scatter(HumidityNorth,LatNorth)
plt.plot(HumidityNorth,regress_values,"r-")
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_eq,(20,70),fontsize=12,color="red")
plt.title("Northern Hemisphere Humidity vs Latitude")
plt.xlabel('Humidity (%)')
plt.ylabel('Latitude')
plt.show()<jupyter_output>The r-squared is 0.44964872302888853<jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>#Loc new varaible
HumiditySouth = Southern.iloc[:,4]

#Create new plot and perform analysis 
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(HumiditySouth, LatSouth)
print(f"The r-squared is {rvalue}")

regress_values = HumiditySouth * slope + intercept
plt.scatter(HumiditySouth,LatSouth)
plt.plot(HumiditySouth,regress_values,"r-")
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_eq,(20,-10),fontsize=12,color="red")
plt.title("Southern Hemisphere Humidity vs Latitude")
plt.xlabel('Humidity (%)')
plt.ylabel('Latitude')
plt.show()<jupyter_output>The r-squared is 0.2054066917773568<jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>#loc new variable preform analysis and create plot
CloudNorth = Northern.iloc[:,1]
(slope, intercept, rvalue, pvalue, stderr) = st.linregress(CloudNorth, LatNorth)
print(f"The r-squared is {rvalue}")
regress_values = CloudNorth * slope + intercept
plt.scatter(CloudNorth,LatNorth)
plt.plot(CloudNorth,regress_values,"r-")
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_eq,(18,76),fontsize=12,color="red")
plt.title("Northern Hemisphere Cloudiness vs Latitude")
plt.xlabel('Cloudiness (%)')
plt.ylabel('Latitude')
plt.show()<jupyter_output>The r-squared is 0.31248142573176835<jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>#loc new variable preform analysis and create plot
CloudSouth = Southern.iloc[:,1]

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(CloudSouth, LatSouth)
print(f"The r-squared is {rvalue}")
regress_values = CloudSouth * slope + intercept
plt.scatter(CloudSouth,LatSouth)
plt.plot(CloudSouth,regress_values,"r-")
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_eq,(20,-50),fontsize=12,color="red")
plt.title("Southern Hemisphere Cloudiness vs Latitude")
plt.xlabel('Cloudiness (%)')
plt.ylabel('Latitude')
plt.show()<jupyter_output>The r-squared is 0.27098335673670054<jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>#loc new variable preform analysis and create plot
WindNorth = Northern.iloc[:,8]

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(WindNorth, LatNorth)
print(f"The r-squared is {rvalue}")
regress_values = WindNorth * slope + intercept
plt.scatter(WindNorth,LatNorth)
plt.plot(WindNorth,regress_values,"r-")
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_eq,(26,20),fontsize=12,color="red")
plt.title("Northern Hemisphere Wind Speed vs Latitude")
plt.xlabel('Wind Speed (MPH)')
plt.ylabel('Latitude')
plt.show()<jupyter_output>The r-squared is 0.055851473657986365<jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>#loc new variable preform analysis and create plot
WindSouth = Southern.iloc[:,8]

(slope, intercept, rvalue, pvalue, stderr) = st.linregress(WindSouth, LatSouth)
print(f"The r-squared is {rvalue}")
regress_values = WindSouth * slope + intercept
plt.scatter(WindSouth,LatSouth)
plt.plot(WindSouth,regress_values,"r-")
line_eq = "y = " + str(round(slope,2)) + "x + " + str(round(intercept,2))
plt.annotate(line_eq,(18,-10),fontsize=12,color="red")
plt.title("Southern Hemisphere Wind Speed vs Latitude")
plt.xlabel('Wind Speed (MPH)')
plt.ylabel('Latitude')
plt.show()<jupyter_output>The r-squared is -0.30389630518988386
{'fraction_non_alphanumeric': 0.12689422929741315, 'fraction_numerical': 0.024491045461503137, 'mean_word_length': 6.013955984970478, 'pattern_counts': {'":': 0, '<': 61, '<?xml version=': 0, '>': 61, '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': '17', 'dir': 'jupyter-structured-clean-dedup', 'id': '605347', 'license': 'ADSL', 'n_tokens_mistral': 4830, 'n_tokens_neox': 4343, 'n_words': 1135, 'path': 'starter_code/WeatherPy.ipynb', 'repo_name': 'kristine848/API_Homework', 'size': '13066'}