Skip to content

Instantly share code, notes, and snippets.

View MLWhiz's full-sized avatar
🤓
Focusing

Rahul Agarwal MLWhiz

🤓
Focusing
View GitHub Profile
def create_glove(word_index,embeddings_index):
emb_mean,emb_std = -0.005838499,0.48782197
all_embs = np.stack(embeddings_index.values())
embed_size = all_embs.shape[1]
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std, (nb_words, embed_size))
count_found = nb_words
for word, i in tqdm(word_index.items()):
if i >= max_features: continue
from textblob import TextBlob
word_sent = TextBlob("good").sentiment
print(word_sent.polarity,word_sent.subjectivity)
# 0.7 0.6
def create_glove(word_index,embeddings_index):
emb_mean,emb_std = -0.005838499,0.48782197
all_embs = np.stack(embeddings_index.values())
embed_size = all_embs.shape[1]
nb_words = min(max_features, len(word_index))
embedding_matrix = np.random.normal(emb_mean, emb_std, (nb_words, embed_size+4))
count_found = nb_words
for word, i in tqdm(word_index.items()):
if i >= max_features: continue
def add_features(df):
df['question_text'] = df['question_text'].progress_apply(lambda x:str(x))
df["lower_question_text"] = df["question_text"].apply(lambda x: x.lower())
df['total_length'] = df['question_text'].progress_apply(len)
df['capitals'] = df['question_text'].progress_apply(lambda comment: sum(1 for c in comment if c.isupper()))
df['caps_vs_length'] = df.progress_apply(lambda row: float(row['capitals'])/float(row['total_length']),
axis=1)
df['num_words'] = df.question_text.str.count('\S+')
df['num_unique_words'] = df['question_text'].progress_apply(lambda comment: len(set(w for w in comment.split())))
df['words_vs_unique'] = df['num_unique_words'] / df['num_words']
""" A Python Class
A simple Python graph class, demonstrating the essential
facts and functionalities of graphs.
Taken from https://www.python-course.eu/graphs_python.php
Changed the implementation a little bit to include weighted edges
"""
class Graph(object):
def __init__(self, graph_dict=None):
""" initializes a graph object
g = { "a" : {"d":2},
"b" : {"c":2},
"c" : {"b":5, "d":3, "e":5}
}
graph = Graph(g)
print("Vertices of graph:")
print(graph.vertices())
print("Edges of graph:")
print(graph.edges())
print("Add vertex:")
g = {'Frankfurt': {'Mannheim':85, 'Wurzburg':217, 'Kassel':173},
'Mannheim': {'Frankfurt':85, 'Karlsruhe':80},
'Karlsruhe': {'Augsburg':250, 'Mannheim':80},
'Augsburg': {'Karlsruhe':250, 'Munchen':84},
'Wurzburg': {'Erfurt':186, 'Numberg':103,'Frankfurt':217},
'Erfurt': {'Wurzburg':186},
'Numberg': {'Wurzburg':103, 'Stuttgart':183,'Munchen':167},
'Munchen': {'Numberg':167, 'Augsburg':84,'Kassel':502},
'Kassel': {'Frankfurt':173, 'Munchen':502},
'Stuttgart': {'Numberg':183}
def min_num_edges_between_nodes(graph,start_node):
distance = 0
shortest_path = []
queue = [start_node] #FIFO
levels = {}
levels[start_node] = 0
shortest_paths = {}
shortest_paths[start_node] = ":"
visited = [start_node]
while len(queue)!=0:
min_num_edges_between_nodes(g,'Frankfurt')
#We add another countries in the loop
graph = Graph(g)
graph.add_edge(("Mumbai", "Delhi"),400)
graph.add_edge(("Delhi", "Kolkata"),500)
graph.add_edge(("Kolkata", "Bangalore"),600)
graph.add_edge(("TX", "NY"),1200)
graph.add_edge(("ALB", "NY"),800)
g = graph.adj_mat()