<jupyter_start><jupyter_text>First Part<jupyter_code># Import packages for data visualization
import networkx as nx                  # package for graph plotting  
import matplotlib.pyplot as plt        # basic plotting library
def graph_g(k,p):  # define the name of function
  clique = nx.caveman_graph(1, k) # Returns a caveman graph of l cliques of size k
  ring = nx.cycle_graph(p)       # returns a cycle graph for ring shape of size p
  g = nx.union(clique,ring, rename=('clique-', 'ring-'))  # join both graph and rename each graph
  g.add_edge('clique-0', 'ring-0')                        # add an edge between both graph
  return g
g = graph_g(6,6)                     # create an example graph using k = 6 and p = 6 
nx.draw_networkx(g,with_labels=False)
print("Number of Arches in g: ",g.number_of_edges())
# grade of each node
for node in g.nodes:
  grade = len(g.edges(node))
  print(f"grade of node {node} is {grade}")<jupyter_output>grade of node clique-0 is 6
grade of node clique-1 is 5
grade of node clique-2 is 5
grade of node clique-3 is 5
grade of node clique-4 is 5
grade of node clique-5 is 5
grade of node ring-0 is 3
grade of node ring-1 is 2
grade of node ring-2 is 2
grade of node ring-3 is 2
grade of node ring-4 is 2
grade of node ring-5 is 2<jupyter_text>Properties<jupyter_code># degree of centrality
deg_of_centrality = nx.degree_centrality(g)
deg_of_centrality
print(f"node with maximum degree of centrality is {max(deg_of_centrality, key=deg_of_centrality.get)}")
# betweeness
betweenness = nx.betweenness_centrality(g)
betweenness
print(f"node with maximum betweenness is {max(betweenness, key=betweenness.get)}")
#eccentricity
eccentricity = nx.eccentricity(g)
eccentricity
print(f"node with maximum eccentricity is {max(eccentricity, key=eccentricity.get)}")
print(f"radius: {nx.radius(g)}")
print(f"diameter: {nx.diameter(g)}")
print(f"center: {nx.center(g)}")
print(f"periphery: {nx.periphery(g)}")
print(f"density: {nx.density(g)}")<jupyter_output>radius: 3
diameter: 5
center: ['ring-0', 'ring-1', 'ring-5']
periphery: ['clique-1', 'clique-2', 'clique-3', 'clique-4', 'clique-5', 'ring-3']
density: 0.3333333333333333<jupyter_text>BFS and DFS<jupyter_code># Breadth First Search
def bfs(graph):
  return list(nx.bfs_edges(g, source=list(g.nodes)[0])) #list(graph.nodes)[0] gives node with index 0

#Depth First Search
def dfs(graph):
  return list(nx.dfs_edges(graph, source=list(graph.nodes)[0]))   #list(graph.nodes)[0] gives node with index 0

bfs(g)
dfs(g)<jupyter_output><empty_output><jupyter_text>Excercise 2<jupyter_code>import plotly.express as px  # import ploting library
import pandas as pd          # load dataset
df = pd.read_csv('https://raw.githubusercontent.com/plotly/datasets/master/earthquakes-23k.csv') # load data in pandas dataframe

fig = px.density_mapbox(df, lat='Latitude', lon='Longitude', z='Magnitude', radius=10,
                        center=dict(lat=0, lon=180), zoom=1,
                        mapbox_style="stamen-terrain")
fig.show()<jupyter_output><empty_output><jupyter_text>Exercise 3<jupyter_code>!pip install geopandas
import geopandas as gpd   # import the geopandas library
gpd.datasets.available    # check the builtin dataset 
earth_lowres = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres')) # load the world dataset
earth_lowres.plot(column='pop_est', legend=True);                          # plot the choropeth map taking population estimate as column
earthquakes=gpd.GeoDataFrame(df, geometry=gpd.points_from_xy(df.Longitude, df.Latitude))  # convert pandas dataframe into geodataframe
base = earth_lowres.plot();                                      # plot the base earth plot
earthquakes.plot(ax=base, marker='o', color='red', markersize=1); # earthquake plotting<jupyter_output><empty_output><jupyter_text>Second Part<jupyter_code>import glob
from google.colab import drive
drive.mount('/content/gdrive',force_remount=True)
!ls '/content/gdrive/My Drive/Images'
images = []
for file in glob.glob('/content/gdrive/My Drive/Images/*.jpg'):
  images.append(file)
from keras.applications.imagenet_utils import decode_predictions
from keras.applications import inception_v3
from keras.preprocessing.image import load_img
from keras.preprocessing.image import img_to_array
from matplotlib import pyplot as plt
from matplotlib.pyplot import imshow
import numpy as np
import math
import glob
from PIL import Image
imodel = inception_v3.InceptionV3(weights='imagenet')

tags ={}
for i, image in enumerate(images):
  im=load_img(image, target_size=(299, 299))
  numpy_image = img_to_array(im)
  image_batch = np.expand_dims(numpy_image, axis=0)
  processed_image = inception_v3.preprocess_input(image_batch.copy())
  predictions = imodel.predict(processed_image)
  label = decode_predictions(predictions)
  tags[image]=label
tags<jupyter_output><empty_output><jupyter_text>Most significative word<jupyter_code>significative_words ={}
for key, values in tags.items():       #iterate over tags dictionary
  significative_word = values[0][0]      # extract the tag with highest probability
  print(key+": "+significative_word[1])  # print the value
  significative_words[key] = significative_word[1] # append in the list for future use
#frequencies of words in the dictionary
list_of_tags =[]                  # create a list of tags
for key, values in tags.items():
  for tag in values[0]:
    list_of_tags.append(tag[1])

from collections import Counter
Counter(list_of_tags)             # count of each tag
# tf-idf of most significative word with respect to whole dictionary
import nltk.corpus
from nltk.text import TextCollection

for key, values in tags.items():     # iterate over dictionary
  print(key)
  print(TextCollection(list_of_tags).tf_idf(significative_words[key], text = list_of_tags)) # print tf_idf<jupyter_output>/content/gdrive/My Drive/Images/1.jpg
0.3057597459741414
/content/gdrive/My Drive/Images/2.jpg
0.2544316243440109
/content/gdrive/My Drive/Images/3.jpg
0.3057597459741414
/content/gdrive/My Drive/Images/4.jpg
0.3057597459741414
/content/gdrive/My Drive/Images/5.jpg
0.3057597459741414
/content/gdrive/My Drive/Images/6.jpg
0.3057597459741414
/content/gdrive/My Drive/Images/7.jpg
0.3057597459741414
/content/gdrive/My Drive/Images/8.jpg
0.3057597459741414
/content/gdrive/My Drive/Images/9.jpg
0.3057597459741414
/content/gdrive/My Drive/Images/10.jpg
0.2544316243440109
/content/gdrive/My Drive/Images/11.jpg
0.24519671766094878
/content/gdrive/My Drive/Images/12.jpg
0.3057597459741414
/content/gdrive/My Drive/Images/13.jpg
0.3057597459741414
/content/gdrive/My Drive/Images/14.jpg
0.3057597459741414
/content/gdrive/My Drive/Images/15.jpg
0.3057597459741414
/content/gdrive/My Drive/Images/16.jpg
0.3057597459741414
/content/gdrive/My Drive/Images/17.jpg
0.3057597459741414
/content/gdrive/My Dri[...]<jupyter_text>Visualization<jupyter_code>import seaborn as sns
sns.set_theme(style="darkgrid")

ax = sns.countplot(x=list_of_tags)  #count plot of tags
ax.set_xticklabels(ax.get_xticklabels(), rotation=90);  #rotate the x axis label   
ax = sns.countplot(x=list(significative_words.values()))  #most significative_words
ax.set_xticklabels(ax.get_xticklabels(), rotation=90);  #rotate the x axis label<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11060606060606061, 'fraction_numerical': 0.0568870523415978, 'mean_word_length': 4.869846402586904, 'pattern_counts': {'":': 1, '<': 30, '<?xml version=': 0, '>': 30, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 1, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '27643', 'license': 'MIT', 'n_tokens_mistral': 2797, 'n_tokens_neox': 2555, 'n_words': 614, 'path': 'networkx and geopandas/networkx.ipynb', 'repo_name': 'SaudQadir/data-practice', 'size': '7260'}