Skip to main content

This package provide some python helper functions that are useful in machine learning.

Project description

helperfns

🎀 This is a python package that contains some helper functions for machine leaning.

logo

Getting started

To start using helperfns in your project you run the following command:

pip install helperfns

Or if you wan to install it in notebooks such as jupyter notebooks you can run the code cell with the following code:

!pip install helperfns

Usage

The helperfns package is made up of different sub packages such as:

  1. tables

  2. text

  3. utils

  4. visualization

  5. torch

tables

In the tables sub package you can print your data in tabular form for example:

from helperfns.tables import tabulate_data



column_names = ["SUBSET", "EXAMPLE(s)", "Hello"]

row_data = [["training", 5, 4],['validation', 4, 4],['test', 3, '']]

tabulate_data(column_names, row_data)

Output:

+---------------------------------+

|              Table              |

+------------+------------+-------+

| SUBSET     | EXAMPLE(s) | Hello |

+------------+------------+-------+

| training   |          5 |     4 |

| validation |          4 |     4 |

| test       |          3 |       |

+------------+------------+-------+

text

The text package offers two main function which are clean_sentence, de_contract, generate_ngrams and generate_bigrams

from helperfns.text import *



# cleans the sentence

print(clean_sentence("text 1 # https://url.com/bla1/blah1/"))

# list of all english words

print(english_words)

# converts strings like `I'm` to 'I am'

print(de_contract("I'm"))



# generate bigrams from a list of word

print(text.generate_bigrams(['This', 'film', 'is', 'terrible']))



# generates n-grams from a list of words

print(text.generate_ngrams(['This', 'film', 'is', 'terrible']))

utils

utils package comes with a simple helper function for converting seconds to hours, minutes and seconds.

Example:

start = time.time()

for i in range(100000):

   pass

end = time.time()

Output:

'0:00:00.01'

visualization

This sub package provides different helper functions for visualizing data using plots.

Examples:

from helperfns.visualization import plot_complicated_confusion_matrix, plot_images, plot_images_predictions, plot_simple_confusion_matrix



# plot predicted image labels with the images

plot_images_predictions(images, true_labels, preds, classes=["dog", "cat"] ,cols=8)



# plot the images with their labels

plot_images(images[:24], true_labels[:24], cols=8)



# plot a simple confusion matrix

y_true = [random.randint(0, 1) for _ in range (100)]

y_pred = [random.randint(0, 1) for _ in range (100)]

classes =["dog", "cat"]

plot_simple_confusion_matrix(y_true, y_pred, classes)



# plot a confusion matrix with percentage value of confusion

y_true = [random.randint(0, 1) for _ in range (100)]

y_pred = [random.randint(0, 1) for _ in range (100)]

classes =["dog", "cat"]

plot_complicated_confusion_matrix(y_true, y_pred, classes)

torch

This is a subpackage that contains other subpackages mainly used when working with pytorch. These sub packages are:

  1. text

  2. models

  3. accuracy

torch.text

This package contains the label_pipeline and text_pipeline. This helper function are normally used when doing text processing in python pytorch.

Example:

from helperfns.torch import text



# converting a sentence into sequence of integer representation.

vocab = {'<unk>': 0, 'this': 1, 'is': 2, 'a': 3, 'dog': 4}

tokenizer = lambda x: x.split(' ')

print(text.text_pipeline("This is a dog that is backing", tokenizer=tokenizer, vocab=vocab, unk_token='<unk>', lower=True))



# converting labels into their integer representation.

labels_dict = {l:i for (i, l) in enumerate(['af', 'en', 'st', 'ts', 'xh', 'zu'])}

print(text.label_pipeline("en", labels_dict=labels_dict))

torch.models

This package contains a helper function called model_params. This function is used to count the model parameters of a pytorch model. Example:

model_params(my_model)

Note that my_model is a python class model instance that is inheriting from the nn.Module class.

torch.accuracy

This package contains two helper functions called that are used to calculate the accuracy between predicted labels and real label. These functions are:

  1. binary_accuracy - used to calculate the binary accuracy between predicted labels and real labels.
y = y.to(device)

predictions = model(X).squeeze(1)

loss = criterion(predictions, y)

acc = binary_accuracy(predictions, y)

print(acc)
  1. categorical_accuracy - used to calculate the categorical accuracy between predicted labels and real labels.
y = y.to(device)

predictions = model(X).squeeze(1)

loss = criterion(predictions, y)

acc = categorical_accuracy(predictions, y)

print(acc)

Contributing to helperfns.

To contribute to helperfns read the CONTRIBUTION.md file.

License

In this package the MIT license was used which reads as follows:


MIT License



Copyright (c) 2022 crispengari



Permission is hereby granted, free of charge, to any person obtaining a copy

of this software and associated documentation files (the "Software"), to deal

in the Software without restriction, including without limitation the rights

to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

copies of the Software, and to permit persons to whom the Software is

furnished to do so, subject to the following conditions:



The above copyright notice and this permission notice shall be included in all

copies or substantial portions of the Software.



THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE

SOFTWARE.



Project details


Download files

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

Source Distribution

helperfns-0.0.4.tar.gz (14.1 kB view details)

Uploaded Source

Built Distribution

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

helperfns-0.0.4-py3-none-any.whl (13.4 kB view details)

Uploaded Python 3

File details

Details for the file helperfns-0.0.4.tar.gz.

File metadata

  • Download URL: helperfns-0.0.4.tar.gz
  • Upload date:
  • Size: 14.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.5

File hashes

Hashes for helperfns-0.0.4.tar.gz
Algorithm Hash digest
SHA256 a15764854c7da6d78acd5f5a734b878748c22b441781886111c0e6430c06177a
MD5 d1b764de7800071f7b0a3470403c749d
BLAKE2b-256 2f8855e4ef1b1d9d9a6cd29b5c58bf868f8046dfe934541d86f9801894dd006c

See more details on using hashes here.

File details

Details for the file helperfns-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: helperfns-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 13.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.5

File hashes

Hashes for helperfns-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 531195e9576632f7b1190fbcbba3858b228c29d51690cd57de1e5c02289dbbf1
MD5 a0144b47c2029405edc667086b6f0298
BLAKE2b-256 eca9d8f577060048d7e947af5dd2900af0b8e7ddbfede7133a84cdad96671db2

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 Pingdom Monitoring Sentry Error logging StatusPage Status page