Skip to content

Instantly share code, notes, and snippets.

View Alvtron's full-sized avatar
💭
Working at dRofus

Thomas Angeland Alvtron

💭
Working at dRofus
View GitHub Profile
@Alvtron
Alvtron / EnumerableSplitExtensions.cs
Last active November 25, 2020 07:10
Split enumerable into two based on predicate.
public static class EnumerableSplitExtensions
{
/// <summary>
/// Splits the enumerable into two separate sequences based on predicate.
/// </summary>
/// <remarks>
/// This is more efficient than using two Where()-filters (from LINQ) because the source is only enumerated one time. Testing reveals an approximate 30% speed increase.
/// </remarks>
/// <typeparam name="TSource">The type of the elements in the enumerable.</typeparam>
/// <param name="source">The source of elements.</param>
@Alvtron
Alvtron / EnumerableCountExtensions.cs
Last active March 26, 2023 01:00
Enumerable extensions for faster count checking instead of using the built in .Count() extension method.
public static class EnumerableCountExtensions
{
/// <summary>
/// Check if the number of elements in the source is equal to the specified count.
/// </summary>
/// <remarks>
/// This will never exhaust the source past the necessary number of elements.
/// This is in almost all circumstances faster than .Count() == x, where x is an arbitrary <see cref="int"/>.<br/>
/// If the source is a sequence that implements <see cref="ICollection{TSource}"/>, the Count-property is used instead.
/// </remarks>
@Alvtron
Alvtron / pytorch_stratified_split.md
Last active November 22, 2023 12:37
Split a PyTorch Dataset into two subsets using stratified random sampling.

Stratified dataset split in PyTorch

When working with imbalanced data for machine learning tasks in PyTorch, and simple random split might not be able to partly divide classes that are not well represented. Resulting sample splits might not portray the real-world population, leading to poor predictive peformance in the resulting model.

Therefore, I have created a simple function for conducting a stratified split with random shuffling, similar to that of StratifiedShuffleSplit from scikit-learn (https://scikit-learn.org/stable/modules/generated/sklearn.model_selection.StratifiedShuffleSplit.html)

import random
import math
import torch.utils.data
@Alvtron
Alvtron / reflect.md
Last active January 10, 2020 15:23
Reflect values between a minimum and maximum.

Reflect

Reflect values between a minimum and maximum. I have written both an iterative and recursive version of the algorithm.

Iterative (fastest)

import math

def reflect(value, min_value, max_value):
    span = max_value - min_value
    if value < min_value:
@Alvtron
Alvtron / download_bob_ross_paintings.md
Last active January 13, 2020 11:12
Downloads all 411 paintings by Bob Ross from the site Two Inch Brush with Python.