小豆豆子 发表于 2022-10-20 20:29:04

代码请教:cg = pc(data, alpha, indep_test, stable, uc_rule, uc_priority, mvpc...

代码请教:在GitHub中下载了causal—learn包,现在想要运行其中一个PC算法(https://causal-learn.readthedocs.io/en/latest/search_methods_index/Constraint-based%20causal%20discovery%20methods/PC.html#algorithm-introduction)
网址中介绍了使用步骤:
from causallearn.search.ConstraintBased.PC import pc
cg = pc(data, alpha, indep_test, stable, uc_rule, uc_priority, mvpc, correction_name, background_knowledge, verbose, show_progress)

# visualization using pydot
cg.draw_pydot_graph()

# or save the graph
from causallearn.utils.GraphUtils import GraphUtils
pyd = GraphUtils.to_pydot(cg.G)
pyd.write_png('simple_test.png')

# visualization using networkx
# cg.to_nx_graph()
# cg.draw_nx_graph(skel=False)

参数解释是:
* data:必须要求numpy.ndarray,格式为(n_samples, n_features),其中n_samples 是样本数,n_features 是特征数
* alpha:独立性测试的显著性水平,(float) in (0, 1),Default: 0.05.
* indep_test:独立性测试的方法 Default: ‘fisherz’
* stable:是否执行稳定的图骨架发现,Default: True.
* uc_rule:how unshielded colliders are oriented. Default: 0.
* uc_priority:rule of resolving conflicts between unshielded colliders. Default: 2.
* mvpc:数据是否包含缺失值,若包含,请设置为True. Default: False.
* correction_name:Missing value correction if using missing-value PC
* background_knowledge:背景知识(指定因果边及方向)
* verbose:如果要打印verbose日志输出,请设置为True,Default: False.
* show_progress:如果要在控制台显示算法的进展,则为True。默认情况下. Default: True.

正常来说更改配好环境,把data设置一下,就可以直接跑了,但是自己编写的运行时出现问题,这里的cg = pc(data, alpha, indep_test, stable, uc_rule, uc_priority, mvpc, correction_name, background_knowledge, verbose, show_progress)要如何输入数据呢?
由于是初学者我自己写的代码很糟糕,请大佬们指点:
from causallearn.search.ConstraintBased.PC import pc
n_samples=100
n_features=100
data1= np.ndarray(n_samples,n_features)

cg = pc(data1)
print(data1)
print(cg)

TypeError                                 Traceback (most recent call last)
/var/folders/dv/n35fhjq505zghgjw41r6z63r0000gn/T/ipykernel_1697/1391707814.py in <module>
      3 n_samples=100
      4 n_features=100
----> 5 data1= np.ndarray(n_samples,n_features)
      6
      7 cg = pc(data1)

TypeError: Cannot interpret '100' as a data type

suchocolate 发表于 2022-10-22 10:03:11

本帖最后由 suchocolate 于 2022-10-22 10:04 编辑

print(help(np.ndarray))
Help on class ndarray in module numpy:

class ndarray(builtins.object)
|ndarray(shape, dtype=float, buffer=None, offset=0,
|          strides=None, order=None)
|
|An array object represents a multidimensional, homogeneous array
|of fixed-size items.An associated data-type object describes the
|format of each element in the array (its byte-order, how many bytes it
|occupies in memory, whether it is an integer, a floating point number,
|or something else, etc.)
|
|Arrays should be constructed using `array`, `zeros` or `empty` (refer
|to the See Also section below).The parameters given here refer to
|a low-level method (`ndarray(...)`) for instantiating an array.
|
|For more information, refer to the `numpy` module and examine the
|methods and attributes of an array.
|
|Parameters
|----------
|(for the __new__ method; see Notes below)
|
|shape : tuple of ints
|      Shape of created array.
shape需要用元祖形式传入:
data1= np.ndarray((n_samples,n_features))

小豆豆子 发表于 2022-10-22 21:40:29

suchocolate 发表于 2022-10-22 10:03
shape需要用元祖形式传入:

谢谢大佬的指导!后续自己也看懂了,我也再学习一下大佬的思路!
页: [1]
查看完整版本: 代码请教:cg = pc(data, alpha, indep_test, stable, uc_rule, uc_priority, mvpc...