-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
131 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Contract\Operation; | ||
|
||
use loophp\collection\Contract\Collection; | ||
|
||
/** | ||
* @template TKey | ||
* @template T | ||
*/ | ||
interface Entropyable | ||
{ | ||
/** | ||
* Calculate the normalized Shannon entropy for each collection items. | ||
* | ||
* If you're looking for one single result, you must get the last item using | ||
* `last` operation. | ||
* | ||
* @see https://en.wikipedia.org/wiki/Entropy_(information_theory) | ||
* @see https://loophp-collection.readthedocs.io/en/stable/pages/api.html#entropy | ||
* | ||
* @return Collection<int, float> | ||
*/ | ||
public function entropy(): Collection; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace loophp\collection\Operation; | ||
|
||
use Closure; | ||
use Generator; | ||
use loophp\collection\Collection; | ||
use loophp\collection\Contract\Collection as CollectionInterface; | ||
|
||
/** | ||
* @immutable | ||
* | ||
* @template TKey | ||
* @template T | ||
*/ | ||
final class Entropy extends AbstractOperation | ||
{ | ||
/** | ||
* @return Closure(iterable<TKey, T>): Generator<int, float> | ||
*/ | ||
public function __invoke(): Closure | ||
{ | ||
/** @var Closure(iterable<TKey, T>): Generator<int, float> $pipe */ | ||
$pipe = (new Pipe())()( | ||
(static fn (iterable $iterable): CollectionInterface => Collection::fromIterable($iterable)->normalize()->squash()), | ||
(new Map())()( | ||
static fn (mixed $_, int $key, Collection $collection): float => $collection | ||
->limit($key + 1) | ||
->frequency() | ||
->keys() | ||
->map( | ||
static fn (int $freq): float => $freq / ($key + 1) | ||
) | ||
->reduce( | ||
static fn (float $acc, float $freq): float => 0 === $key ? $acc : $acc - (($freq) * log($freq, 2)) / log($key + 1, 2), | ||
0 | ||
) | ||
), | ||
); | ||
|
||
return $pipe; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters