Skip to main content

PyPI version Downloads Downloads Downloads

cost2fitness

PyPI package for conversion cost values (less is better) to fitness values (more is better) and vice versa

pip install cost2fitness

About

This is the package containing several methods for transformation numpy arrays depended on scales, averages and so on. But the primary way to use it is the conversion from cost values (less is better) to fitness values (more is better) and vice versa. It can be highly helpful when u r using

  • evolutionary algorithms depended on numeric differences: so, it's important to set good representation of samples scores for better selection
  • sampling methods with probabilities depended on samples scores

Transformers

There are several simple transformers. Each transformer is the subclass of BaseTransformer class containing name field and transform(array) method which transforms input array to new representation.

Checklist:

  • ReverseByAverage,
  • AntiMax,
  • AntiMaxPercent(percent),
  • Min2Zero,
  • Min2Value(value),
  • ProbabilityView (converts data to probabilities),
  • SimplestReverse,
  • AlwaysOnes (returns array of ones),
  • NewAvgByMult(new_average),
  • NewAvgByShift(new_average)
  • Divider(divider_number_or_array) (divides array on number or array, useful for fixed start normalization)
  • Argmax (returns position of maximum element in array)
  • Prob2Class(threshold = 0.5) (to convert probabilities to classes 0/1)
  • ToNumber (converts array to one number by getting first element)

U can create your transformer using simple logic from file.

import numpy as np 

from cost2fitness import Min2Zero

tf = Min2Zero()

arr_of_scores = np.array([10, 8, 7, 5, 8, 9, 20, 12, 6, 18])

tf.transform(arr_of_scores)
# array([ 5,  3,  2,  0,  3,  4, 15,  7,  1, 13])

Pipeline of transformers

U also can combine these transformers using Pl pipeline. For example:

import numpy as np 

from cost2fitness import ReverseByAverage, AntiMax, Min2Zero, Pl

pipe = Pl([
        Min2Zero(),
        ReverseByAverage(),
        AntiMax()        
        ])


arr_of_scores = np.array([10, 8, 7, 5, 8, 9])


# return each result of pipeline transformation (with input)
pipe.transform(arr_of_scores, return_all_steps= True)
#array([[10.        ,  8.        ,  7.        ,  5.        ,  8.        ,
#         9.        ],
#       [ 5.        ,  3.        ,  2.        ,  0.        ,  3.        ,
#         4.        ],
#       [ 0.66666667,  2.66666667,  3.66666667,  5.66666667,  2.66666667,
#         1.66666667],
#       [ 5.        ,  3.        ,  2.        ,  0.        ,  3.        ,
#         4.        ]])

# return only result of transformation
pipe.transform(arr_of_scores, return_all_steps= False)
#array([5., 3., 2., 0., 3., 4.])

How to plot

There is plot_scores function for plotting transformation process results. It has arguments:

  • scores : 2D numpy array 2D numpy array with structure [start_values, first_transform(start_values), second_transform(first_transform), ...], where each object is 1D-array of scores (values/costs/fitnesses).
  • names : None/string list, optional Names for each step for plot labels. The default is None.
  • kind : str, optional for 'beside' each new column will be beside previous; for 'under' there will be new plot under previous. The default is 'beside'.
  • save_as : None/str, optional File path to save the plot. The default is None.

Examples with plotting

Each transformer

Code

Pipeline

Code

Neural net tools

I have made basic neural network tools here because it's very necessary to use simple networks with some reinforcement learning tasks, but common packages like Keras work extremely slow if u need just prediction (forward propagation) only for 1 sample but many times. So it will be faster to use simple numpy-based packages for these cases.

Layers

It was not so difficult to use this transformers logic for creating neural networks. So this package has next neural network layers as transformers:

  • Activations:

    • Softmax
    • Relu
    • LeakyRelu(alpha = 0.01)
    • Sigmoid
    • Tanh
    • ArcTan
    • Swish(beta = 0.25)
    • Softplus
    • Softsign
    • Elu(alpha)
    • Selu(alpha, scale)
  • Dense layers tools:

    • Bias(bias_len, bias_array = None) -- to add bias with length bias_len. If bias_array is None, uses random bias
    • MatrixDot(from_size, to_size, matrix_array = None)
    • NNStep(from_size, to_size, matrix_array = None, bias_array = None) -- it's MatrixDot and Bias together, if u wanna create them faster

Helpers

And there are several helpers methods for using pipeline object like neural network (only for forward propagation of course):

  • pipeline objects methods:

    • get_shapes() -- to get list of shapes of needed array for NN
    • total_weights() -- get count of weights for overall NN
    • set_weights(weights) - set weights (as list of arrays with needed shapes) for NN
  • Alone functions:

    • arr_to_weigths(arr, shapes) -- converts 1D-array arr to list of arrays with shapes shapes to put it in set_weights method

Examples

See simplest example

Metadata

Release files for cost2fitness 2.0.7

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cost2fitness 2.0.7
File Size Uploaded
cost2fitness-2.0.7.tar.gz 10.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for cost2fitness 2.0.7
File Interpreter ABI Platform
cost2fitness-2.0.7-py3-none-any.whl Python 3 none any Details

Total release size: 19.7 kB

Release files / cost2fitness-2.0.7.tar.gz

Download URL cost2fitness-2.0.7.tar.gz
Size 10.3 kB
Tags Source
SHA-256 checksum
How to use checksums
712492ec0e815e18cf9e2592b5b68d42c5f59b381d2208d5728b2cbae8b21855
BLAKE2b-256 checksum
How to use checksums
03c7c1ba04217ce46dfe073d6d17cc8f5e6d54a627deb4f494d427103e4fedb1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

Release files / cost2fitness-2.0.7-py3-none-any.whl

Download URL cost2fitness-2.0.7-py3-none-any.whl
Size 9.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bbeaafdbd3b8b2af1bc131336c5c6b617d705d9e84d0719dcdc8c7521cf7a0ea
BLAKE2b-256 checksum
How to use checksums
f56669ecbf1758ff4b5f68c3d9d9778a515247c5568abc4adc71ffecee89e70a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.3

Release history Release notifications | RSS feed

This release

2.0.7 This release

2 release files

2.0.6

2 release files

2.0.5

2 release files

2.0.4

2 release files

2.0.3

2 release files

2.0.2

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page