我已阅读了手册页?poly(我承认我并未完全理解),并且还阅读了" 统计学习简介"一书中对该功能的描述.
我目前的理解是,呼叫poly(horsepower, 2)应该等同于写作horsepower + I(horsepower^2).但是,这似乎与以下代码的输出相矛盾:
library(ISLR)
summary(lm(mpg~poly(horsepower,2), data=Auto))$coef
# Estimate Std. Error t value Pr(>|t|)
#(Intercept) 23.44592 0.2209163 106.13030 2.752212e-289
#poly(horsepower, 2)1 -120.13774 4.3739206 -27.46683 4.169400e-93
#poly(horsepower, 2)2 44.08953 4.3739206 10.08009 2.196340e-21
summary(lm(mpg~horsepower+I(horsepower^2), data=Auto))$coef
# Estimate Std. Error t value Pr(>|t|)
#(Intercept) 56.900099702 1.8004268063 31.60367 1.740911e-109
#horsepower -0.466189630 0.0311246171 -14.97816 2.289429e-40
#I(horsepower^2) 0.001230536 0.0001220759 10.08009 2.196340e-21
Run Code Online (Sandbox Code Playgroud)
我的问题是,为什么输出不匹配,poly真正做的是什么?
我试图取代两个"st." 和"ste." 用"st".似乎以下应该可以工作,但它不会:
require("stringr")
county <- c("st. landry", "ste. geneveve", "st. louis")
str_replace_all(county, c("st\\.", "ste\\."), "st")
Run Code Online (Sandbox Code Playgroud) 我有一个基于Python的网络抓取宠物项目,我正在尝试实现一些TDD,但我很快就遇到了问题.单元测试需要互联网连接,以及下载html文本.虽然我知道实际的解析可以使用本地文件完成,但是一些方法用于简单地重新定义URL并再次查询网站.这似乎打破了TDD的一些最佳实践(引用:罗伯特·马丁的清洁代码声称测试应该在任何环境中都可以运行).虽然这是一个Python项目,但我遇到了一个类似的问题,使用R进行Yahoo Finance抓取,我确信这种事情与语言无关.至少,这个问题似乎违反了TDD的主要指导原则,即测试应该快速运行.
tldr; 在TDD中是否有处理网络连接的最佳实践?
AbstractScraper.py
from urllib.request import urlopen
from bs4 import BeautifulSoup
class AbstractScraper:
def __init__(self, url):
self.url = url
self.dataDictionary = None
def makeDataDictionary(self):
html = urlopen(self.url)
text = html.read().decode("utf-8")
soup = BeautifulSoup(text, "lxml")
self.dataDictionary = {"html": html, "text": text, "soup": soup}
def writeSoup(self, path):
with open(path, "w") as outfile:
outfile.write(self.dataDictionary["soup"].prettify())
Run Code Online (Sandbox Code Playgroud)
TestAbstractScraper.py
import unittest
from http.client import HTTPResponse
from bs4 import BeautifulSoup
from CrackedScrapeProject.scrape.AbstractScraper import AbstractScraper
from io import StringIO
class TestAbstractScraperMethods(unittest.TestCase): …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像的数据框:
from to value sourceID targetID clustid
1 1400 1413 0.6846 3055586 3060697 1
2 323 661 0.5550 1596205 724084 1
3 323 1411 0.6817 724084 3060607 1
4 1413 1411 0.6729 3060697 3060607 1
5 1498 1411 0.6381 3111960 3060607 1
6 1478 1415 0.7423 3062164 3099199 2
7 1478 1414 0.7423 3099199 3062163 2
8 1415 1462 0.7078 3090708 3062164 2
9 1415 1463 0.7078 3062164 3090709 2
10 1462 1404 0.7078 3090708 3058341 2
11 1418 278 …Run Code Online (Sandbox Code Playgroud) 所以我使用all_shortest_paths来获取输出,如下所示:
PathsE
$res[[1]]
+ 4/990 vertices, named:
[1] Sortilin GGA1 Ubiquitin PIMT
$res[[2]]
+ 4/990 vertices, named:
[1] Sortilin TrkA PLK1 PIMT
$res[[3]]
+ 4/990 vertices, named:
[1] Sortilin APP JAB1 PIMT
Run Code Online (Sandbox Code Playgroud)
我想将其转换为数据框,以便可以对其进行操作。作为参考,我希望数据框看起来像这样:
Prot1 Prot2 Prot3 Prot4
Pathway1 Sortilin GGA1 PLK1 PIMT
Pathway2 Sortilin TrkA PLK1 PIMT
Pathway3 Sortilin APP JAB1 PIMT
Run Code Online (Sandbox Code Playgroud)
*我知道如何更改
我尝试过的轴名称
PathsDF<-as.data.frame(PathsE)
Run Code Online (Sandbox Code Playgroud)
但是我得到这个错误:
as.data.frame.default(x [[i]],可选= TRUE)中的错误:无法将类“ igraph.vs”强制转换为data.frame
我也试过这个:
PathDF <- as.data.frame(get.edgelist(PathsE))
Run Code Online (Sandbox Code Playgroud)
但是我得到这个错误
get.edgelist(PathsE)中的错误:不是图形对象
当我使用检查数据结构时
class(PathsEF)
Run Code Online (Sandbox Code Playgroud)
它说这是一个清单。但是当我使用
str(PathsE)
Run Code Online (Sandbox Code Playgroud)
它看起来像这样:
..$ :Class 'igraph.vs' atomic [1:4] 338 204 40 913 …Run Code Online (Sandbox Code Playgroud) 我正在使用 R 中的 bnlearn 包来训练贝叶斯网络。我在使用以下代码时遇到了麻烦(稍微修改了 bnlearn 示例代码):
library(bnlearn)
data(learning.test)
fitted = bn.fit(hc(learning.test), learning.test)
myfuncBN=function(){
var = names(learning.test)
obs = 2
str = paste("(", names(learning.test)[-3], "=='",
sapply(learning.test[obs,-3], as.character), "')",
sep = "", collapse = " & ")
str2 = paste("(", names(learning.test)[3], "=='",
as.character(learning.test[obs, 3]), "')", sep = "")
cpquery(fitted, eval(parse(text = str2)), eval(parse(text = str)))
}
myfuncBN()
Run Code Online (Sandbox Code Playgroud)
此代码引发错误:
结束时出错:无法将类型“闭包”强制转换为“字符”类型的向量
但是,如果 str 和 str2 是在函数 myfuncBN() 之外定义的,则它有效。有谁知道这是什么原因?
我正在尝试使用R中的树状图制作一个热图。我还试图将其设置为使颜色矩阵位于热图的底部。我知道我必须为此更改lmat的值。到目前为止,对于lmat我有类似的东西。
lmat=rbind(c(0,3,0), c(2,1,0), c(0,4,0)).
Run Code Online (Sandbox Code Playgroud)
运行它后,它要求我更新lhei和lwid值。查看文档后,我了解到这些是列宽和行高的向量。但我不了解它们的值需要多个值。例如,当我设置this时,出现此错误。
lhei must have length = nrow(lmat) = 3
Run Code Online (Sandbox Code Playgroud)
我不确定lhei的三个值对应什么。这就引出了一个更广泛的问题,lhei和lwid向量中的每个值对应什么?
r ×6
bayesian ×1
bnlearn ×1
dataframe ×1
formattable ×1
heatmap ×1
html-table ×1
igraph ×1
python ×1
regex ×1
stringr ×1
testing ×1
unit-testing ×1
web-scraping ×1