<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)
!pip install citipy<jupyter_output>Requirement already satisfied: citipy in c:\users\dhruv\anaconda3\lib\site-packages (0.0.5)
Requirement already satisfied: kdtree>=0.12 in c:\users\dhruv\anaconda3\lib\site-packages (from citipy) (0.16)<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>#starting url for Weather Map API call
url = f"http://api.openweathermap.org/data/2.5/weather?units=Imperial&APPID={weather_api_key}"

#list of city data
city_data = []

#print to logger
print("Beginning Data Retrieval")
print("-" * 15)

# Create counters
record_count = 1
set_count = 1

# loop through all the cities in out list
for index, city in enumerate(cities):

    # Group cities in sets of 50 for logging purpose
    if (index % 50 == 0 and index >=50):
        set_count += 1
        record_count = 0
    # Create endpoint URL with each city
    city_url = url + "&q=" + city
    
    # Log the url record and set number
    print(f"Processing Record {record_count} of Set {set_count} | {city}")
    
    record_count += 1
    
    # Run an API reqest for each of the cities
    try: 
        # Parse the JSON and retrieve data
        city_weather = requests.get(city_url).json()
        #print(city_weather)
        #break
        # Extract out max temp and humidity and cloudiness
        city_lat = city_weather["coord"]["lat"]
        #print("lat", city_lat) 
        city_lng = city_weather["coord"]["lon"]
        #print("coord", "lon", city_lng)
        city_max_temp = city_weather["main"]["temp_max"]
        #print("main", "temp_max", city_max_temp)
        city_humidity = city_weather["main"]["humidity"]
        #print("Clouds", "all", city_humidity)
        city_clouds = city_weather["clouds"]["all"]
        #print("Clouds", "all", city_clouds)
        city_wind = city_weather["wind"]["speed"]
        #print("Wind", "speed", city_wind)
        city_country = city_weather["sys"]["country"]
        #print("sys", "country", city_country)
        city_date = city_weather["dt"]
        #print("dt", city_date)
        
        # Append the City info into city data list
        city_data.append({
            "City":city,
            "Lat": city_lat,
            "Lng": city_lng,
            "Max Temp": city_max_temp,
            "Humidity": city_humidity,
            "Cloudiness": city_clouds,
            "Wind Speed": city_wind,
            "Country": city_country,
            "Date": city_date 
        })
        
    except:
        print("City not found, Skipping...")
        pass
# Indicate that Data loading is complete
print("----------------")
print("Data Retrieval Complete")
print("----------------")<jupyter_output>Beginning Data Retrieval
---------------
Processing Record 1 of Set 1 | kodinsk
Processing Record 2 of Set 1 | hithadhoo
Processing Record 3 of Set 1 | coulihaut
City not found, Skipping...
Processing Record 4 of Set 1 | upernavik
Processing Record 5 of Set 1 | bismarck
Processing Record 6 of Set 1 | avarua
Processing Record 7 of Set 1 | leningradskiy
Processing Record 8 of Set 1 | dikson
Processing Record 9 of Set 1 | oktyabrskoye
Processing Record 10 of Set 1 | belushya guba
City not found, Skipping...
Processing Record 11 of Set 1 | carnarvon
Processing Record 12 of Set 1 | hobart
Processing Record 13 of Set 1 | lazurne
Processing Record 14 of Set 1 | nanortalik
Processing Record 15 of Set 1 | northam
Processing Record 16 of Set 1 | severo-kurilsk
Processing Record 17 of Set 1 | bud
Processing Record 18 of Set 1 | saint-philippe
Processing Record 19 of Set 1 | taltal
Processing Record 20 of Set 1 | codrington
Processing Record 21 of Set 1 | arraial do cabo
Processing Record 22 of Se[...]<jupyter_text>Convert Raw Data to DataFrame* Export the city data into a .csv.* Display the DataFrame<jupyter_code># Convert array of JSON into pandas
city_data_df = pd.DataFrame(city_data)

# Extract relevant field from the dataframe
lats = city_data_df["Lat"]
max_temps = city_data_df["Max Temp"]
humidity = city_data_df["Humidity"]
cloudiness = city_data_df["Cloudiness"]
wind_speed = city_data_df["Wind Speed"]

city_data_df.to_csv(output_data_file, index_label="City_ID")

city_data_df.count()
# Display the City DataFrame
city_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>#  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># Build Scatter plot for latitude vs. temperature
plt.scatter(lats,
           max_temps,
           edgecolor="black", linewidth=1, marker="o",
           alpha=.8, label="Cities")

#Incorporate other graph properties
plt.title("City Latitude vs. Max Temperature (%s)" % time.strftime("%x"))
plt.ylabel("Max Temperature (F)")
plt.xlabel("Latitude")
plt.grid(True)

#Save the figure
plt.savefig("output_data/Fig1.png")

# Show Plot
plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Humidity Plot<jupyter_code>plt.scatter(lats,
           humidity, 
           edgecolor="black", linewidth=1, marker="o",
           alpha=.8, label="Cities")

# Incorporate other graph properties
plt.title("City Latitude vs. Humidity (%s)" % time.strftime("%x"))
plt.ylabel("Humidity (%)")
plt.xlabel("Latitude")
plt.grid(True)

#Save the figure
plt.savefig("output_data/Fig2.png")

# Show Plot
plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Cloudiness Plot<jupyter_code>plt.scatter(lats,
           cloudiness, 
           edgecolor="black", linewidth=1, marker="o",
           alpha=.8, label="Cities")

# Incorporate other graph properties
plt.title("City Latitude vs. Cloudiness (%s)" % time.strftime("%x"))
plt.ylabel("Cloudiness (%)")
plt.xlabel("Latitude")
plt.grid(True)

#Save the figure
plt.savefig("output_data/Fig3.png")

# Show Plot
plt.show()<jupyter_output><empty_output><jupyter_text>Latitude vs. Wind Speed Plot<jupyter_code>plt.scatter(lats,
           wind_speed, 
           edgecolor="black", linewidth=1, marker="o",
           alpha=.8, label="Cities")

# Incorporate other graph properties
plt.title("City Latitude vs. Wind Speed (%s)" % time.strftime("%x"))
plt.ylabel("Wind Speed mph")
plt.xlabel("Latitude")
plt.grid(True)

#Save the figure
plt.savefig("output_data/Fig4.png")

# Show Plot
plt.show()<jupyter_output><empty_output><jupyter_text>Linear Regression<jupyter_code>def plot_linear_regression(x_values, y_values, title, text_coordinates):
    
    #run regression on southern hemisphere
    (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))
    
    # Plot
    plt.scatter(x_values,y_values)
    plt.plot(x_values,regress_values, "r-")
    plt.annotate(line_eq, text_coordinates, fontsize=15, color="red")
    plt.xlabel("Latitude")
    plt.ylabel(title)
    print(f"The r-squared is : {rvalue}")
    plt.show()<jupyter_output><empty_output><jupyter_text><jupyter_code>#Create northern and southern hemisphere DataFrame
northern_hemi_df = city_data_df.loc[(city_data_df["Lat"] >= 0)]
southern_hemi_df = city_data_df.loc[(city_data_df["Lat"] < 0)]<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x_values = northern_hemi_df["Lat"]
y_values = northern_hemi_df["Max Temp"]
plot_linear_regression(x_values, y_values, 'Max Temp', (6,30))
Findings: As the latitude increases in this hemisphere, the max temperature generally goes down. 
This is an inverse relationship<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Max Temp vs. Latitude Linear Regression<jupyter_code>x_values = southern_hemi_df["Lat"]
y_values = southern_hemi_df["Max Temp"]
plot_linear_regression(x_values, y_values, 'Max Temp', (-30,40))
Findings: As the latitude in southern hemisphere reaches closer to 0, the max temp is higher.
A positive relationship due to being closer to equator.<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>x_values = northern_hemi_df["Lat"]
y_values = northern_hemi_df["Humidity"]
plot_linear_regression(x_values, y_values, 'Humidity', (40,10))
Findings: Humidity is all over the place in these findings for the northern hemisphere.
It can be said that it is generally higher in most places.<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression<jupyter_code>x_values = southern_hemi_df["Lat"]
y_values = southern_hemi_df["Humidity"]
plot_linear_regression(x_values, y_values, 'Humidity', (-30,150))
Findings: Humidity is all over the place in these findings for the northern hemisphere.
It can be said that it is generally higher in most places.<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x_values = northern_hemi_df["Lat"]
y_values = northern_hemi_df["Cloudiness"]
plot_linear_regression(x_values, y_values, 'Cloudiness', (40,10))
Findings: While cloudiness is all over the place in relation to latitude,
it is typically very high or very low in the northern hemisphere.<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression<jupyter_code>x_values = southern_hemi_df["Lat"]
y_values = southern_hemi_df["Cloudiness"]
plot_linear_regression(x_values, y_values, 'Cloudiness', (-30,30))
Findings: While cloudiness is all over the place in relation to latitude,
it is typically very high or very low in the southern hemisphere.<jupyter_output><empty_output><jupyter_text>Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x_values = northern_hemi_df["Lat"]
y_values = northern_hemi_df["Wind Speed"]
plot_linear_regression(x_values, y_values, 'Wind Speed', (40,25))
Findings: There is very little positive correlation in wind speed with regards to latitude.<jupyter_output><empty_output><jupyter_text>Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression<jupyter_code>x_values = southern_hemi_df["Lat"]
y_values = southern_hemi_df["Wind Speed"]
plot_linear_regression(x_values, y_values, 'Wind Speed', (-30,30))
Findings: There is very little negative correlation in wind speed with regards to latitude.<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10676070038910505, 'fraction_numerical': 0.013537613488975357, 'mean_word_length': 4.584880036215482, 'pattern_counts': {'":': 9, '<': 76, '<?xml version=': 0, '>': 80, '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': '19', 'dir': 'jupyter-structured-clean-dedup', 'id': '107551', 'license': 'MIT', 'n_tokens_mistral': 4212, 'n_tokens_neox': 3936, 'n_words': 1181, 'path': 'WeatherPy.ipynb', 'repo_name': 'Dhruvpatel1417/python-api-challenge', 'size': '12336'}