Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
356b55f2
提交
356b55f2
authored
4月 13, 2017
作者:
Joseph Paul Cohen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
added more test cases and more arguments to cov
上级
7a1e9ec4
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
149 行增加
和
28 行删除
+149
-28
basic.py
theano/tensor/basic.py
+48
-9
test_basic.py
theano/tensor/tests/test_basic.py
+101
-19
没有找到文件。
theano/tensor/basic.py
浏览文件 @
356b55f2
...
...
@@ -2168,27 +2168,66 @@ def sqr(a):
square
=
sqr
def
cov
(
a
):
def
cov
(
X
,
y
=
None
,
rowvar
=
True
,
bias
=
False
,
ddof
=
None
,
fweights
=
None
,
aweights
=
None
):
"""Calculate the covariance matrix.
Covariance indicates the level to which two variables vary together.
If we examine N-dimensional samples, :math:`X = [x_1, x_2, ... x_N]^T`,
then the covariance matrix element :math:`C_{ij}` is the covariance of
:math:`x_i` and :math:`x_j`. The element :math:`C_{ii}` is the variance
of :math:`x_i`.
of :math:`x_i`.
Code and docstring ported from numpy.
----------
a : array_like
A 2-D array containing multiple variables and observations.
Each row of `a` represents a variable, and each column is
observations of all those variables.
Each row of `a` represents a variable, and each column is
observations of all those variables.
y : array_like, optional
An additional set of variables and observations. `y` has the same form
as that of `m`.
rowvar : bool, optional
If `rowvar` is True (default), then each row represents a
variable, with observations in the columns. Otherwise, the relationship
is transposed: each column represents a variable, while the rows
contain observations.
bias : bool, optional
Default normalization (False) is by ``(N - 1)``, where ``N`` is the
number of observations given (unbiased estimate). If `bias` is True, then
normalization is by ``N``. These values can be overridden by using the
keyword ``ddof``.
ddof : int, optional
If not ``None`` the default value implied by `bias` is overridden.
The default value is ``None``.
Returns
-------
out : The covariance matrix of the variables.
"""
a
-=
a
.
mean
(
axis
=
1
,
keepdims
=
1
)
c
=
a
.
dot
(
a
.
T
)
return
c
/
(
a
.
shape
[
1
]
-
1
)
if
fweights
is
not
None
:
raise
NotImplementedError
(
'fweights are not implemented'
)
if
aweights
is
not
None
:
raise
NotImplementedError
(
'aweights are not implemented'
)
if
not
rowvar
and
X
.
shape
[
0
]
!=
1
:
X
=
X
.
T
if
y
is
not
None
:
if
not
rowvar
and
y
.
shape
[
0
]
!=
1
:
y
=
y
.
T
X
=
theano
.
tensor
.
concatenate
((
X
,
y
),
axis
=
0
)
if
ddof
is
None
:
if
not
bias
:
ddof
=
1
else
:
ddof
=
0
# Determine the normalization
fact
=
X
.
shape
[
1
]
-
ddof
X
-=
X
.
mean
(
axis
=
1
,
keepdims
=
1
)
c
=
X
.
dot
(
X
.
T
)
c
*=
theano
.
tensor
.
constant
(
1
)
/
fact
return
c
.
squeeze
()
@_scal_elemwise
def
sqrt
(
a
):
...
...
theano/tensor/tests/test_basic.py
浏览文件 @
356b55f2
...
...
@@ -8171,25 +8171,107 @@ def test_norm():
f
=
theano
.
function
([
x
],
n
)
assert
np
.
allclose
(
f
([
1
,
1
]),
np
.
sqrt
(
2
))
def
test_cov
():
x
=
theano
.
tensor
.
matrix
(
'x'
)
c
=
theano
.
tensor
.
cov
(
x
)
f
=
theano
.
function
([
x
],
c
)
data
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
5
,
3
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
10
,
10
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
2
,
2
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
1
,
2
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
class
test_cov
(
unittest
.
TestCase
):
def
test_core
(
self
):
x
=
theano
.
tensor
.
matrix
(
'x'
)
c
=
theano
.
tensor
.
cov
(
x
)
f
=
theano
.
function
([
x
],
c
)
# basic cov function
data
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
5
,
3
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
10
,
10
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
2
,
2
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
1
,
2
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
))
def
test_rowvar
(
self
):
for
rowvar
in
[
True
,
False
]:
x
=
theano
.
tensor
.
matrix
(
'x'
)
c
=
theano
.
tensor
.
cov
(
x
,
rowvar
=
rowvar
)
f
=
theano
.
function
([
x
],
c
)
data
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
rowvar
=
rowvar
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
5
,
3
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
rowvar
=
rowvar
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
10
,
10
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
rowvar
=
rowvar
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
2
,
2
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
rowvar
=
rowvar
))
# check when variables are along the first axis
x
=
theano
.
tensor
.
matrix
(
'x'
)
c
=
theano
.
tensor
.
cov
(
x
,
rowvar
=
False
)
f
=
theano
.
function
([
x
],
c
)
data
=
np
.
asarray
(
np
.
random
.
rand
(
2
,
1
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
rowvar
=
False
))
def
test_y
(
self
):
# test y
x
=
theano
.
tensor
.
matrix
(
'x'
)
y
=
theano
.
tensor
.
matrix
(
'y'
)
c
=
theano
.
tensor
.
cov
(
x
,
y
=
y
)
f
=
theano
.
function
([
x
,
y
],
c
)
data
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
y
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
,
y
),
np
.
cov
(
data
,
y
=
y
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
5
,
3
),
dtype
=
config
.
floatX
)
y
=
np
.
asarray
(
np
.
random
.
rand
(
5
,
3
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
,
y
),
np
.
cov
(
data
,
y
=
y
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
10
,
10
),
dtype
=
config
.
floatX
)
y
=
np
.
asarray
(
np
.
random
.
rand
(
10
,
10
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
,
y
),
np
.
cov
(
data
,
y
=
y
))
data
=
np
.
asarray
(
np
.
random
.
rand
(
2
,
2
),
dtype
=
config
.
floatX
)
y
=
np
.
asarray
(
np
.
random
.
rand
(
2
,
2
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
,
y
),
np
.
cov
(
data
,
y
=
y
))
def
test_ddof
(
self
):
for
ddof
in
range
(
0
,
5
):
x
=
theano
.
tensor
.
matrix
(
'x'
)
c
=
theano
.
tensor
.
cov
(
x
,
ddof
=
ddof
)
f
=
theano
.
function
([
x
],
c
)
data
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
ddof
=
ddof
))
def
test_bias
(
self
):
for
bias
in
[
True
,
False
]:
x
=
theano
.
tensor
.
matrix
(
'x'
)
c
=
theano
.
tensor
.
cov
(
x
,
bias
=
bias
)
f
=
theano
.
function
([
x
],
c
)
data
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
bias
=
bias
))
for
ddof
in
range
(
0
,
5
):
for
bias
in
[
True
,
False
]:
x
=
theano
.
tensor
.
matrix
(
'x'
)
c
=
theano
.
tensor
.
cov
(
x
,
ddof
=
ddof
,
bias
=
bias
)
f
=
theano
.
function
([
x
],
c
)
data
=
np
.
asarray
(
np
.
random
.
rand
(
3
,
5
),
dtype
=
config
.
floatX
)
assert
np
.
allclose
(
f
(
data
),
np
.
cov
(
data
,
ddof
=
ddof
,
bias
=
bias
))
class
test_ptp
(
unittest
.
TestCase
):
def
test_scalar
(
self
):
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论