我有一个Dataframe,df,包含以下列:
df['ArrivalDate'] =
...
936 2012-12-31
938 2012-12-29
965 2012-12-31
966 2012-12-31
967 2012-12-31
968 2012-12-31
969 2012-12-31
970 2012-12-29
971 2012-12-31
972 2012-12-29
973 2012-12-29
...
Run Code Online (Sandbox Code Playgroud)
该列的元素是pandas.tslib.Timestamp.
我想要包括年份和月份.我认为会有简单的方法,但我无法弄清楚.
这是我尝试过的:
df['ArrivalDate'].resample('M', how = 'mean')
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
Only valid with DatetimeIndex or PeriodIndex
Run Code Online (Sandbox Code Playgroud)
然后我尝试了:
df['ArrivalDate'].apply(lambda(x):x[:-2])
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
'Timestamp' object has no attribute '__getitem__'
Run Code Online (Sandbox Code Playgroud)
有什么建议?
编辑:我有点想通了.
df.index = df['ArrivalDate']
Run Code Online (Sandbox Code Playgroud)
然后,我可以使用索引重新采样另一列.
但我仍然想要一种重新配置整个列的方法.有任何想法吗?
我注意到'r2_score'和'explain_variance_score'都是用于回归问题的内置sklearn.metrics方法.
我一直认为r2_score是模型解释的百分比差异.它与'explain_variance_score'有什么不同?
你何时会选择一个而不是另一个?
谢谢!
我今天安装了 homebrew,但并不真正知道自己在做什么,现在我的 scikit-learn 包坏了。我想通过卸载自制程序来撤消所做的一切,并尝试按照此处的提示进行操作: https: //github.com/Homebrew/homebrew/wiki/FAQ
但是,我认为自制程序安装到 /usr/bin/local 中,而不是 /usr/bin/ 中,所以我不确定是否可以使用链接中的说明。
当我最初安装 homebrew ( ruby -e "$(curl -fsSL https://raw.github.com/Homebrew/homebrew/go/install)") 时,我收到以下消息:
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1
==> The following directories will be made group writable:
/usr/local/lib/pkgconfig
/usr/local/share/man/man3
/usr/local/share/man/man5
/usr/local/share/man/man7
Run Code Online (Sandbox Code Playgroud)
我可以直接删除里面的文件吗
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1
Run Code Online (Sandbox Code Playgroud)
我害怕在卸载过程中搞砸一些东西。顺便说一句,卸载自制程序是否会将我的系统恢复到以前的状态?我该怎么做呢?
我试图理解功能选择包中的f_regression().(http://scikit-learn.org/stable/modules/generated/sklearn.feature_selection.f_regression.html#sklearn.feature_selection.f_regression)
根据文档,f_regression的第一步如下:
"1. the regressor of interest and the data are orthogonalized wrt constant regressors."
Run Code Online (Sandbox Code Playgroud)
这条线到底意味着什么?这些恒定的回归量是什么?
谢谢!
我不确定为什么我不能在R/RStudio中使用简单的后向引用.
grepl('name\1','namename')返回FALSE. grepl('(name)\1','namename')也不好.我究竟做错了什么?
谢谢!
我正在尝试理解以下代码(来自这个网站:http://jeremykun.com/2012/01/12/a-spoonful-of-python/):
def memoize(f):
cache = {}
def memoizedFunction(*args):
if args not in cache:
cache[args] = f(*args)
return cache[args]
memoizedFunction.cache = cache
return memoizedFunction
@memoize
def fib(n):
if n <= 2:
return 1
else:
return fib(n-1) + fib(n-2)
Run Code Online (Sandbox Code Playgroud)
我理解拥有缓存的好处,特别是计算Fibonacci数字之类的东西.我也明白,现在当我调用fib(4)时,它相当于调用myfun(4),其中myfun = memoize(fib).
我不明白为什么每次调用fib都不会将缓存重新分配给{}.
谁能解释一下?
谢谢!
我正在练习用while循环替换递归,我坚持以下问题.
如果你一次只能走1或2楼梯,你可以走多长度n的楼梯?
递归解决方案非常简单:
def stairs(n):
if n <= 1:
return 1
else:
return stairs(n-2) + stairs(n-1)
Run Code Online (Sandbox Code Playgroud)
我觉得迭代程序的结构应该是这样的:
def stairs_iterative(n):
ways = 0
while n > 1:
# do something
ways +=1
return ways
Run Code Online (Sandbox Code Playgroud)
但我不知道我需要把什么放在#do中.有人能帮我吗?伪代码很好!
我写了以下代码片段:
b=function(x=numeric()){print(x)}
Run Code Online (Sandbox Code Playgroud)
怎么x=numeric()办?
b('i')并且b(3)都产生正确的输出.
谢谢!