我正在尝试实现以下(分裂)聚类算法(下面是算法的简短形式,完整描述可在此处获得):
从样本x开始,i = 1,...,n被视为n个数据点的单个簇和针对所有点对定义的相异度矩阵D. 修复阈值T以决定是否拆分集群.
首先确定所有数据点对之间的距离,并选择它们之间具有最大距离(Dmax)的对.
将Dmax与T进行比较.如果Dmax> T,则使用所选对作为两个新簇中的第一个元素,将单个簇分成两个.剩下的n - 2个数据点被放入两个新集群中的一个.如果D(x_i,x_l)<D(x_j,x_l),则将x_l添加到包含x_i的新集群中,否则将其添加到包含x_i的新集群中.
在第二阶段,在两个新簇之一中找到值D(x_i,x_j)以找到簇中具有它们之间的最大距离Dmax的对.如果Dmax <T,则群集的划分停止,并考虑另一个群集.然后,对从该迭代生成的聚类重复该过程.
输出是群集数据记录的层次结构.我恳请一下如何实现聚类算法的建议.
编辑1:我附加了定义距离(相关系数)的Python函数和在数据矩阵中找到最大距离的函数.
# Read data from GitHub
import pandas as pd
df = pd.read_csv('https://raw.githubusercontent.com/nico/collectiveintelligence-book/master/blogdata.txt', sep = '\t', index_col = 0)
data = df.values.tolist()
data = data[1:10]
# Define correlation coefficient as distance of choice
def pearson(v1, v2):
# Simple sums
sum1 = sum(v1)
sum2 = sum(v2)
# Sums of the squares
sum1Sq = sum([pow(v, 2) for v in v1])
sum2Sq = sum([pow(v, 2) for v …Run Code Online (Sandbox Code Playgroud) python cluster-analysis hierarchical-clustering data-structures
第197行的脚本'/usr/local/apache2/htdocs/read.php'发生错误:只应通过引用传递变量(第196行$ext = strtolower(array_pop(explode('.',$filename)));)
if(!function_exists('mime_content_type')) {
function mime_content_type($filename) {
$mime_types = array(
'txt' => 'text/plain',
'htm' => 'text/html',
'html' => 'text/html', //ETC
);
$ext = strtolower(array_pop(explode('.',$filename)));
if (array_key_exists($ext, $mime_types)) {
return $mime_types[$ext];
}
elseif (function_exists('finfo_open')) {
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);
return $mimetype;
}
else {
return 'application/octet-stream';
}
}
}
Run Code Online (Sandbox Code Playgroud)
我正在使用来自http://php.net/manual/en/function.mime-content-type.php的这个小脚本,虽然我遇到了一个我似乎无法弄清楚的致命错误.是否有任何人有这方面的经验并提出一些指示或指出我正确的方向?
假设我有一个不同类型的列表:
即
[7, 'string 1', 'string 2', [a, b c], 'string 3', 0, (1, 2, 3)]
Run Code Online (Sandbox Code Playgroud)
是否有Pythonic方式返回'string 3'?
我试图修复我的登录脚本,在我的localhost它工作但上传到我的在线测试服务器,注销被破坏,我收到此错误:
Warning: session_destroy() [function.session-destroy]: Trying to destroy uninitialized session in htdocs/logout.php on line 17
Warning: Cannot modify header information - headers already sent by (output started at htdocs/logout.php:17) in htdocs/logout.php on line 18
Warning: Cannot modify header information - headers already sent by (output started at htdocs/logout.php:17) in www/htdocs/logout.php on line 34
Run Code Online (Sandbox Code Playgroud)
不太确定是什么导致它,因为它正在我的本地主机上工作,任何想法?
我对正则表达式还没有很好的处理,但我希望有人可以帮助我.我在这里找到了这个表达式,它几乎正是我所需要的,但我需要添加它
/^[A-Za-z][A-Za-z0-9]*(?:_[A-Za-z0-9]+)*$/
Run Code Online (Sandbox Code Playgroud)
我需要确保提交的字符串长度在{4,20} 4到20个字符之间,我已经查看了其他更简单的示例,但这个表达式有点复杂.在这种情况下,我会在哪里添加"{4,20}"?
php ×3
arrays ×2
python ×2
expression ×1
list ×1
loops ×1
preg-match ×1
reference ×1
regex ×1
session ×1