-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
3,607 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
#include<bits/stdc++.h> | ||
#include<unistd.h> | ||
using namespace std; | ||
struct Node | ||
{ | ||
double x, y; | ||
int truth; | ||
int cluster;//-1:undefined;0:noise; | ||
|
||
bool operator< (const Node &rhs) const | ||
{ | ||
if (x != rhs.x) return x < rhs.x; | ||
return y < rhs.y; | ||
} | ||
}; | ||
vector<Node> nodes; | ||
const int MAXN = 1e4 + 5; | ||
double dis[MAXN][MAXN]; | ||
double eps; | ||
int minPts; | ||
|
||
void readData() | ||
{ | ||
string s = ""; | ||
cout << "Please enter the file name , eps and minPts:" << endl; | ||
cin >> s >> eps >> minPts; | ||
freopen(("../../data/synthetic_data/" + s).c_str(), "r", stdin); | ||
double x, y; | ||
int t; | ||
while(~scanf("%lf,%lf,%d", &x, &y, &t)) | ||
{ | ||
nodes.push_back((Node){x, y, t, -1}); | ||
} | ||
} | ||
|
||
double squDistance(Node &u, Node &v) | ||
{ | ||
double dx = u.x - v.x; | ||
double dy = u.y - v.y; | ||
return dx * dx + dy * dy; | ||
} | ||
|
||
double mydistance(Node &u, Node &v) | ||
{ | ||
return sqrt(squDistance(u, v)); | ||
} | ||
|
||
void calcDis() | ||
{ | ||
for(int i = 0; i < nodes.size(); ++i) | ||
{ | ||
for(int j = i + 1; j < nodes.size(); ++j) | ||
{ | ||
dis[i][j] = dis[j][i] = squDistance(nodes[i], nodes[j]); | ||
} | ||
} | ||
} | ||
|
||
void dbscan() | ||
{ | ||
int clusterId = 0; | ||
for(int i = 0; i < nodes.size(); ++i) | ||
{ | ||
Node &cur = nodes[i]; | ||
if(cur.cluster >= 0) continue; | ||
queue<int> neighbors; | ||
set<int> se; | ||
se.insert(i); | ||
for(int j = 0; j < nodes.size(); ++j) | ||
{ | ||
if(i == j || nodes[j].cluster > 0) continue; | ||
if(mydistance(cur, nodes[j]) <= eps) | ||
{ | ||
neighbors.push(j); | ||
se.insert(j); | ||
} | ||
} | ||
if(neighbors.size() < minPts) | ||
{ | ||
nodes[i].cluster = 0; | ||
continue; | ||
} | ||
nodes[i].cluster = ++clusterId; | ||
while(!neighbors.empty()) | ||
{ | ||
int tp = neighbors.front(); | ||
neighbors.pop(); | ||
nodes[tp].cluster = clusterId; | ||
for(int j = 0; j < nodes.size(); ++j) | ||
{ | ||
if(j == i || j == tp || nodes[j].cluster > 0 || se.find(j) != se.end()) continue; | ||
if(mydistance(nodes[tp], nodes[j]) <= eps) | ||
{ | ||
se.insert(j); | ||
neighbors.push(j); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
void out(vector<Node> &nodes) | ||
{ | ||
for(int i = 0; i < nodes.size(); ++i) | ||
cout << nodes[i].x << ' ' << nodes[i].y << ' ' << nodes[i].truth << ' ' << nodes[i].cluster << endl; | ||
} | ||
|
||
void outputResult() | ||
{ | ||
freopen("../data/data.tsv", "w", stdout); | ||
puts("x\ty\tclusters"); | ||
for(int i = 0; i < nodes.size(); ++i) | ||
{ | ||
printf("%.2f\t%.2f\tCluster%d\n", nodes[i].x, nodes[i].y, nodes[i].cluster); | ||
} | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
readData(); | ||
calcDis(); | ||
dbscan(); | ||
outputResult(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
<!DOCTYPE html> | ||
<meta charset="utf-8"> | ||
<style> | ||
|
||
body { | ||
font: 10px sans-serif; | ||
} | ||
|
||
.axis path, | ||
.axis line { | ||
fill: none; | ||
stroke: #000; | ||
shape-rendering: crispEdges; | ||
} | ||
|
||
.dot { | ||
stroke: #eee; | ||
} | ||
|
||
</style> | ||
<body> | ||
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.7/d3.min.js"></script> | ||
<!–– do not use version 4 of d3, some functions API are different ––> | ||
<script> | ||
|
||
var margin = {top: 20, right: 20, bottom: 30, left: 40}, | ||
width = 960 - margin.left - margin.right, | ||
height = 500 - margin.top - margin.bottom; | ||
|
||
var x = d3.scale.linear() | ||
.range([0, width]); | ||
|
||
var y = d3.scale.linear() | ||
.range([height, 0]); | ||
|
||
var color = d3.scale.category10(); | ||
var xAxis = d3.svg.axis() | ||
.scale(x) | ||
.orient("bottom"); | ||
|
||
var yAxis = d3.svg.axis() | ||
.scale(y) | ||
.orient("left"); | ||
|
||
var svg = d3.select("body").append("svg") | ||
.attr("width", width + margin.left + margin.right) | ||
.attr("height", height + margin.top + margin.bottom) | ||
.append("g") | ||
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | ||
|
||
d3.tsv("../data/data.tsv", function(error, data) { | ||
if (error) throw error; | ||
|
||
data.forEach(function(d) { | ||
d.y = +d.y; | ||
d.x = +d.x; | ||
}); | ||
|
||
x.domain(d3.extent(data, function(d) { return d.x; })).nice(); | ||
y.domain(d3.extent(data, function(d) { return d.y; })).nice(); | ||
|
||
svg.append("g") | ||
.attr("class", "x axis") | ||
.attr("transform", "translate(0," + height + ")") | ||
.call(xAxis) | ||
.append("text") | ||
.attr("class", "label") | ||
.attr("x", width) | ||
.attr("y", -6) | ||
.style("text-anchor", "end") | ||
.text("x"); | ||
|
||
svg.append("g") | ||
.attr("class", "y axis") | ||
.call(yAxis) | ||
.append("text") | ||
.attr("class", "label") | ||
.attr("transform", "rotate(-90)") | ||
.attr("y", 6) | ||
.attr("dy", ".71em") | ||
.style("text-anchor", "end") | ||
.text("y") | ||
|
||
svg.selectAll(".dot") | ||
.data(data) | ||
.enter().append("circle") | ||
.attr("class", "dot") | ||
.attr("r", 3.5) | ||
.attr("cx", function(d) { return x(d.x); }) | ||
.attr("cy", function(d) { return y(d.y); }) | ||
.style("fill", function(d) { return color(d.clusters); }); | ||
|
||
var legend = svg.selectAll(".legend") | ||
.data(color.domain()) | ||
.enter().append("g") | ||
.attr("class", "legend") | ||
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; }); | ||
|
||
legend.append("rect") | ||
.attr("x", width - 18) | ||
.attr("width", 18) | ||
.attr("height", 18) | ||
.style("fill", color); | ||
|
||
legend.append("text") | ||
.attr("x", width - 24) | ||
.attr("y", 9) | ||
.attr("dy", ".35em") | ||
.style("text-anchor", "end") | ||
.text(function(d) { return d; }); | ||
|
||
}); | ||
|
||
</script> |
Binary file added
BIN
+85.8 KB
...ents/DongHuibing/Experiment1/SpectralClustering/results/spectral_A_0.5_10_7.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+35 KB
...s/DongHuibing/Experiment1/SpectralClustering/results/spectral_R15_0.5_10_14.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+53.3 KB
.../DongHuibing/Experiment1/SpectralClustering/results/spectral_flame_0.5_10_2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+70.9 KB
students/DongHuibing/Experiment1/SpectralClustering/results/spectral_mix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+68.2 KB
...s/DongHuibing/Experiment1/SpectralClustering/results/spectral_mix_0.5_10_20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added
BIN
+71.8 KB
...s/DongHuibing/Experiment1/SpectralClustering/results/spectral_mix_0.5_20_20.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions
86
students/DongHuibing/Experiment1/SpectralClustering/src/SpectralClustering.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
#include<bits/stdc++.h> | ||
#include<unistd.h> | ||
using namespace std; | ||
struct Node | ||
{ | ||
double x, y; | ||
int truth; | ||
int cluster;//-1:undefined;0:noise; | ||
|
||
bool operator< (const Node &rhs) const | ||
{ | ||
if (x != rhs.x) return x < rhs.x; | ||
return y < rhs.y; | ||
} | ||
}; | ||
vector<Node> nodes; | ||
const int MAXN = 1e4 + 5; | ||
double sim[MAXN][MAXN], deg[MAXN][MAXN], L[MAXN][MAXN]; | ||
double sigma; | ||
int k; | ||
|
||
void readData() | ||
{ | ||
string s = ""; | ||
cout << "Please enter the file name , sigma and k:" << endl; | ||
cin >> s >> sigma >> k; | ||
freopen(("../data/synthetic_data/" + s).c_str(), "r", stdin); | ||
double x, y; | ||
int t; | ||
while(~scanf("%lf,%lf,%d", &x, &y, &t)) | ||
nodes.push_back((Node){x, y, t, -1}); | ||
} | ||
|
||
double squDistance(Node &u, Node &v) | ||
{ | ||
double dx = u.x - v.x; | ||
double dy = u.y - v.y; | ||
return dx * dx + dy * dy; | ||
} | ||
|
||
|
||
void calcSim() | ||
{ | ||
for(int i = 0; i < nodes.size(); ++i) | ||
for(int j = i; j < nodes.size(); ++j) | ||
sim[i][j] = sim[j][i] = exp(-squDistance(nodes[i], nodes[j])*0.5/sigma/sigma); | ||
} | ||
|
||
void calcDeg() | ||
{ | ||
for(int i = 0; i < nodes.size(); ++i) | ||
for(int j = i; j < nodes.size(); ++j) deg[i][j] = deg[j][i] = 0; | ||
for(int i = 0; i < nodes.size(); ++i) | ||
for(int j = i + 1; j < nodes.size(); ++j) | ||
{ | ||
deg[i][i] += sim[i][j]; | ||
deg[j][j] += sim[i][j]; | ||
} | ||
} | ||
|
||
void calcLap() | ||
{ | ||
for(int i = 0; i < nodes.size(); ++i) | ||
for(int j = 0; j < nodes.size(); ++j) L[i][j] = sim[i][j] - deg[i][j]; | ||
} | ||
|
||
void outputResult() | ||
{ | ||
freopen("../data/data.tsv", "w", stdout); | ||
puts("x\ty\tclusters"); | ||
for(int i = 0; i < nodes.size(); ++i) | ||
{ | ||
printf("%.2f\t%.2f\tCluster%d\n", nodes[i].x, nodes[i].y, nodes[i].cluster); | ||
} | ||
} | ||
|
||
|
||
int main() | ||
{ | ||
readData(); | ||
calcSim(); | ||
calcDeg(); | ||
calcLap(); | ||
outputResult(); | ||
return 0; | ||
} |
Oops, something went wrong.