<jupyter_start><jupyter_text>Spatial Data and Networks Live Demos<jupyter_code>earthquake_data = pd.read_csv("data/earthquakes.csv")
earthquake_data
earthquake_data.shape
earthquake_data.columns
earthquake_data.dtypes
earthquake_data = earthquake_data[["Date", "Time","Latitude", "Longitude", "Magnitude", "Depth"]]
earthquake_data
plt.hist(earthquake_data.Magnitude, bins = 20)
plt.xlabel("Magnitudet")
plt.ylabel("Number of earthquakes")

plt.show()
earthquake_data[earthquake_data.Magnitude > 9]
eartquake_x, earthquake_y = m(earthquake_data.Longitude.tolist(), earthquake_data.Latitude.tolist())

m = Basemap(
    projection = "merc", 
    llcrnrlat = -80, 
    urcrnrlat = 80,
    llcrnrlon = -180, 
    urcrnrlon = 180)
m.drawcoastlines()
m.fillcontinents(color = "coral", lake_color = "aqua")
m.drawparallels(np.arange(-90, 91, 30))
m.drawmeridians(np.arange(-180, 181, 60))
m.drawmapboundary(fill_color = "aqua")

m.plot(eartquake_x, earthquake_y, "o", color="red", markersize = 1)

plt.title("All earthquakes (M > 5.5)")
plt.show()

volcanos_data = pd.read_csv("data/volcanos.csv")
volcanos_data.head()

m = Basemap(
    projection = "merc", 
    llcrnrlat = -80, 
    urcrnrlat = 80,
    llcrnrlon = -180, 
    urcrnrlon = 180)
m.drawcoastlines()
m.fillcontinents(color = "coral", lake_color = "aqua")
m.drawparallels(np.arange(-90, 91, 30))
m.drawmeridians(np.arange(-180, 181, 60))
m.drawmapboundary(fill_color = "aqua")

volcano_x, volcano_y = m(volcanos_data.Longitude.tolist(), volcanos_data.Latitude.tolist())
m.plot(volcano_x, volcano_y, "x", color="green", markersize = 3)

plt.title("Major volcano eruptions")
plt.show()
ufo_sights = pd.read_csv("data/ufo_sightings_scrubbed.csv", low_memory= False)
ufo_sights.columns
ufo_sights.columns = [
        "datetime", "city", "state", "country", "shape", "duration (seconds)",
        "duration (hours/min)", "comments", "date posted", "latitude",
        "longitude"]
ufo_sights = ufo_sights.drop(ufo_sights[ufo_sights.latitude == "33q.200088"].index, axis = 0)
ufo_sights.latitude = ufo_sights.latitude.astype(float, errors = "ignore")

m = Basemap(
    projection = "mill", 
    llcrnrlat = -80, 
    urcrnrlat = 80,
    llcrnrlon = -180, 
    urcrnrlon = 180)
m.drawcoastlines()
m.fillcontinents(color = "coral", lake_color = "aqua")
m.drawparallels(np.arange(-90, 91, 30))
m.drawmeridians(np.arange(-180, 181, 60))
m.drawmapboundary(fill_color = "aqua")

ufo_x, ufo_y = m(ufo_sights.longitude.tolist(), ufo_sights.latitude.tolist())
m.plot(ufo_x, ufo_y, "x", color="blue", markersize = 3)

plt.title("UFO seen")
plt.show()<jupyter_output><empty_output><jupyter_text>Graphs<jupyter_code>g = nx.Graph()
g.add_edge("a", "b", weight = 0.1)
g.add_edge("b", "c", weight = 1.5)
g.add_edge("a", "c", weight = 1.0)
g.add_edge("c", "d", weight = 2.2)

g.edges
g.nodes
nx.draw(g, with_labels = True)
plt.show()
pos = nx.spring_layout(g)
weights = nx.get_edge_attributes(g, "weight")
nx.draw(g, pos, with_labels = True)
nx.draw_networkx_edge_labels(g, pos, edge_labels = weights)
nx.draw_networkx_edges(g, pos, width = [v * 2 for v in weights.values()])
plt.show()

# Directed Graph
dg = nx.DiGraph()
dg.add_edge("a", "b", weight = 0.1)
dg.add_edge("b", "c", weight = 1.5)
dg.add_edge("a", "c", weight = 1.0)
dg.add_edge("c", "d", weight = 2.2)
nx.draw(dg, with_labels = True)
plt.show()
nx.shortest_path(dg, "a", "d")
facebook_graph = nx.read_edgelist("data/facebook_combined.txt")
print(len(facebook_graph.nodes)) # 4039
print(len(facebook_graph.edges)) # 88234

karate_graph = nx.karate_club_graph()
centrality = nx.betweenness_centrality(karate_graph)

from networkx.algorithms import community
nx.draw(karate_graph, with_labels=True)
communities_generator = community.girvan_newman(karate_graph)
for i in range(1, 4):
  communities = next(communities_generator)
  print("level " + str(i), communities)<jupyter_output>level 1 ({0, 1, 3, 4, 5, 6, 7, 10, 11, 12, 13, 16, 17, 19, 21}, {2, 8, 9, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33})
level 2 ({0, 1, 3, 4, 5, 6, 7, 10, 11, 12, 13, 16, 17, 19, 21}, {32, 33, 2, 8, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, {9})
level 3 ({0, 1, 3, 7, 11, 12, 13, 17, 19, 21}, {32, 33, 2, 8, 14, 15, 18, 20, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}, {4, 5, 6, 10, 16}, {9})
{'fraction_non_alphanumeric': 0.17744186046511629, 'fraction_numerical': 0.06837209302325581, 'mean_word_length': 5.16189111747851, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 13, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '663339', 'license': 'MIT', 'n_tokens_mistral': 2131, 'n_tokens_neox': 1804, 'n_words': 385, 'path': 'DataScience/05.WorkingWithSpatialData/Lubo-SpatialDataAndNetworksDemos.ipynb', 'repo_name': 'LuGeorgiev/Python-SoftUni', 'size': '4300'}