This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def flatten_json(nested_json): | |
""" | |
Flatten json object with nested keys into a single level. | |
Args: | |
nested_json: A nested json object. | |
Returns: | |
The flattened json object if successful, None otherwise. | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def get_matches_df(sparse_matrix, name_vector, top=100): | |
non_zeros = sparse_matrix.nonzero() | |
sparserows = non_zeros[0] | |
sparsecols = non_zeros[1] | |
if top: | |
nr_matches = top | |
else: | |
nr_matches = sparsecols.size |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from itertools import chain, starmap | |
def flatten_json_iterative_solution(dictionary): | |
"""Flatten a nested json file""" | |
def unpack(parent_key, parent_value): | |
"""Unpack one level of nesting in json file""" | |
# Unpack one level only!!! | |
if isinstance(parent_value, dict): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from scipy.sparse import csr_matrix | |
!pip install sparse_dot_topn | |
import sparse_dot_topn.sparse_dot_topn as ct | |
def awesome_cossim_top(A, B, ntop, lower_bound=0): | |
# force A and B as a CSR matrix. | |
# If they have already been CSR, there is no overhead | |
A = A.tocsr() | |
B = B.tocsr() | |
M, _ = A.shape |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import plotly.graph_objects as go | |
fig = go.Figure(data=go.Scattergeo( | |
lat = df['latitude'], | |
lon = df['longitude'], | |
text = df['_id'], | |
marker = dict( | |
size = 8, | |
opacity = 0.7, | |
reversescale = True, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Encourage lander to use as little fuel as possible | |
# i.e. 0.85, or 0.32 | |
fuel_conservation = fuel_remaining / total_fuel | |
if distance_to_goal is decreasing: | |
if speed < threshold: | |
if position is on landing pad: | |
# Land successfully; give a big reward | |
landing_reward = 100 | |
# Multiply percentage of remaining fuel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
STOPWORDS = stopwords.words('english') | |
STOPWORDS = set(STOPWORDS) | |
def text_prepare(text, STOPWORDS): | |
""" | |
text: a string | |
return: a clean string | |
""" | |
REPLACE_BY_SPACE_RE = re.compile('[\n\"\'/(){}\[\]\|@,;#]') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
This season reminds me of the day I met you! You are my best colleague. Thank you! | |
### | |
We have surely come a long way! Happy Christmas my friend. | |
### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Christmas Wishes for Colleagues | |
### | |
You have worked very hard this year. You really deserve to have a wonderful Christmas. Merry Christmas my colleague. | |
### | |
It has always been a pleasure working with you! I wish you a Merry Christmas! | |
### | |
It feels great working with someone as knowledgeable as you and yet so humble. I wish you a Merry Christmas! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fig1, (ax1, ax2) = plt.subplots(1,2, figsize=(12,8), sharey=False) | |
fig1.tight_layout() | |
fig1.subplots_adjust(top=0.86) | |
sns.set_color_codes("pastel") | |
sns.barplot(x="Deaths", y="Province/State", data=bar_plot_df, label="num of Deaths", color="b", ax=ax1) | |
ax1.legend(ncol=2, loc="lower left", frameon=True) | |
ax1.set(xlim=(0, 33323), ylabel="", xlabel="Num of Deaths by states") | |
sns.despine(left=True, bottom=True,ax=ax1) |
NewerOlder