我有一些数据,我可以使用例如从Fitting a gamma distribution with (python) Scipy 中获取的代码来拟合伽马分布。
import scipy.stats as ss
import scipy as sp
Run Code Online (Sandbox Code Playgroud)
生成一些伽马数据:
alpha=5
loc=100.5
beta=22
data=ss.gamma.rvs(alpha,loc=loc,scale=beta,size=10000)
print(data)
# [ 202.36035683 297.23906376 249.53831795 ..., 271.85204096 180.75026301
# 364.60240242]
Run Code Online (Sandbox Code Playgroud)
这里我们将数据拟合到伽马分布:
fit_alpha,fit_loc,fit_beta=ss.gamma.fit(data)
print(fit_alpha,fit_loc,fit_beta)
# (5.0833692504230008, 100.08697963283467, 21.739518937816108)
print(alpha,loc,beta)
# (5, 100.5, 22)
Run Code Online (Sandbox Code Playgroud)
我还可以将指数分布拟合到相同的数据。然而,我想做一个似然比测试。为此,我不仅需要拟合分布,还需要返回可能性。你怎么能在python中做到这一点?
我的数学编程正在从 Maple 转移到 python。作为其中的一部分,我试图找出以数字方式执行无限求和的正确工具。
我想以数值方式计算,例如:
sum(exp(-x^2), x = -infinity..infinity)
在 Maple 中,这只是
evalf(sum(exp(-x^2), x = -infinity..infinity));
Run Code Online (Sandbox Code Playgroud)1.772637205
你能在 python 中做类似的事情,也许使用科学的 python 生态系统(scipy、numpy、sympy 等)?
我正在使用R Armadillo C++库中的随机数生成器.Armadillo允许对矩阵和向量进行高性能计算.但是,我无法看到它实际实现的是哪个随机数生成器.
Rcpp Armadillo使用的随机数生成器究竟是什么?
如果我做
ulimit -v 200000
和运行
sort largefile
我可以从top这种情况中看到最多使用 142232 Virt 和 92764 Res,但一段时间后会减少更多。
我经常想要获取一个文件的前1000行,比如说,另一个文件的前200行,并将连接管道传递给文件.
要做到这一点我能做到
head -1000 fileA > fileA-1000
head -200 fileB > fileB-200
cat fileB-200 fileA-1000 > file C
Run Code Online (Sandbox Code Playgroud)
有可能作为一个命令完成所有这些吗?
我在Haskell开始,并且对如何获得我通常用C或Python编写的简单代码的匹配性能感兴趣.考虑以下问题.
给你一长串1和0的长串n.我们希望为每个长度子串输出m该窗口中1的数量.也就是说,输出具有n-m+1不同的可能值0,m包括在内.
在C中,这与在时间上成比例n并且使用与m比特成比例的额外空间(在存储输入所需的空间之上)非常简单.你只计算第一个长度窗口中的1的数量,m然后保持两个指针,一个指向窗口的开始,一个指向结束,并根据是否指向1而另一个指向0来递增或递减或者相反.
是否有可能在Haskell中以纯粹的功能方式获得相同的理论性能?
一些可怕的代码:
chunkBits m = helper
where helper [] = []
helper xs = sum (take m xs) : helper (drop m xs)
main = print $ chunkBits 5 [0,1,1,0,1,0,0,1,0,1,0,1,1,1,0,0,0,1]
Run Code Online (Sandbox Code Playgroud) 我试图在http://scikit-learn.org/stable/auto_examples/model_selection/plot_roc.html重现该示例,但使用RandomForestClassifer.
我看不出如何转换这部分代码
# Learn to predict each class against the other
classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True,
random_state=random_state))
y_score = classifier.fit(X_train, y_train).decision_function(X_test)
Run Code Online (Sandbox Code Playgroud)
我试过了
# Learn to predict each class against the other
classifier = OneVsRestClassifier(RandomForestClassifier())
y_score = classifier.fit(X_train, y_train).decision_function(X_test)
Run Code Online (Sandbox Code Playgroud)
但我明白了
# Learn to predict each class against the other
classifier = OneVsRestClassifier(svm.SVC(kernel='linear', probability=True,
random_state=random_state))
y_score = classifier.fit(X_train, y_train).decision_function(X_test)
Run Code Online (Sandbox Code Playgroud)
有解决方法吗?
在pandas中,我可以搜索并替换包含单词fish的所有字段,例如,使用df.replace(r'.*fish.*', 'foo', regex = True).
但是如何搜索和替换不包含单词fish的所有字段?
在我的例子中,用'foo'一词替换所有不包含单词fish的字段.
例如,假设数据帧是
applefish pear
water afishfarm
Run Code Online (Sandbox Code Playgroud)
我希望将其转化为
applefish foo
foo afishfarm
Run Code Online (Sandbox Code Playgroud) 考虑这段代码?
b = [[5], [3]]
a = b[1]
b[-1] += b.pop()
print(a)
Run Code Online (Sandbox Code Playgroud)
这给了[3,3].
似乎无法通过扩展b[-1] += b.pop()来解释它b[-1] = b[-1] + b.pop().
你为什么得到这个输出?
我有一个df这样的数据框,但更大。
ID_0 ID_1 location
0 a b 1
1 a c 1
2 a b 0
3 d c 0
4 a c 0
5 a c 1
Run Code Online (Sandbox Code Playgroud)
我想添加一列来标识前两个。例如:
ID_0 ID_1 location group_ID
0 a b 1 0
1 a c 1 1
2 a b 0 0
3 d c 0 2
4 a c 0 1
5 a c 1 1
Run Code Online (Sandbox Code Playgroud)
此新列来自映射“ ab”到0,“ ac”到1和“ dc”到2。
我认为第一步是
grouped = df.groupby(['ID_0', 'ID_1'])
Run Code Online (Sandbox Code Playgroud)
但我不确定从那里去哪里。
您如何在熊猫中创建这个新专栏?