Skip to content

Commit

Permalink
cmt-20230411-1: +other_models:knn+svm
Browse files Browse the repository at this point in the history
  • Loading branch information
nicewang committed Apr 11, 2023
1 parent 14c7491 commit 32af898
Show file tree
Hide file tree
Showing 179 changed files with 3,933 additions and 0 deletions.
4 changes: 4 additions & 0 deletions other_models/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.idea
**/*.txt
**/*.png
**/*.jpg
Binary file not shown.
80 changes: 80 additions & 0 deletions other_models/kmeans_gray/batch_plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
from travel_path import *
import pylab as pl

mark = ['ob', 'or', 'og', 'ok', '^r', '+r', 'sr', 'dr', '<r', 'pr']

files = travel_txt('cluster_freqXstd/')
files = files.reshape(20,1)
files1 = np.copy(files[6:16])
files2 = np.row_stack((files[16:],files[:6]))

xlines = np.genfromtxt('Mild Outlier.txt')
xlines = xlines.reshape(20,1)
xlines1 = np.copy(xlines[6:16])
xlines2 = np.row_stack((xlines[16:],xlines[:6]))

pl.figure(figsize=(10,18))
for i in range(files1.shape[0]):
data = np.genfromtxt(files1[i,0])

SpacePoint = files1[i,0].split(' ')[-1]
SpacePoint = SpacePoint.split('.')[0] + '.' + SpacePoint.split('.')[1]
print(SpacePoint)

data_tmp = data[:,0]
max = np.max(data_tmp, axis=0)
min = np.min(data_tmp, axis=0)

pl.subplot(5,2,i+1)
for j in range(data.shape[0]):
if data[j,2] == 1:
pl.plot(data[j, 0], data[j, 1], mark[np.int(data[j, 2])], label='Relatively Abnormal Signal')
else:
pl.plot(data[j, 0], data[j, 1], mark[np.int(data[j, 2])])
pl.legend()
pl.xlabel("timeStd / V")
pl.ylabel("freqCorr")
pl.title("2-Clustering Result of freqXstd of Point at " + SpacePoint)

# x = np.arange(min, max, (max-min)/data.shape[0])
# if x.shape[0] > 1728:
# x = x[:1728]
#
# pl.axvline(xlines1[i], color='k', linewidth=0.5)
# yline = 0.55*np.ones(data.shape[0])
# pl.plot(x, yline, color='k', linewidth=0.5)
pl.tight_layout()
pl.savefig('cluster_freqXstd/fig1.png')

pl.figure(figsize=(10,18))
for i in range(files2.shape[0]):
data = np.genfromtxt(files2[i,0])

SpacePoint = files2[i,0].split(' ')[-1]
SpacePoint = SpacePoint.split('.')[0] + '.' + SpacePoint.split('.')[1]
print(SpacePoint)

data_tmp = data[:,0]
max = np.max(data_tmp, axis=0)
min = np.min(data_tmp, axis=0)

pl.subplot(5,2,i+1)
for j in range(data.shape[0]):
if data[j,2] == 1:
pl.plot(data[j, 0], data[j, 1], mark[np.int(data[j, 2])], label='Relatively Abnormal Signal')
else:
pl.plot(data[j, 0], data[j, 1], mark[np.int(data[j, 2])])
pl.legend()
pl.xlabel("timeStd / V")
pl.ylabel("freqCorr")
pl.title("2-Clustering Result of freqXstd of Point at " + SpacePoint)

# x = np.arange(min, max, (max-min)/data.shape[0])
# if x.shape[0] > 1728:
# x = x[:1728]

# pl.axvline(xlines2[i], color='k', linewidth=0.5)
# yline = 0.55*np.ones(data.shape[0])
# pl.plot(x, yline, color='k', linewidth=0.5)
pl.tight_layout()
pl.savefig('cluster_freqXstd/fig2.png')
11 changes: 11 additions & 0 deletions other_models/kmeans_gray/cluster_freqStd/2clusters/scatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pylab as pl
import numpy as np

mark = ['ob', 'or', 'og', 'ok', '^r', '+r', 'sr', 'dr', '<r', 'pr']

data = np.genfromtxt("2 clusters of point at 3.1km.txt")
for i in range(data.shape[0]):
pl.plot(data[i,0],data[i,1], mark[np.int(data[i,2])])
pl.xlabel("timeStd / V")
pl.ylabel("freqCorr")
pl.show()
11 changes: 11 additions & 0 deletions other_models/kmeans_gray/cluster_freqStd/3clusters/scatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pylab as pl
import numpy as np

mark = ['og', 'ob', 'or', 'ok', '^r', '+r', 'sr', 'dr', '<r', 'pr']

data = np.genfromtxt("3 clusters of point at 3.6km.txt")
for i in range(data.shape[0]):
pl.plot(data[i,0],data[i,1], mark[np.int(data[i,2])])
pl.xlabel("timeStd / V")
pl.ylabel("freqCorr")
pl.show()
11 changes: 11 additions & 0 deletions other_models/kmeans_gray/cluster_freqStd/4clusters/scatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pylab as pl
import numpy as np

mark = ['ok', 'og', 'ob', 'or', '^r', '+r', 'sr', 'dr', '<r', 'pr']

data = np.genfromtxt("4 clusters of point at 3.1km.txt")
for i in range(data.shape[0]):
pl.plot(data[i,0],data[i,1], mark[np.int(data[i,2])])
pl.xlabel("timeStd / V")
pl.ylabel("freqCorr")
pl.show()
11 changes: 11 additions & 0 deletions other_models/kmeans_gray/cluster_freqXstd/scatter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pylab as pl
import numpy as np

mark = ['ob', 'or', 'og', 'ok', '^r', '+r', 'sr', 'dr', '<r', 'pr']

data = np.genfromtxt("2 clusters of point at 3.6km.txt")
for i in range(data.shape[0]):
pl.plot(data[i,0],data[i,1], mark[np.int(data[i,2])])
pl.xlabel("timeStd / V")
pl.ylabel("freqCorr")
pl.show()
14 changes: 14 additions & 0 deletions other_models/kmeans_gray/freqCorr/plot.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import pylab as pl
import numpy as np

data = np.genfromtxt('freq corr of point at 3.6km.txt')

# x = np.linspace(0,1728,1728)
pl.xlim(0, data.shape[0])
# pl.ylim(0.6)
pl.plot(data)

# x = 0.65*np.ones(data.shape[0])
# pl.plot(x, color='red')

pl.show()
4 changes: 4 additions & 0 deletions other_models/kmeans_gray/freqCorr/read.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import numpy as np

data = np.genfromtxt("freq corr of point at 3.1km.txt")
print(data.shape)
24 changes: 24 additions & 0 deletions other_models/kmeans_gray/getFreqStd.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from travel_path import *

freqCorr_files = travel_txt("freqCorr/")
timeStd_files = travel_txt("timeStd/")

for i in range(freqCorr_files.shape[0]):
SpacePoint = freqCorr_files[i].split(' ')[-1]
SpacePoint = SpacePoint.split('.')[0] + '.' + SpacePoint.split('.')[1] + '.' + SpacePoint.split('.')[-1]
SpacePoint1 = timeStd_files[i].split('_')[-1]
SpacePoint1 = SpacePoint1.split('.')[0] + '.' + SpacePoint1.split('.')[1] + '.' + SpacePoint.split('.')[-1]
if SpacePoint == SpacePoint1:
print("Bingo!")
print(SpacePoint)

data_freCorr = np.genfromtxt(freqCorr_files[i])
data_timeStd = np.genfromtxt(timeStd_files[i])
data_timeStd = np.transpose(data_timeStd)
data_timeStd = data_timeStd.reshape(1728, )
# Normalization
data_timeStd = data_timeStd/np.max(data_timeStd)
data_freqStd = np.column_stack((data_timeStd, data_freCorr))
print(data_freqStd.shape)

np.savetxt("freqStd/freqStd of point at " + SpacePoint, data_freqStd)
52 changes: 52 additions & 0 deletions other_models/kmeans_gray/kmeans_2clusters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
import cv2
from travel_path import *
import math

# calculate Euclidean distance
def euclDistance(vector1, vector2):
return math.sqrt(sum(pow(vector2 - vector1, 2)))

freqStd_files = travel_txt("freqStd/")

# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)

for i in range(freqStd_files.shape[0]):
SpacePoint = freqStd_files[i].split(' ')[-1]
SpacePoint = SpacePoint.split('.')[0] + '.' + SpacePoint.split('.')[1] + '.' + SpacePoint.split('.')[-1]
print(SpacePoint)

data = np.genfromtxt(freqStd_files[i])
data_1 = data
data_1 = np.float32(data_1)
# k聚类
ret, label, center = cv2.kmeans(data_1, 2, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
print(label.shape)

suffix = np.array([[0],[1]])
center = np.column_stack((center, suffix))
print(center)
for j in range(2):
for k in range(j+1,2):
vec0 = np.array([0,0])
vec1 = center[j,:2]
vec2 = center[k,:2]
if euclDistance(vec1,vec0) > euclDistance(vec2,vec0):
tmp1 = np.copy(center[k,:])
tmp2 = np.copy(center[j,:])
center[j,:] = tmp1
center[k,:] = tmp2
print(center)

print(label)
label1 = np.copy(label)
for j in range(label.shape[0]):
for k in range(2):
if label[j,0] == center[k,2]:
label1[j,0] = np.copy(k)
print(label1)

data_new = np.column_stack((data, label1))
print(data_new.shape)
np.savetxt("cluster_freqStd/2clusters/2 clusters of point at " + SpacePoint, data_new)
50 changes: 50 additions & 0 deletions other_models/kmeans_gray/kmeans_2clusters_multi.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# -*- coding: utf-8 -*-
import cv2
from travel_path import *

freqStd_files = travel_txt("freqStd/")

# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)

for i in range(freqStd_files.shape[0]):
SpacePoint = freqStd_files[i].split(' ')[-1]
SpacePoint = SpacePoint.split('.')[0] + '.' + SpacePoint.split('.')[1] + '.' + SpacePoint.split('.')[-1]
print(SpacePoint)

data = np.genfromtxt(freqStd_files[i])
data_multi = []
for j in range(data.shape[0]):
if data_multi == []:
data_multi = data[j,0] * data[j,1]
else:
data_tmp = data[j,0] * data[j,1]
data_multi = np.column_stack((data_multi, data_tmp))
data_multi = np.float32(data_multi)
# k聚类
ret, label, center = cv2.kmeans(data_multi, 2, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
print(label.shape)

suffix = np.array([[0],[1]])
center = np.column_stack((center, suffix))
print(center)
for j in range(2):
for k in range(j+1,2):
if center[j,0] > center[k,0]:
tmp1 = np.copy(center[k,:])
tmp2 = np.copy(center[j,:])
center[j,:] = tmp1
center[k,:] = tmp2
print(center)

print(label)
label1 = np.copy(label)
for j in range(label.shape[0]):
for k in range(2):
if label[j,0] == center[k,1]:
label1[j,0] = np.copy(k)
print(label1)

data_new = np.column_stack((data, label1))
print(data_new.shape)
np.savetxt("cluster_freqXstd/2 clusters of point at " + SpacePoint, data_new)
52 changes: 52 additions & 0 deletions other_models/kmeans_gray/kmeans_3clusters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
import cv2
from travel_path import *
import math

# calculate Euclidean distance
def euclDistance(vector1, vector2):
return math.sqrt(sum(pow(vector2 - vector1, 2)))

freqStd_files = travel_txt("freqStd/")

# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)

for i in range(freqStd_files.shape[0]):
SpacePoint = freqStd_files[i].split(' ')[-1]
SpacePoint = SpacePoint.split('.')[0] + '.' + SpacePoint.split('.')[1] + '.' + SpacePoint.split('.')[-1]
print(SpacePoint)

data = np.genfromtxt(freqStd_files[i])
data_1 = data
data_1 = np.float32(data_1)
# k聚类
ret, label, center = cv2.kmeans(data_1, 3, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
print(label.shape)

suffix = np.array([[0],[1],[2]])
center = np.column_stack((center, suffix))
print(center)
for j in range(3):
for k in range(j+1,3):
vec0 = np.array([0, 0])
vec1 = center[j, :2]
vec2 = center[k, :2]
if euclDistance(vec1, vec0) > euclDistance(vec2, vec0):
tmp1 = np.copy(center[k,:])
tmp2 = np.copy(center[j,:])
center[j,:] = tmp1
center[k,:] = tmp2
print(center)

print(label)
label1 = np.copy(label)
for j in range(label.shape[0]):
for k in range(3):
if label[j,0] == center[k,2]:
label1[j,0] = np.copy(k)
print(label1)

data_new = np.column_stack((data, label1))
print(data_new.shape)
np.savetxt("cluster_freqStd/3clusters/3 clusters of point at " + SpacePoint, data_new)
52 changes: 52 additions & 0 deletions other_models/kmeans_gray/kmeans_4clusters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
import cv2
from travel_path import *
import math

# calculate Euclidean distance
def euclDistance(vector1, vector2):
return math.sqrt(sum(pow(vector2 - vector1, 2)))

freqStd_files = travel_txt("freqStd/")

# define criteria, number of clusters(K) and apply kmeans()
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)

for i in range(freqStd_files.shape[0]):
SpacePoint = freqStd_files[i].split(' ')[-1]
SpacePoint = SpacePoint.split('.')[0] + '.' + SpacePoint.split('.')[1] + '.' + SpacePoint.split('.')[-1]
print(SpacePoint)

data = np.genfromtxt(freqStd_files[i])
data_1 = data
data_1 = np.float32(data_1)
# k聚类
ret, label, center = cv2.kmeans(data_1, 4, None, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)
print(label.shape)

suffix = np.array([[0],[1],[2],[3]])
center = np.column_stack((center, suffix))
print(center)
for j in range(4):
for k in range(j+1,4):
vec0 = np.array([0, 0])
vec1 = center[j, :2]
vec2 = center[k, :2]
if euclDistance(vec1, vec0) > euclDistance(vec2, vec0):
tmp1 = np.copy(center[k,:])
tmp2 = np.copy(center[j,:])
center[j,:] = tmp1
center[k,:] = tmp2
print(center)

print(label)
label1 = np.copy(label)
for j in range(label.shape[0]):
for k in range(4):
if label[j,0] == center[k,2]:
label1[j,0] = np.copy(k)
print(label1)

data_new = np.column_stack((data, label1))
print(data_new.shape)
np.savetxt("cluster_freqStd/4clusters/4 clusters of point at " + SpacePoint, data_new)
Loading

0 comments on commit 32af898

Please sign in to comment.