鱼C论坛

 找回密码
 立即注册
查看: 2391|回复: 2

[已解决]代码请教:cg = pc(data, alpha, indep_test, stable, uc_rule, uc_priority, mvpc...

[复制链接]
发表于 2022-10-20 20:29:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码请教:在GitHub中下载了causal—learn包,现在想要运行其中一个PC算法(https://causal-learn.readthedocs ... orithm-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

最佳答案
2022-10-22 10:03:11
本帖最后由 suchocolate 于 2022-10-22 10:04 编辑
  1. print(help(np.ndarray))
  2. Help on class ndarray in module numpy:

  3. class ndarray(builtins.object)
  4. |  ndarray(shape, dtype=float, buffer=None, offset=0,
  5. |          strides=None, order=None)
  6. |  
  7. |  An array object represents a multidimensional, homogeneous array
  8. |  of fixed-size items.  An associated data-type object describes the
  9. |  format of each element in the array (its byte-order, how many bytes it
  10. |  occupies in memory, whether it is an integer, a floating point number,
  11. |  or something else, etc.)
  12. |  
  13. |  Arrays should be constructed using `array`, `zeros` or `empty` (refer
  14. |  to the See Also section below).  The parameters given here refer to
  15. |  a low-level method (`ndarray(...)`) for instantiating an array.
  16. |  
  17. |  For more information, refer to the `numpy` module and examine the
  18. |  methods and attributes of an array.
  19. |  
  20. |  Parameters
  21. |  ----------
  22. |  (for the __new__ method; see Notes below)
  23. |  
  24. |  shape : tuple of ints
  25. |      Shape of created array.
复制代码

shape需要用元祖形式传入:
  1. data1= np.ndarray((n_samples,n_features))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2022-10-22 10:03:11 | 显示全部楼层    本楼为最佳答案   
本帖最后由 suchocolate 于 2022-10-22 10:04 编辑
  1. print(help(np.ndarray))
  2. Help on class ndarray in module numpy:

  3. class ndarray(builtins.object)
  4. |  ndarray(shape, dtype=float, buffer=None, offset=0,
  5. |          strides=None, order=None)
  6. |  
  7. |  An array object represents a multidimensional, homogeneous array
  8. |  of fixed-size items.  An associated data-type object describes the
  9. |  format of each element in the array (its byte-order, how many bytes it
  10. |  occupies in memory, whether it is an integer, a floating point number,
  11. |  or something else, etc.)
  12. |  
  13. |  Arrays should be constructed using `array`, `zeros` or `empty` (refer
  14. |  to the See Also section below).  The parameters given here refer to
  15. |  a low-level method (`ndarray(...)`) for instantiating an array.
  16. |  
  17. |  For more information, refer to the `numpy` module and examine the
  18. |  methods and attributes of an array.
  19. |  
  20. |  Parameters
  21. |  ----------
  22. |  (for the __new__ method; see Notes below)
  23. |  
  24. |  shape : tuple of ints
  25. |      Shape of created array.
复制代码

shape需要用元祖形式传入:
  1. data1= np.ndarray((n_samples,n_features))
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-10-22 21:40:29 | 显示全部楼层
suchocolate 发表于 2022-10-22 10:03
shape需要用元祖形式传入:

谢谢大佬的指导!后续自己也看懂了,我也再学习一下大佬的思路!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-5-20 07:38

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表