为了说明它的一般形式,我正在寻找一种方式来加入几个点用渐变色线使用matplotlib,我不会在任何地方找到它.更具体地说,我正在绘制一条带有一条颜色线的2D随机游走.但是,由于这些点具有相关的序列,我想查看该图并查看数据的移动位置.渐变色线可以解决这个问题.或者是一条渐变透明度的线.
我只是想改善数据的虚拟化.看看这个由ggplot2包产生的漂亮图像.我在matplotlib中寻找相同的图像.谢谢.

我正在使用Python的fastcluster包来计算大量观测值上的分层聚类过程的链接矩阵。
到目前为止,fastcluster 的方法表现良好,它能够对一组观测值进行聚类,这比使用相同内存量进行计算的情况要linkage_vector()大得多。scipy.linkage()
完成此操作后,我现在想要检查聚类结果并计算相对于原始数据的共表相关系数。通常的过程是首先计算共表距离矩阵,然后检查与原始数据的相关性。使用 scipy 的cophenet()方法,它看起来像这样:
import fastcluster as fc
import numpy as np
from scipy.cluster.hierarchy import cophenet
X = np.random.random((1000,10)) # Original data (1000 observations)
Z = fc.linkage_vector(X) # Clustering
orign_dists = fc.pdist(X) # Matrix of original distances between observations
cophe_dists = cophenet(Z) # Matrix of cophenetic distances between observations
# What I really want at the end of the day is
corr_coef = np.corrcoef(orign_dists, cophe_dists)[0,1]
Run Code Online (Sandbox Code Playgroud)
然而,当观察集非常大时,这不起作用(只需将 1000 替换为 …
我在 Python 3 中做了一个凝聚层次聚类实验,我发现scipy.cluster.hierarchy.cut_tree()没有为某些输入链接矩阵返回请求的聚类数。所以,现在我知道有在一个bug cut_tree()函数(如描述在这里)。
但是,我需要能够获得平面聚类,并k为我的数据点分配不同的标签。您知道k从任意输入链接矩阵中获得带有标签的平面聚类的算法Z吗?我的问题归结为:如何cut_tree()在没有错误的情况下从头开始计算什么?
您可以使用此数据集测试您的代码。
from scipy.cluster.hierarchy import linkage, is_valid_linkage
from scipy.spatial.distance import pdist
## Load dataset
X = np.load("dataset.npy")
## Hierarchical clustering
dists = pdist(X)
Z = linkage(dists, method='centroid', metric='euclidean')
print(is_valid_linkage(Z))
## Now let's say we want the flat cluster assignement with 10 clusters.
# If cut_tree() was working we would do
from scipy.cluster.hierarchy import cut_tree
cut = cut_tree(Z, 10)
Run Code Online (Sandbox Code Playgroud)
旁注:另一种方法也许可以是使用 …
我在下面的第三个示例图中遇到了这种不同的行为。为什么我可以使用pandas line()和area()绘图正确编辑 x 轴的刻度,但不能使用bar()?修复(一般)第三个示例的最佳方法是什么?
import numpy as np
import pandas as pd
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
x = np.arange(73,145,1)
y = np.cos(x)
df = pd.Series(y,x)
ax1 = df.plot.line()
ax1.xaxis.set_major_locator(ticker.MultipleLocator(10))
ax1.xaxis.set_minor_locator(ticker.MultipleLocator(2.5))
plt.show()
ax2 = df.plot.area(stacked=False)
ax2.xaxis.set_major_locator(ticker.MultipleLocator(10))
ax2.xaxis.set_minor_locator(ticker.MultipleLocator(2.5))
plt.show()
ax3 = df.plot.bar()
ax3.xaxis.set_major_locator(ticker.MultipleLocator(10))
ax3.xaxis.set_minor_locator(ticker.MultipleLocator(2.5))
plt.show()
Run Code Online (Sandbox Code Playgroud) 我正在使用fastcluster包与scipy.cluster.hierarchy模块函数进行凝聚层次聚类 ( AHC ) 实验,在 中,我发现cut_tree()函数的令人费解的行为。Python 3
我毫无问题地对数据进行聚类,并Z使用linkage_vector()with获得链接矩阵method=ward。然后,我想切割树状图树以获得固定数量的簇(例如 33),并且我使用 正确地执行此操作cut_tree(Z, n_clusters=33)。(请记住,AHC 是一种确定性方法,生成连接所有数据点的二叉树,这些数据点位于树的叶子;您可以在任何级别查看这棵树,以“查看”您最终想要的集群数量;所有cut_tree() 的作用是返回一组从 0 到 n_clusters - 1 的“n_cluster”整数标签,归因于数据集的每个点。)
我在其他实验中已经做过很多次,并且总是得到我请求的集群数量。问题是,对于这个数据集,当我要求cut_tree()33 个簇时,它只给我 32 个。我不明白为什么会出现这种情况。这可能是一个错误吗?您知道 的任何错误吗cut_tree()?我尝试调试这种行为,并使用 scipy 的links()函数执行相同的聚类实验。将生成的链接矩阵作为输入,cut_tree()我没有得到意外数量的簇作为输出。我还验证了两种方法输出的链接矩阵不相等。
我使用的 [数据集]由 10680 个向量组成,每个向量有 20 个维度。检查以下实验:
import numpy as np
import fastcluster as fc
import scipy.cluster.hierarchy as hac
from scipy.spatial.distance import pdist
### *Load dataset (10680 vectors, each with 20 …Run Code Online (Sandbox Code Playgroud) python debugging hierarchical-clustering data-analysis scipy