python可以构建sem模型_python-分组的熊猫DataFrames:如何将scipy.stats.sem应用于它们?...
我知道我可以通過執(zhí)行以下操作來應(yīng)用numpy方法:
dataList是DataFrames的列表(相同的列/行).
testDF = (concat(dataList, axis=1, keys=range(len(dataList)))
.swaplevel(0, 1, axis=1)
.sortlevel(axis=1)
.groupby(level=0, axis=1))
testDF.aggregate(numpy.mean)
testDF.aggregate(numpy.var)
等等.但是,如果我想計(jì)算均值(sem)的標(biāo)準(zhǔn)誤差怎么辦?
我試過了:
testDF.aggregate(scipy.stats.sem)
但它給出了一個(gè)令人困惑的錯(cuò)誤.有人知道怎么做嗎? scipy.stats方法有何不同之處?
這是一些為我重現(xiàn)錯(cuò)誤的代碼:
from scipy import stats as st
import pandas
import numpy as np
df_list = []
for ii in range(30):
df_list.append(pandas.DataFrame(np.random.rand(600, 10),
columns = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J']))
testDF = (pandas.concat(df_list, axis=1, keys=range(len(df_list)))
.swaplevel(0, 1, axis=1)
.sortlevel(axis=1)
.groupby(level=0, axis=1))
testDF.aggregate(st.sem)
這是錯(cuò)誤消息:
---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
in ()
12 .groupby(level=0, axis=1))
13
---> 14 testDF.aggregate(st.sem)
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/core/groupby.py in aggregate(self, arg, *args, **kwargs)
1177 return self._python_agg_general(arg, *args, **kwargs)
1178 else:
-> 1179 result = self._aggregate_generic(arg, *args, **kwargs)
1180
1181 if not self.as_index:
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/core/groupby.py in _aggregate_generic(self, func, *args, **kwargs)
1248 else:
1249 result = DataFrame(result, index=obj.index,
-> 1250 columns=result_index)
1251 else:
1252 result = DataFrame(result)
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/core/frame.py in __init__(self, data, index, columns, dtype, copy)
300 mgr = self._init_mgr(data, index, columns, dtype=dtype, copy=copy)
301 elif isinstance(data, dict):
--> 302 mgr = self._init_dict(data, index, columns, dtype=dtype)
303 elif isinstance(data, ma.MaskedArray):
304 mask = ma.getmaskarray(data)
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/core/frame.py in _init_dict(self, data, index, columns, dtype)
389
390 # consolidate for now
--> 391 mgr = BlockManager(blocks, axes)
392 return mgr.consolidate()
393
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/core/internals.py in __init__(self, blocks, axes, do_integrity_check)
329
330 if do_integrity_check:
--> 331 self._verify_integrity()
332
333 def __nonzero__(self):
/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/site-packages/pandas/core/internals.py in _verify_integrity(self)
404 mgr_shape = self.shape
405 for block in self.blocks:
--> 406 assert(block.values.shape[1:] == mgr_shape[1:])
407 tot_items = sum(len(x.items) for x in self.blocks)
408 assert(len(self.items) == tot_items)
AssertionError:
解決方法:
更新的答案:
看來我可以使用各種庫的工作版本來復(fù)制它.稍后,我將檢查我的家庭版本,以查看這些功能的文檔是否有所不同.
在此期間,以下內(nèi)容使用了您的確切編輯版本對我有用:
In [35]: testDF.aggregate(lambda x: st.sem(x, axis=None))
Out[35]:
Int64Index: 600 entries, 0 to 599
Data columns:
A 600 non-null values
B 600 non-null values
C 600 non-null values
D 600 non-null values
E 600 non-null values
F 600 non-null values
G 600 non-null values
H 600 non-null values
I 600 non-null values
J 600 non-null values
dtypes: float64(10)
這使我懷疑它與sem()軸約定有關(guān).它的默認(rèn)值為0,最終映射到的Pandas對象可能具有第0個(gè)怪異的軸或其他東西.當(dāng)我使用選項(xiàng)axis = None時(shí),它使應(yīng)用了該對象的對象變得雜亂無章,這使它起作用.
就像進(jìn)行健全性檢查一樣,我也這樣做,它也起作用:
In [37]: testDF.aggregate(lambda x: st.sem(x, axis=1))
Out[37]:
Int64Index: 600 entries, 0 to 599
Data columns:
A 600 non-null values
B 600 non-null values
C 600 non-null values
D 600 non-null values
E 600 non-null values
F 600 non-null values
G 600 non-null values
H 600 non-null values
I 600 non-null values
J 600 non-null values
dtypes: float64(10)
但是您應(yīng)該檢查以確保這實(shí)際上是您想要的SEM值,可能是在一些較小的示例數(shù)據(jù)上.
較舊的答案:
這可能與scipy.stats的模塊問題有關(guān)嗎?當(dāng)我使用該模塊時(shí),我必須從scipy import stats中將其稱為st或類似名稱. import scipy.stats不起作用,并調(diào)用import scipy; scipy.stats.sem給出錯(cuò)誤,指出不存在名為“ stats”的模塊.
熊貓似乎根本沒有找到這種功能.我認(rèn)為錯(cuò)誤消息應(yīng)該得到改善,因?yàn)檫@并不明顯.
>>> from scipy import stats as st
>>> import pandas
>>> import numpy as np
>>> df_list = []
>>> for ii in range(10):
... df_list.append(pandas.DataFrame(np.random.rand(10,3),
... columns = ['A', 'B', 'C']))
...
>>> df_list
# Suppressed the output cause it was big.
>>> testDF = (pandas.concat(df_list, axis=1, keys=range(len(df_list)))
... .swaplevel(0, 1, axis=1)
... .sortlevel(axis=1)
... .groupby(level=0, axis=1))
>>> testDF
>>> testDF.aggregate(np.mean)
key_0 A B C
0 0.660324 0.408377 0.374681
1 0.459768 0.345093 0.432542
2 0.498985 0.443794 0.524327
3 0.605572 0.563768 0.558702
4 0.561849 0.488395 0.592399
5 0.466505 0.433560 0.408804
6 0.561591 0.630218 0.543970
7 0.423443 0.413819 0.486188
8 0.514279 0.479214 0.534309
9 0.479820 0.506666 0.449543
>>> testDF.aggregate(np.var)
key_0 A B C
0 0.093908 0.095746 0.055405
1 0.075834 0.077010 0.053406
2 0.094680 0.092272 0.095552
3 0.105740 0.126101 0.099316
4 0.087073 0.087461 0.111522
5 0.105696 0.110915 0.096959
6 0.082860 0.026521 0.075242
7 0.100512 0.051899 0.060778
8 0.105198 0.100027 0.097651
9 0.082184 0.060460 0.121344
>>> testDF.aggregate(st.sem)
A B C
0 0.089278 0.087590 0.095891
1 0.088552 0.081365 0.098071
2 0.087968 0.116361 0.076837
3 0.110369 0.087563 0.096460
4 0.101328 0.111676 0.046567
5 0.085044 0.099631 0.091284
6 0.113337 0.076880 0.097620
7 0.087243 0.087664 0.118925
8 0.080569 0.068447 0.106481
9 0.110658 0.071082 0.084928
似乎為我工作.
標(biāo)簽:pandas,scipy,statistics,python,numpy
來源: https://codeday.me/bug/20191201/2078362.html
總結(jié)
以上是生活随笔為你收集整理的python可以构建sem模型_python-分组的熊猫DataFrames:如何将scipy.stats.sem应用于它们?...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: kali linux 双显卡,Kali上
- 下一篇: 四川中职计算机专业考的学校,四川中职学校