DEV Community

Mostafa Gazar
Mostafa Gazar

Posted on

Some Python handy snippets for Machine Learning Engineers

Get file name and extension

from pathlib import Path

path = Path("dir/file.xml")

file_name = path.stem  # file
file_extension = path.suffix  # .xml
Enter fullscreen mode Exit fullscreen mode

Iterate through directory contents

from pathlib import Path

data_root = Path("/imgs")
for item in data_root.iterdir():
    print(item)
Enter fullscreen mode Exit fullscreen mode

You can also use glob

from pathlib import Path

data_root = Path("/imgs")

# Get all png images in this directory level
data_root.glob("*.png")
# Get all png images in this directory including sub-directories
data_root.glob("*.png", recursive=True)
Enter fullscreen mode Exit fullscreen mode

Inverse dictionary key/value => value/key

inversed_dict = {v: k for k, v in some_dict.items()}
Enter fullscreen mode Exit fullscreen mode

Flatten a list of lists

import operator

flat_list = reduce(operator.concat, some_list)
Enter fullscreen mode Exit fullscreen mode

Map string list to int list

elements = ["1", "2", "3"]

elements = list(map(int, elements))
Enter fullscreen mode Exit fullscreen mode

Count unique values in list and their total

from collections import Counter

elements = ["1", "2", "3", "2", "3", "3"]

# Unique values in list
keys = Counter(elements).keys()

# Frequency of their appearance
values = Counter(elements).values()
Enter fullscreen mode Exit fullscreen mode

Sort dict by value

for key in sorted(confidences, key=confidences.get, reverse=True):
    print(f'key {key}')
Enter fullscreen mode Exit fullscreen mode

Format f strings

print(f'2 decimal points {some_variable:.2f}')
Enter fullscreen mode Exit fullscreen mode

Batch an iterable

def batch(iterable, n=1):
    l = len(iterable)
    for index in range(0, l, n):
        yield iterable[index:min(index + n, l)]

# and call it like
for x, pred in enumerate(batch(preds, 32), 1):
    pass
Enter fullscreen mode Exit fullscreen mode

I am working on a project called ML Studio, want to get early access to and product updates? Subscribe here or follow me on twitter.

Top comments (0)