小编her*_*rfz的帖子

如何将pandas DataFrame的第一列作为一个系列?

我试过了:

x=pandas.DataFrame(...)
s = x.take([0], axis=1)
Run Code Online (Sandbox Code Playgroud)

s获得一个DataFrame,而不是一个系列.

python series dataframe pandas

126
推荐指数
4
解决办法
26万
查看次数

Python统计包:statsmodel和scipy.stats之间的区别

我需要一些关于为Python选择统计软件包的建议,我做了很多搜索,但不确定我是否做得对,特别是statsmodels和scipy.stats之间的区别.

我知道的一件事是scikits命名空间是scipy的特定"分支",而以前的scikits.statsmodels现在称为statsmodels.另一方面,还有scipy.stats.两者之间有什么区别,哪一个是Python 统计软件包?

谢谢.

- 编辑 -

我更改了标题,因为有些答案与问题没有关系,我认为这是因为标题不够明确.

python scipy scikits statsmodels

21
推荐指数
2
解决办法
1万
查看次数

在scikit中结合网格搜索和交叉验证学习

为了改进支持向量机的结果,我必须使用网格搜索来搜索更好的参数和交叉验证.我不确定如何在scikit-learn中将它们结合起来.网格搜索搜索最佳参数(http://scikit-learn.org/stable/modules/grid_search.html)和交叉验证避免过度拟合(http://scikit-learn.org/dev/modules/cross_validation.html)

#GRID SEARCH
from sklearn import grid_search
parameters = {'kernel':('linear', 'rbf'), 'C':[1, 10]}
svr = svm.SVC()
clf = grid_search.GridSearchCV(svr, parameters)
#print(clf.fit(X, Y))

#CROSS VALIDATION
from sklearn import cross_validation
X_train, X_test, y_train, y_test = cross_validation.train_test_split(X, Y, test_size=0.4, random_state=0)
clf = svm.SVC(kernel='linear', C=1).fit(X_train, y_train)

print("crossvalidation")
print(clf.score(X_test, y_test))
clf = svm.SVC(kernel='linear', C=1)
scores = cross_validation.cross_val_score(clf, X, Y, cv=3)
print(scores )
Run Code Online (Sandbox Code Playgroud)

结果:

GridSearchCV(cv=None,
   estimator=SVC(C=1.0, cache_size=200, class_weight=None, coef0=0.0, degree=3, gamma=0.0,
kernel=rbf, probability=False, shrinking=True, tol=0.001, verbose=False),
   estimator__C=1.0, estimator__cache_size=200,
   estimator__class_weight=None, estimator__coef0=0.0,
   estimator__degree=3, estimator__gamma=0.0, …
Run Code Online (Sandbox Code Playgroud)

python svm scikit-learn cross-validation

11
推荐指数
1
解决办法
1万
查看次数

如何在Pandas DataFrame上计算滚动累积产品

我在pandas DataFrame中有一系列的回归,滚动测试和滚动alpha.如何计算DataFrame的alpha列的滚动年化alpha?(我想做相当于= PRODUCT(1+ [尾随12个月]) - excel中的1)

            SPX Index BBOEGEUS Index    Beta      Alpha
2006-07-31   0.005086    0.001910    1.177977   -0.004081
2006-08-31   0.021274    0.028854    1.167670    0.004012
2006-09-30   0.024566    0.009769    1.101618   -0.017293
2006-10-31   0.031508    0.030692    1.060355   -0.002717
2006-11-30   0.016467    0.031720    1.127585    0.013153
Run Code Online (Sandbox Code Playgroud)

我很惊讶地看到pandas中没有内置"滚动"功能,但是我希望有人可以帮助我使用pd.rolling_apply然后应用于df ['Alpha']列的函数.

在此先感谢您提供的任何帮助.

python finance time-series pandas

8
推荐指数
3
解决办法
1万
查看次数

Scipy树状图叶子标签颜色

是否可以为Scipy的树状图的叶子标签指定颜色?我无法从文档中找到它.这是我到目前为止所尝试的:

from scipy.spatial.distance import pdist, squareform
from scipy.cluster.hierarchy import linkage, dendrogram

distanceMatrix = pdist(subj1.ix[:,:3])
dendrogram(linkage(distanceMatrix, method='complete'), 
           color_threshold=0.3, 
           leaf_label_func=lambda x: subj1['activity'][x],
           leaf_font_size=12)
Run Code Online (Sandbox Code Playgroud)

谢谢.

python hierarchical-clustering dendrogram scipy

5
推荐指数
1
解决办法
6113
查看次数

从多索引 DataFrame 中提取数据

我是 pandas 新手,我正在尝试从 MultiIndex'ed DataFrame 中提取数据

pandas 是否可以从 MultiIndex 对象中选择一系列值,例如下面的示例 DataFrame 我想从第一级(bar、baz、foo 和 qux)以及“one”和“所有列的第二级中的“2”。那可能吗?

arrays = [np.array(['bar', 'bar', 'bar', 'baz', 'baz', 'baz', 'foo', 'foo', 'foo', 'qux', 'qux','qux']),
np.array(['one', 'two','three','one', 'two', 'three','one', 'two','three', 'one', 'two','three'])]

df = pd.DataFrame(randn(12, 6), index=arrays)
                  0         1         2         3         4         5
bar one   -0.031447  0.084358 -0.045284 -0.073702 -0.566905 -0.541734
    two   -0.381897  0.422047 -0.527828  0.419984 -0.920186  0.643190
    three  0.082314  2.584901  1.149755 -0.741753  0.696301 -0.132365
baz one    0.637182 -0.210955 -0.329989  0.021509 -0.483201 -1.194727
    two    3.602497 -0.010458  1.734119 -0.332384 …
Run Code Online (Sandbox Code Playgroud)

python dataframe pandas

1
推荐指数
1
解决办法
243
查看次数