Skip to content

Instantly share code, notes, and snippets.

@jackty9
jackty9 / faker_python_install_library
Created August 11, 2021 18:54
How to install Faker library in Google Colab
!pip install Faker
from faker import Faker
@jackty9
jackty9 / treemap_python_plotly_treemap_bonus.py
Last active March 9, 2021 15:22
Plotly bonus to push plotly from Google Colab to Plotly Studio
#BONUS
#To push your GC plot to Plotly Studio automatically
#!pip install chart_studio
import chart_studio
import chart_studio.plotly as py
username = '#REPLACE YOUR USERNAME HERE#' # your username
api_key = '#REPLACE YOUR API KEY HERE#' # your api key - go to profile > settings > regenerate key
chart_studio.tools.set_credentials_file(username=username, api_key=api_key)
py.plot(fig, filename = 'cmc_treemap', auto_open=True)
@jackty9
jackty9 / treemap_python_plotly_treemap.py
Last active March 4, 2021 15:16
Using Plotly to plot an interactive treemap
#!pip install --upgrade plotly
import plotly.graph_objects as go
import plotly.express as px
fig = px.treemap(df_final,
path=['name'],
values='USD_market_cap',
color_continuous_scale='RdYlGn',
color='USD_percent_change_24h',
)
@jackty9
jackty9 / treemap_python_squarify_treemap.py
Last active March 4, 2021 14:30
Use Squarify to plot a static treemap
import matplotlib
import matplotlib.pyplot as plt
#!pip install squarify
import squarify
#load data
sizes=df_final["USD_market_cap"]
label=df_final["name"]
# color scale on the price development
@jackty9
jackty9 / treemap_python_normalize_json_data.py
Created March 3, 2021 15:49
Normalize and convert the json data from CMC into pandas df format
import pandas as pd
#normalize the data into dataframe format
df = pd.json_normalize(data["data"])
cols_to_keep = ['name','symbol','cmc_rank','quote.USD.price','quote.USD.percent_change_24h','quote.USD.market_cap',]
df_final = df[cols_to_keep]
#rename columns
df_final.columns = ['name','symbol','cmc_rank','USD_price','USD_percent_change_24h','USD_market_cap',]
#uncomment below to print the table
#df_final
@jackty9
jackty9 / treemap_python_cmc_api_price.py
Created March 3, 2021 15:28
How to retrieve data from CMC using API key
#This example uses Python 2.7 and the python-request library.
from requests import Request, Session
from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
import json
url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
parameters = {
'start':'1',
'limit':'10',
@jackty9
jackty9 / confusion_matrix_classification_report.py
Last active December 1, 2020 21:48
Beyond_accuracy_classification_metrics_in_Python
# import initial libraries
from collections import Counter
from sklearn.datasets import make_classification
from imblearn.under_sampling import NearMiss
from matplotlib import pyplot
from numpy import where
# create dataset - imbalanced in the ratio of 90:10
X, y = make_classification(n_samples=1000, n_features=20, n_redundant=1,
n_clusters_per_class=1, weights=[0.9], flip_y=0.3, random_state=1)
counter = Counter(y)
@jackty9
jackty9 / Finding_the_best_sampling_ratio.py
Last active November 10, 2020 23:25
Dealing_imbalanced_data_in_Python
# import necessary libraries
import numpy as np
from sklearn.svm import SVC
from imblearn.over_sampling import SMOTE
from imblearn.under_sampling import RandomUnderSampler
from imblearn.pipeline import Pipeline
from sklearn.model_selection import cross_val_score
from sklearn.metrics import roc_auc_score
from numpy import mean
@jackty9
jackty9 / A_combination_of_over_and_under_sampling_imblearn.py
Last active November 10, 2020 22:46
Dealing_imbalanced_data_in_Python
#PART 1
# import sampling and other necessary libraries
from collections import Counter
from imblearn.over_sampling import SMOTE
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
from sklearn.svm import SVC
@jackty9
jackty9 / SMOTE_oversampling_imblearn.py
Last active November 12, 2020 08:25
Dealing_imbalanced_data_in_Python
#PART 1
# import SMOTE oversampling and other necessary libraries
from collections import Counter
from imblearn.over_sampling import SMOTE
from sklearn.model_selection import train_test_split
import pandas as pd
import numpy as np
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)