Skip to content

Instantly share code, notes, and snippets.

View alinazhanguwo's full-sized avatar

Alina Zhang alinazhanguwo

View GitHub Profile
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.
"""
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
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):
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
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,
# 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
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 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.
###
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!
@alinazhanguwo
alinazhanguwo / NumberOfDeathsCovid.py
Last active October 15, 2020 18:39
Number of deaths and confirmed cases in top 10 states of America (2020-09-13)
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)