Python Library

2 minute read

In this post, I would like to share about the useful Python libraries.

Pyforest

Lazy-import of all popular Python Data Science libraries. Stop writing the same imports over and over again.

pip install pyforest

or

pip install --upgrade pyforest

It supports almost most of the popular libraries such as pd(pandas), np(numpy), sklearn etc. You can check the supported libraries by running below command

dir(pyforest)

In the jupyter notebook just run command below and you dont have to import for each libraries when you want to use it.

import pyforest

To check what libraries you have imported you can run command below

active_imports()

Then you dont have to import all the libraries at the beginning each time, so give it a try!

Prophet

How to install it

pip install fbprophet

python import

from fbprophet import Prophet
df = pd.read_csv('data.csv')
m = Prophet()
m.fit(df)

#The predict method will assign each row in future a predicted value
forecast = m.predict(future)
forecast[['ds', 'yhat', 'yhat_lower', 'yhat_upper']].tail()
#Plot
fig1 = m.plot(forecast)
fig2 = m.plot_components(forecast)

#It also has it owns plot
from fbprophet.plot import plot_plotly
import plotly.offline as py
py.init_notebook_mode()

fig = plot_plotly(m, forecast)  # This returns a plotly Figure
py.iplot(fig)

Code Formatter Black

Black is the uncompromising Python code formatter A simple extension for Jupyter Notebook and Jupyter Lab to beautify Python code automatically using Black

pip install nb_black

For Jupyter Notebook

%load_ext nb_black
d = {"a": 1, "b":2, 
     'c' : "nikhil"}

Formatted

d = {"a": 1, "b": 2, "c": "nikhil"}

Progress Bar in Jupyter notebook

Library tqdm

pip install tqdm

In the Jupyter notebook

from tqdm import trange
from time import sleep

for i in trange(100):
    sleep(0.01)

Code Refactoring

Radon

pip install radon

Radon is a Python tool that computes various metrics from the source code. Radon can compute:

  • McCabe’s complexity, i.e. cyclomatic complexity
  • raw metrics (these include SLOC, comment lines, blank lines, &c.)
  • Halstead metrics (all of them)
  • Maintainability Index (the one used in Visual Studio)

Data model

Special Methods

  • under under method under under = Dunder
    __name__ == __main__:
    main()
    

The function’s name.

You can refer for more information about Dunder Methods

streamlit

import streamlit as st

## Title
st.title("Hello World")

## Markdown
st.markdown("This example of Markdown")

## Header/subheader
st.header("Header")
st.subheader("Subheader")

## Colourful(Error/Information)
st.successful("Successful")
st.warning("Error")
st.info("Information")
st.error("Error")
st.exception("Exception")

from PIL import Image
img = Image.open("example.jpg")

import datetime

today = st.date_input("Today is ", datetime.datetime.now)
the_time = st.time_input("The time is",datetime.time())

if st.button("Thanks"):
    st.balloons()

st.sidebar.header("About App")
st.sidebar.info("A Visualization Demo for TRI Data Scientist Challenge")
st.sidebar.header("About")
st.sidebar.info("Naeem Hussien")
st.sidebar.text("Machine Learning Engineer\n"
    "beBit Tokyo Japan")

Tags:

Updated: