- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试使用 sklearn.cluster
中的 kmeans
对一些数据进行聚类。但我想先美化我的数据。我有一个包含以下三列的 Pandas df(有几百行):
1) zipcode
2) highclust
3) callclust
我想美白(使用scipy.cluster.vq.whiten
)。从我到目前为止的研究来看, df 列必须在美白之前转换为矩阵。所以我做了:
features = df.as_matrix(columns = ['highclust', 'callclust'])
然后我使用了whiten(features)
。
效果很好,但现在我想将这些值恢复到原始 df 中。
问题是我没有任何值可以将其合并回去。如果我在创建 features
时将 zipcode
带入 features
中,zipcode 会与 highclust
和 callclust
一起变白,渲染它没用。
最佳答案
最简单的解决方案是先保存邮政编码,美化,然后重新应用邮政编码。
from scipy.cluster.vq import whiten
import pandas as pd
zips = df.zipcode
df = pd.DataFrame(whiten(df), columns=df.columns)
df['zipcode'] = zips
您也可以自己进行计算,而不是使用 lambda 函数使用 scipy。
np.random.seed(0)
whiten_cols = ['highclust', 'callclust']
df = pd.DataFrame({'zipcode': [1, 2, 3, 4, 5],
'highclust': np.random.randn(5),
'callclust': np.random.randn(5)})[['zipcode'] + whitencols]
>>> df
zipcode highclust callclust
0 1 1.764052 -0.977278
1 2 0.400157 0.950088
2 3 0.978738 -0.151357
3 4 2.240893 -0.103219
4 5 1.867558 0.410599
>>> df.std()
zipcode 1.581139
highclust 0.745445
callclust 0.717038
dtype: float64
# Whiten data.
df.loc[:, whiten_cols] = df[whiten_cols].apply(lambda col: col / col.std())
>>> df
zipcode highclust callclust
0 1 2.366442 -1.362937
1 2 0.536803 1.325018
2 3 1.312958 -0.211087
3 4 3.006115 -0.143952
4 5 2.505293 0.572631
>>> df.std()
zipcode 1.581139
highclust 1.000000
callclust 1.000000
dtype: float64
默认情况下,Pandas 将标准偏差归一化为 N-1。这在大型数据集上不会成为问题,但您可以将 scipy 结果与以下内容相匹配:
df.loc[:, whiten_cols] = df[whiten_cols].apply(lambda col: col / col.std(ddof=0))
>>> df
zipcode highclust callclust
0 1 2.645763 -1.523810
1 2 0.600164 1.481415
2 3 1.467932 -0.236002
3 4 3.360938 -0.160943
4 5 2.801003 0.640221
如果你更喜欢直接使用 scipy:
# After resetting the seed and reinitializing the dataframe.
df.loc[:, whiten_cols] = whiten(df[whiten_cols].values)
>>> df
zipcode highclust callclust
0 1 2.645763 -1.523810
1 2 0.600164 1.481415
2 3 1.467932 -0.236002
3 4 3.360938 -0.160943
4 5 2.801003 0.640221
>>> df.std()
zipcode 1.581139
highclust 1.118034
callclust 1.118034
dtype: float64
scipy.cluster.vq.whiten(obs, check_finite=True)[source] Normalize a group of observations on a per feature basis.
Before running k-means, it is beneficial to rescale each feature dimension of the observation set with whitening. Each feature is divided by its standard deviation across all observations to give it unit variance.
这是 source code对于美白
:
obs = _asarray_validated(obs, check_finite=check_finite)
std_dev = std(obs, axis=0)
zero_std_mask = std_dev == 0
if zero_std_mask.any():
std_dev[zero_std_mask] = 1.0
warnings.warn("Some columns have standard deviation zero. "
"The values of these columns will not change.",
RuntimeWarning)
return obs / std_dev
关于python - kmeans 聚类之前的 Scipy 白化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34373448/
我在使用 cx_freeze 和 scipy 时无法编译 exe。特别是,我的脚本使用 from scipy.interpolate import griddata 构建过程似乎成功完成,但是当我尝试
是否可以通过函数在 scipy 中定义一个稀疏矩阵,而不是列出所有可能的值?在文档中,我看到可以通过以下方式创建稀疏矩阵 There are seven available sparse matrix
SciPy为非线性最小二乘问题提供了两种功能: optimize.leastsq()仅使用Levenberg-Marquardt算法。 optimize.least_squares()允许我们选择Le
SciPy 中的求解器能否处理复数值(即 x=x'+i*x")?我对使用 Nelder-Mead 类型的最小化函数特别感兴趣。我通常是 Matlab 用户,我知道 Matlab 没有复杂的求解器。如果
我有看起来像这样的数据集: position number_of_tag_at_this_position 3 4 8 6 13 25 23 12 我想对这个数据集应用三次样条插值来插值标签密度;为此
所以,我正在处理维基百科转储,以计算大约 5,700,000 个页面的页面排名。这些文件经过预处理,因此不是 XML 格式。 它们取自 http://haselgrove.id.au/wikipedi
Scipy 和 Numpy 返回归一化的特征向量。我正在尝试将这些向量用于物理应用程序,我需要它们不被标准化。 例如a = np.matrix('-3, 2; -1, 0') W,V = spl.ei
基于此处提供的解释 1 ,我正在尝试使用相同的想法来加速以下积分: import scipy.integrate as si from scipy.optimize import root, fsol
这很容易重新创建。 如果我的脚本 foo.py 是: import scipy 然后运行: python pyinstaller.py --onefile foo.py 当我启动 foo.exe 时,
我想在我的代码中使用 scipy.spatial.distance.cosine。如果我执行类似 import scipy.spatial 或 from scipy import spatial 的操
Numpy 有一个基本的 pxd,声明它的 c 接口(interface)到 cython。是否有用于 scipy 组件(尤其是 scipy.integrate.quadpack)的 pxd? 或者,
有人可以帮我处理 scipy.stats.chisquare 吗?我没有统计/数学背景,我正在使用来自 https://en.wikipedia.org/wiki/Chi-squared_test 的
我正在使用 scipy.odr 拟合数据与权重,但我不知道如何获得拟合优度或 R 平方的度量。有没有人对如何使用函数存储的输出获得此度量有建议? 最佳答案 res_var Output 的属性是所谓的
我刚刚下载了新的 python 3.8,我正在尝试使用以下方法安装 scipy 包: pip3.8 install scipy 但是构建失败并出现以下错误: **Failed to build sci
我有 my own triangulation algorithm它基于 Delaunay 条件和梯度创建三角剖分,使三角形与梯度对齐。 这是一个示例输出: 以上描述与问题无关,但对于上下文是必要的。
这是一个非常基本的问题,但我似乎找不到好的答案。 scipy 到底计算什么内容 scipy.stats.norm(50,10).pdf(45) 据我了解,平均值为 50、标准差为 10 的高斯中像 4
我正在使用 curve_fit 来拟合一阶动态系统的阶跃响应,以估计增益和时间常数。我使用两种方法。第一种方法是在时域中拟合从函数生成的曲线。 # define the first order dyn
让我们假设 x ~ Poisson(2.5);我想计算类似 E(x | x > 2) 的东西。 我认为这可以通过 .dist.expect 运算符来完成,即: D = stats.poisson(2.
我正在通过 OpenMDAO 使用 SLSQP 来解决优化问题。优化工作充分;最后的 SLSQP 输出如下: Optimization terminated successfully. (Exi
log( VA ) = gamma - (1/eta)log[alpha L ^(-eta) + 测试版 K ^(-eta)] 我试图用非线性最小二乘法估计上述函数。我为此使用了 3 个不同的包(Sc
我是一名优秀的程序员,十分优秀!