<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># Import all the dependencies

import matplotlib.pyplot as plt
import pandas as pd
import scipy.stats as st
from scipy.stats import linregress
import requests
import json
import random
from pprint import pprint
from api_keys import weather_api_key
from api_keys import g_key
from citipy import citipy

import numpy as np<jupyter_output><empty_output><jupyter_text>Generate Cities List<jupyter_code>#generate random list for latitudes and longitudes 

latitude = []
longitude = []

for lat in np.random.uniform(-90,90,1800):
    latitude.append(lat)

for lng in np.random.uniform(-180,180,1800):
    longitude.append(lng)
# Zip the latitude and longtitude combination
city_location = []

city_location = zip(latitude, longitude)
# Find the nearest city by using citipy

cities_list = []

for location in city_location:
    
    city = citipy.nearest_city(location[0], location[1]).city_name
    
    cities_list.append(city)
# Create a DataFrame and remove all the duplicate cities
city_df = pd.DataFrame({"City": cities_list}).drop_duplicates(subset=['City'])

# Check the number of unique cities left in the DataFrame
len(city_df)<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># Weather check, save base_url and the units for temperature

base_url = "http://api.openweathermap.org/data/2.5/weather?"

units = "imperial"

# Build query_url 

query_url = f"{base_url}appid={weather_api_key}&units={units}&q="

API_cities = city_df["City"]

# The list of variables we're trying to retrieve from the API
# Create empty list to store them

City = []
Lat = []
Lng = []
Max_Temp = []
Humidity = []
Cloudiness = []
Wind_Speed = []
Country = []
Date = []

# Set up Count to print for the log

count = 1

# The beginning of the log

print("Beginning Data Retrieval     ")
print("-----------------------------")

# Loop through the list in cities in the city_df and perform the request for data on each of the cities

for city in API_cities:
    
    response = requests.get(query_url + city).json()
    
    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'])
        City.append(city)
        
        print(f"Processing Record {count} | {city}")
        
        count += 1
    
    except:
        
        print(f"Country information not found.")
        
        
print("-----------------------------")
print("Data Retrieval Complete")
print("-----------------------------")<jupyter_output>Beginning Data Retrieval     
-----------------------------
Country information not found.
Processing Record 1 | saint-philippe
Processing Record 2 | nizhniy kuranakh
Processing Record 3 | rikitea
Processing Record 4 | arraial do cabo
Processing Record 5 | bilibino
Processing Record 6 | benatky nad jizerou
Processing Record 7 | jumla
Processing Record 8 | cape town
Processing Record 9 | tiksi
Processing Record 10 | port alfred
Processing Record 11 | yellowknife
Processing Record 12 | carnarvon
Processing Record 13 | siena
Processing Record 14 | atuona
Processing Record 15 | mondovi
Processing Record 16 | rosita
Country information not found.
Processing Record 17 | kavieng
Processing Record 18 | sturgeon falls
Processing Record 19 | atasu
Processing Record 20 | castro
Processing Record 21 | vostok
Processing Record 22 | shenjiamen
Processing Record 23 | omboue
Processing Record 24 | bethel
Processing Record 25 | conde
Processing Record 26 | kaitangata
Processing Record 27 | ushuaia
Proc[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code># Create a DataFrame for the loop outputs (country weather information)

weather_df = pd.DataFrame ({
    "City":City,
    "Lat":Lat,
    "Lng":Lng,
    "Max Temp":Max_Temp,
    "Humidity":Humidity,
    "Cloudiness":Cloudiness,
    "Wind Speed":Wind_Speed,
    "Country":Country,
    "Date":Date})
# Export the DataFrame to a csv.

weather_df.to_csv('weather_df.csv')

# Preview DataFrame
weather_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>#  Get the indices of cities that have humidity over 100%.
humidity_outlier = weather_df.loc[weather_df["Humidity"] > 100]
humidity_outlier
# 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".
weather_clean = weather_df.drop(weather_df[weather_df["Humidity"] >100].index, inplace = False)
weather_clean.head()
# Check the number of rows in the clean datafram

print(len(weather_clean))<jupyter_output>630<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># Plot City Latitude vs. Max Temperature

x_axis = weather_clean["Lat"]
y_axis = weather_clean["Max Temp"]

plt.scatter(x_axis, y_axis, marker = "o", facecolors = "steelblue", edgecolors = "black", s = 30)

plt.title("City Latitude vs. Max Temperature (03/16/21)")
plt.xlabel("Latitude")
plt.ylabel("Max Temperature (F)")
plt.grid()

plt.savefig("Latitude vs. Temperature Plot.png")

plt.show()<jupyter_output><empty_output><jupyter_text>The above plot is showing the relationship between city latitude and their max temperature. From the plot we can see a trend that when the latitude move towards 0 the max temperature increases, and when the latitude moves away from 0 the max temperature start to decrease again.  Latitude vs. Humidity Plot<jupyter_code>x_axis = weather_clean["Lat"]
y_axis = weather_clean["Humidity"]

plt.scatter(x_axis, y_axis, marker = "o", facecolors = "steelblue", edgecolors = "black", s = 30)

plt.title("City Latitude vs. Humidity (03/16/21)")
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")
plt.grid()

plt.savefig("Latitude vs. Humidity Plot.png")

plt.show()<jupyter_output><empty_output><jupyter_text>The plot is trying to identify the relationship between city latitude and city humidity, trying to discover the impact of latitude on the humidity.  Latitude vs. Cloudiness Plot<jupyter_code>x_axis = weather_clean["Lat"]
y_axis = weather_clean["Cloudiness"]

plt.scatter(x_axis, y_axis, marker = "o", facecolors = "steelblue", edgecolors = "black", s = 30)

plt.title("City Latitude vs. Cloudiness (03/16/21)")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")
plt.grid()

plt.savefig("Latitude vs. Cloudiness Plot.png")

plt.show()<jupyter_output><empty_output><jupyter_text>The above plot is trying to discover the relationship between city latitude and their cloud coverage.  Latitude vs. Wind Speed Plot<jupyter_code>x_axis = weather_clean["Lat"]
y_axis = weather_clean["Wind Speed"]

plt.scatter(x_axis, y_axis, marker = "o", facecolors = "steelblue", edgecolors = "black", s = 30)

plt.title("City Latitude vs. Wind Speed (03/16/21)")
plt.xlabel("Latitude")
plt.ylabel("Wind Speed (mph)")
plt.grid()

plt.savefig("Latitude vs. Wind Speed Plot.png")

plt.show()<jupyter_output><empty_output><jupyter_text>The last plot is plotting the relationship between latitude and wind speed, trying to identify if the wind speed is different when the city is located in different latitude location.  Linear Regression<jupyter_code># Create new DataFrame for Northern & Souther Hemisphere
# Northern Hemisphere (greater than or equal to 0 degrees latitude) and
# Southern Hemisphere (less than 0 degrees latitude):

northern = weather_clean.loc[weather_clean["Lat"] >= 0]

southern = weather_clean.loc[weather_clean["Lat"] < 0]

northern.head()

print(len(northern))
southern.head()

print(len(southern))<jupyter_output>190<jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x_values = northern["Lat"]
y_values = northern["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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(15,-25),fontsize=15,color="red")

plt.title("Northern Hemisphere - Max Temp vs. Latitude")
plt.xlabel("Latitude")
plt.ylabel("Max Temp")

plt.savefig("Northern Hemisphere - Max Temp vs. Latitude.png")

plt.show()

print(f"The r-squared is: {rvalue**2}")<jupyter_output><empty_output><jupyter_text>From the scatter plot and the linear regression, we could conclude in the Northern hemisphere there is a strong negative correlation between latitude and max temperature. When the latitude increases, the max temperature decreases or in other words the weather become colder.   Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x_values = southern["Lat"]
y_values = southern["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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(-40,45),fontsize=15,color="red")

plt.title("Southern Hemisphere - Max Temp vs. Latitude")
plt.xlabel("Latitude")
plt.ylabel("Max Temp")

plt.savefig("Southern Hemisphere - Max Temp vs. Latitude.png")

plt.show()

print(f"The r-squared is: {rvalue**2}")<jupyter_output><empty_output><jupyter_text>Opposite to the result we saw in the Northern hemisphere, in Southern hemisphere there is a weak positive correlation relationship between latitude and max temperature.   Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>x_values = northern["Lat"]
y_values = northern["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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(45,12),fontsize=15,color="red")

plt.title("Northern Hemisphere - Humidity (%) vs. Latitude")
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")

plt.savefig("Northern Hemisphere - Humidity (%) vs. Latitude.png")

plt.show()

print(f"The r-squared is: {rvalue**2}")<jupyter_output><empty_output><jupyter_text>When looking at the linear regression between Latitude and humidity level in the Northern hemisphere, there is no correlation relationship.   Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>x_values = southern["Lat"]
y_values = southern["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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(-55,30),fontsize=15,color="red")

plt.title("Southern Hemisphere - Humidity (%) vs. Latitude")
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")

plt.savefig("Southern Hemisphere - Humidity (%) vs. Latitude.png")

plt.show()

print(f"The r-squared is: {rvalue**2}")<jupyter_output><empty_output><jupyter_text>In the Southern hemisphere, there is no correlation relationship between latitude and humidity level.   Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x_values = northern["Lat"]
y_values = northern["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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(40,5),fontsize=15,color="red")

plt.title("Northern Hemisphere - Cloudiness (%) vs. Latitude")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")

plt.savefig("Northern Hemisphere - Cloudiness (%) vs. Latitude.png")

plt.show()

print(f"The r-squared is: {rvalue**2}")<jupyter_output><empty_output><jupyter_text>In the Northern hemisphere, there is no correlation relationship between the cloud coverage and latitude. From the plot we could see the cloudiness are spreaded out across all different latitude.   Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x_values = southern["Lat"]
y_values = southern["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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(-50,10),fontsize=15,color="red")

plt.title("Southern Hemisphere - Cloudiness (%) vs. Latitude")
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")

plt.savefig("Southern Hemisphere - Cloudiness (%) vs. Latitude.png")

plt.show()

print(f"The r-squared is: {rvalue**2}")<jupyter_output><empty_output><jupyter_text>Again, if we look at the relationship between cloud coverage and latitude in the Southern hemisphere, there is also no correlation relationship between the two variables.   Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x_values = northern["Lat"]
y_values = northern["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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(10,30),fontsize=15,color="red")

plt.title("Northern Hemisphere - Wind Speed (mph) vs. Latitude")
plt.xlabel("Latitude")
plt.ylabel("Wind Speed (mph)")

plt.savefig("Northern Hemisphere - Wind Speed (mph) vs. Latitude.png")

plt.show()

print(f"The r-squared is: {rvalue**2}")<jupyter_output><empty_output><jupyter_text>Lastly, we want to look at the relationship between latitude & wind speed in the cities in the Northern hemisphere. The plot and the r value is stating that there is no correlation relationship between them.   Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x_values = southern["Lat"]
y_values = southern["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)
plt.plot(x_values,regress_values,"r-")
plt.annotate(line_eq,(-50,17),fontsize=15,color="red")

plt.title("Southern Hemisphere - Wind Speed (mph) vs. Latitude")
plt.xlabel("Latitude")
plt.ylabel("Wind Speed (mph)")

plt.savefig("Southern Hemisphere - Wind Speed (mph) vs. Latitude.png")

plt.show()

print(f"The r-squared is: {rvalue**2}")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12619747094137182, 'fraction_numerical': 0.012709158257759612, 'mean_word_length': 5.321760193782802, 'pattern_counts': {'":': 10, '<': 71, '<?xml version=': 0, '>': 75, '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': '292283', 'license': 'ADSL', 'n_tokens_mistral': 5436, 'n_tokens_neox': 4999, 'n_words': 1440, 'path': 'WeatherPy/WeatherPy.ipynb', 'repo_name': 'Rain-Yang18/python-api-challenge', 'size': '15658'}