Skip to main content

Data Science Utils: Frequently Used Methods for Data Science

License: MIT GitHub release (latest SemVer) GitHub issues Documentation Status PyPI - Python Version PyPI - Wheel PyPI version Anaconda-Server Badge Build Status Coverage Status

Data Science Utils extends the Scikit-Learn API and Matplotlib API to provide simple methods that simplify task and visualization over data.

Code Examples and Documentation

Let's see some code examples and outputs.

You can read the full documentation with all the code examples from: https://datascienceutils.readthedocs.io/en/latest/

In the documentation you can find more methods and more examples.

The API of the package is build to work with Scikit-Learn API and Matplotlib API. Here is an example of a typical workflow sequence with scikit-learn, matplotlib and data-science-utils:

Plot Confusion Matrix

from matplotlib import pyplot
from sklearn.model_selection import train_test_split
from sklearn.multiclass import OneVsRestClassifier
from sklearn import svm

from ds_utils.metrics import plot_confusion_matrix


x_train, x_test, y_train, y_test = train_test_split(x, y, test_size=.5)

# Create a simple classifier
classifier = OneVsRestClassifier(svm.LinearSVC())
classifier.fit(x_train, y_train)
y_pred = classifier.predict(x_test)

plot_confusion_matrix(y_test, y_pred, [0, 1, 2])
pyplot.show()

The following image will be shown:

multi label classification confusion matrix

see full example here.

Generate Decision Paths

from sklearn.tree import DecisionTreeClassifier

from ds_utils.xai import generate_decision_paths


# Create decision tree classifier object
clf = DecisionTreeClassifier(max_depth=3)

# Train model
clf.fit(x, y)
print(generate_decision_paths(clf, feature_names, target_names.tolist(), 
                              "iris_tree"))

The following text will be printed:

def iris_tree(petal width (cm), petal length (cm)):
    if petal width (cm) <= 0.8000:
        # return class setosa with probability 0.9804
        return ("setosa", 0.9804)
    else:  # if petal width (cm) > 0.8000
        if petal width (cm) <= 1.7500:
            if petal length (cm) <= 4.9500:
                # return class versicolor with probability 0.9792
                return ("versicolor", 0.9792)
            else:  # if petal length (cm) > 4.9500
                # return class virginica with probability 0.6667
                return ("virginica", 0.6667)
        else:  # if petal width (cm) > 1.7500
            if petal length (cm) <= 4.8500:
                # return class virginica with probability 0.6667
                return ("virginica", 0.6667)
            else:  # if petal length (cm) > 4.8500
                # return class virginica with probability 0.9773
                return ("virginica", 0.9773)

see full example here.

Extract Significant Terms from Subset

Based on the elasticsearch significant_text aggregation.

import pandas

from ds_utils.strings import extract_significant_terms_from_subset

corpus = ['This is the first document.', 'This document is the second document.',
          'And this is the third one.', 'Is this the first document?']
data_frame = pandas.DataFrame(corpus, columns=["content"])
# Let's differentiate between the last two documents from the full corpus
subset_data_frame = data_frame[data_frame.index > 1]
terms = extract_significant_terms_from_subset(data_frame, subset_data_frame, 
                                               "content")

And the following table will be the output for terms:

third one and this the is first document second
1.0 1.0 1.0 0.67 0.67 0.67 0.5 0.25 0.0

see full example here.

Excited?

Read about all the modules here and see more methods and abilities (such as drawing a decision tree and more):

  • Metrics - The module of metrics contains methods that help to calculate and/or visualize evaluation performance of an algorithm.
  • Preprocess - The module of preprocess contains methods that are processes that could be made to data before training.
  • Strings - The module of strings contains methods that help manipulate and process strings in a dataframe.
  • Visualization Aids - The module of visualization aids contains methods that visualize by drawing or printing ML output.
  • XAI - The module of xai contains methods that help explain a model decisions.

Contributing

Interested in contributing to Data Science Utils? Great! You're welcome, and we would love to have you. We follow the Python Software Foundation Code of Conduct and Matplotlib Usage Guide.

No matter your level of technical skill, you can be helpful. We appreciate bug reports, user testing, feature requests, bug fixes, product enhancements, and documentation improvements.

Thank you for your contributions!

Find a Bug?

Check if there's already an open issue on the topic. If needed, file an issue.

Open Source

Data Science Utils license is MIT License.

Installing Data Science Utils

Data Science Utils is compatible with Python 3.6 or later. The simplest way to install Data Science Utils and its dependencies is from PyPI with pip, Python's preferred package installer:

pip install data-science-utils

Note that this package is an active project and routinely publishes new releases with more methods. In order to upgrade Data Science Utils to the latest version, use pip as follows:

pip install -U data-science-utils

Alternatively you can install from source by cloning the repo and running:

git clone https://github.com/idanmoradarthas/DataScienceUtils.git
cd DataScienceUtils
python setup.py install

Or install using pip from source:

pip install git+https://github.com/idanmoradarthas/DataScienceUtils.git

If you're using Anaconda, you can install using conda:

conda install -c idanmorad data-science-utils

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

data_science_utils-1.6.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

data_science_utils-1.6-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file data_science_utils-1.6.tar.gz.

File metadata

  • Download URL: data_science_utils-1.6.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1.post20200604 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for data_science_utils-1.6.tar.gz
Algorithm Hash digest
SHA256 822a8fa970a4bcc9d6aec899ca01012e708e2ffed35af1e85cae063b571cf817
MD5 10ac15167f2f8f850fbc3c1b7cd29ab9
BLAKE2b-256 667bf14976edceceea0683757ad3de0a61eff9be40975be23de6dbebd52dc51b

See more details on using hashes here.

File details

Details for the file data_science_utils-1.6-py3-none-any.whl.

File metadata

  • Download URL: data_science_utils-1.6-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/2.0.0 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1.post20200604 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.7.7

File hashes

Hashes for data_science_utils-1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 10b209602919e10912a1374725e886ce5ee0796e75b306e95f8d982a658b532f
MD5 94af4b6bebeda2648bee24cead482ac0
BLAKE2b-256 6609e5459f1a44e033f3e8e01548fd0b69d3107939242a422f7fedaaf48ddad9

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page