<jupyter_start><jupyter_text>WeatherPy Observation:1) API calls are best way to perform real time analysis. 2) By plotting scatter plot we can easily pridict the trend.3) Linear regression helps to understand how strong is the corelation. Negative value means weak corelation.<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 scipy.stats as st
import json

# Import API key
from api_keys import weather_api_key

# Incorporated citipy to determine city based on latitude and longitude
from citipy import citipy

# Import CSV file
output_data_file = "output_data/cities.csv"

# Range of latitudes and longitudes
lat_range = (-90, 90)
lng_range = (-180, 180)
output_data_file = pd.read_csv("../output_data/cities.csv")
output_data_file<jupyter_output><empty_output><jupyter_text>Generate Cities List<jupyter_code>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)
#Print url:
url = "http://api.openweathermap.org/data/2.5/forecast?id=524901&APPID="
target_url = url + weather_api_key
target_url
#List setup 

cityname = []
cloudiness = []
country = []
date = []
humidity = []
lat = []
lng = []
max_temp = []
wind_speed = []
count = 0

   
# Loop through the list of cities

for city in cities:
    weather_response = requests.get(target_url + "&q=" + city + "&units=Imperial")
    weather_json = weather_response.json()
    try:
        cityname.append(weather_json['city']['name'])
        cloudiness.append(weather_json['list'][0]['clouds']['all'])
        country.append(weather_json['city']['country'])
        date.append(weather_json['list'][0]['dt'])
        humidity.append(weather_json['list'][0]['main']['humidity'])
        lat.append(weather_json['city']['coord']['lat'])
        lng.append(weather_json['city']['coord']['lon'])
        max_temp.append(weather_json['list'][0]['main']['temp_max'])
        wind_speed.append(weather_json['list'][0]['wind']['speed'])
        count = count + 1
        print(f"Processing record {count} of Set 1 | {city}")
    except:
        print("Record not found") 
        pass
        
# Create a DataFrame

weather_dict = {"City": cityname,"Latitude": lat, "Longitude":lng,
                "Max Temp":max_temp,"Humidity":humidity,"Cloudiness":cloudiness,
                "Wind Speed":wind_speed,"Country":country, "Date":date}
weather_data = pd.DataFrame(weather_dict)

weather_data.head()
weather_dict = {
    "City": cityname,
    "Cloudiness" : cloudiness,
    "Country" : country,
    "Date" : date,
    "Humidity" : humidity,
    "Temp": max_temp,
    "Lat" : lat,
    "Lng" : lng,   
    "Wind Speed" : wind_speed
}
weather_data = pd.DataFrame(weather_dict)
weather_data.count()
weather_data.head()
# Save a new csv file
weather_data.to_csv("../output_data/new_city_weather.csv")
#Plot latitude vs temperature
plt.scatter(weather_data['Lat'], weather_data['Temp'])
plt.title(f'City Latitude vs. Max Temperature')
plt.xlabel('Latitude')
plt.ylabel('Temperature (F)')
plt.grid(True)
plt.savefig('../Images/lat vs temp.png', bbox_inches='tight')
#Plot latitude vs humidity and save as .png
plt.scatter(weather_data['Lat'],weather_data['Humidity'])
plt.title(f'City Latitude vs. Humidity')
plt.xlabel('Latitude')
plt.ylabel('Humidity (%)')
plt.grid(True)
plt.savefig('../Images/lat vs humid.png', bbox_inches='tight')
#Plot latitude vs cloudiness and save as .png
plt.scatter(weather_data['Lat'], weather_data['Cloudiness'])
plt.title(f'City Latitude vs. Cloudiness')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.grid(True)
plt.savefig('../Images/lat vs cloud.png', bbox_inches='tight')
#Plot latitude vs Wind speed and save as .png
plt.scatter(weather_data['Lat'], weather_data['Wind Speed'])
plt.title(f'City Latitude vs. Wind Speed')
plt.xlabel('Latitude')
plt.ylabel('Cloudiness (%)')
plt.grid(True)
plt.savefig('../Images/lat vs windspeed.png', bbox_inches='tight')
north_hemi = weather_data.loc[weather_data["Lat"] >= 0]
south_hemi = weather_data.loc[weather_data["Lat"] < 0]
def linear_agression(x,y):
    print(f"The r-squared is : {round(st.pearsonr(x, y)[0],2)}")
    (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))
    plt.scatter(x, y)
    plt.plot(x,regress_values,"r-")
    return line_eq

# Define a fuction for annotating
def annotate(line_eq, a, b):
    plt.annotate(line_eq,(a,b),fontsize=15,color="red")
#Northern Hemisphere - Max Temp vs. Latitude Linear Regression

equation = linear_agression(north_hemi["Lat"],north_hemi["Temp"])
annotate(equation, 0, 50)

# Set a title
plt.title("Northern Hemisphere - Max Temp vs. Latitude Linear Regression")

# Set x and y label
plt.xlabel("Latitude")
plt.ylabel("Max Temp (F)")

plt.savefig("../Images/Northern Hemisphere - Max Temp vs. Latitude.png")
#Southern Hemisphere - Max Temp vs. Latitude Linear Regression

equation = linear_agression(south_hemi["Lat"],south_hemi["Temp"])
annotate(equation, -30, 40)

# Set a title
plt.title("Southern Hemisphere - Max Temp vs. Latitude Linear Regression")

# Set x and y label
plt.xlabel("Latitude")
plt.ylabel("Max Temp (F)")

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

#Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression
equation = linear_agression(north_hemi["Lat"], north_hemi["Humidity"])
annotate(equation, 45, 10)
#Set title
plt.title("Northern Hemisphere - Humidity (%) vs. Latitude Linear Regression")

#Set x and y label
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")

plt.savefig("../Images/Northern Hemisphere - Humidity vs. Latitude.png")
#Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression
equation = linear_agression(south_hemi["Lat"], south_hemi["Humidity"])
annotate(equation, -55, 30)
#set Title 
plt.title("Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression")
#Set x and y label
plt.xlabel("Latitude")
plt.ylabel("Humidity (%)")

plt.savefig("../Images/Southern Hemisphere - Humidity vs. Latitude.png")
#Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression

equation = linear_agression(north_hemi["Lat"], north_hemi["Cloudiness"])
annotate(equation, 30, 40)

# Set a title
plt.title("Northern Hemisphere - Cloudiness (%) vs. Latitude Linear Regression")

# Set x and y labels
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")

plt.savefig("../Images/Northern Hemisphere - Cloudiness vs. Latitude.png")
#Southern Hemisphere - Humidity (%) vs. Latitude Linear Regression

equation = linear_agression(south_hemi["Lat"], south_hemi["Cloudiness"])
annotate(equation,-50, 10)

# Set a title
plt.title("southern_hemisphere - Cloudiness (%) vs. Latitude Linear Regression")

# Set x and y labels
plt.xlabel("Latitude")
plt.ylabel("Cloudiness (%)")


plt.savefig("../Images/Southern Hemisphere - Cloudiness vs. Latitude.png")
#Northern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression

equation = linear_agression(north_hemi["Lat"], north_hemi["Wind Speed"])
annotate(equation, 40, 25)

# Set a title
plt.title("Northern Hemisphere - Wind speed(mph) (%) vs. Latitude Linear Regression")

# Set x and y labels
plt.xlabel("Latitude")
plt.ylabel("Wind Speed")

plt.savefig("../Images/Northern Hemisphere - Wind speed vs. Latitude.png")
#Southern Hemisphere - Wind Speed (mph) vs. Latitude Linear Regression
equation = linear_agression(south_hemi["Lat"],south_hemi["Wind Speed"])
annotate(equation, -25, 25)
# Set a title
plt.title("Southern Hemisphere - Wind speed(mph) (%) vs. Latitude Linear Regression")

# Set x and y labels
plt.xlabel("Latitude")
plt.ylabel("Wind Speed")


plt.savefig("../Images/Southern Hemisphere - Wind speed vs. Latitude.png")<jupyter_output>The r-squared is : -0.09
{'fraction_non_alphanumeric': 0.14444843693855552, 'fraction_numerical': 0.012336806803209966, 'mean_word_length': 4.943060498220641, 'pattern_counts': {'":': 11, '<': 9, '<?xml version=': 0, '>': 9, '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': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '547930', 'license': 'ADSL', 'n_tokens_mistral': 3074, 'n_tokens_neox': 2770, 'n_words': 708, 'path': 'starter_code/WeatherPy.ipynb', 'repo_name': 'PR1690/python-api-challenge', 'size': '8349'}