2014/05/26(月)Perl Data Language 基礎編 #03 「PDLの初歩的なメソッド」
コードに無駄があったのと、転置行列を追加したので再投稿です。
ドキュメントは以下の通り。
- http://pdl.perl.org/PDLdocs/Ufunc.html
- http://search.cpan.org/~chm/PDL/Basic/Primitive/primitive.pd#random
- http://search.cpan.org/~chm/PDL/Basic/MatrixOps/matrixops.pd
- https://metacpan.org/pod/PDL::Basic#transpose
#!/usr/bin/env perl
use strict;
use warnings;
use feature qw/say/;
use PDL;
use DDP filters => { -external => [ 'PDL' ] };
my $a = pdl [ 8, 7, 3, 9, 1, 6, 8 ];
my $b = pdl [
[ 2, 3, 2 ],
[ 4, 5, 6 ],
[ 1, 2, 1 ],
];
say "最小値";
say $a->min;
say $b->min;
print "\n";
say "最大値";
say $a->max;
say $b->max;
print "\n";
say "平均";
say $a->avg;
say $b->avg;
print "\n";
say "最頻値";
say $a->mode;
say $b->mode;
say PDL->new([4,4,2,3,3])->mode;
print "\n";
say "メディアン";
say $a->median;
say $b->median;
print "\n";
say "ランダム";
say PDL->random(3, 4);
print "\n";
say "単位行列";
say identity(3, 3);
print "\n";
say "逆行列";
say inv( PDL->new([[4,2],[1,3]]) );
print "\n";
say "転置行列";
say transpose($a);
print "\n";
say "ゼロ行列";
say PDL->zeros(3, 1);
print "\n";
say "クイックソート";
say $a->qsort;
say $b->qsort;
print "\n";
実行結果↓
最小値
1
1
最大値
9
6
平均
6
2.88888888888889
最頻値
8
2
3
メディアン
7
2
ランダム
[
[0.27851303 0.36231595 0.40859514]
[0.33323768 0.29340034 0.67641457]
[0.69607193 0.39091348 0.3260092]
[0.61342505 0.94111466 0.22114707]
]
単位行列
[
[1 0 0]
[0 1 0]
[0 0 1]
]
逆行列
[
[ 0.3 -0.2]
[-0.1 0.4]
]
転置行列
[
[8]
[7]
[3]
[9]
[1]
[6]
[8]
]
ゼロ行列
[
[0 0 0]
]
クイックソート
[1 3 6 7 8 8 9]
[
[2 2 3]
[4 5 6]
[1 1 2]
]