fixed Z-Scores
This commit is contained in:
parent
58eb8a2f40
commit
a04dfc45ca
3 changed files with 93 additions and 78 deletions
24
test.py
24
test.py
|
|
@ -24,8 +24,12 @@ def generate_random_data(size=1024, max_value=100):
|
|||
|
||||
return random_data
|
||||
|
||||
def filter_by_z_score(data, z_score_threshold):
|
||||
filtered_data = [item for item in data if item['z_score'] is not None and abs(item['z_score']) >= z_score_threshold]
|
||||
return filtered_data
|
||||
|
||||
# load the random test data
|
||||
dataset = []
|
||||
# dataset = []
|
||||
# with open('random_values.txt', 'r') as file:
|
||||
# for line in file:
|
||||
# random_data.append(int(line.strip()))
|
||||
|
|
@ -33,8 +37,15 @@ dataset = []
|
|||
dataset = generate_random_data(1024, 1024)
|
||||
dataset.sort()
|
||||
|
||||
with open('dataset.json', 'w') as r:
|
||||
r.write(json.dumps(dataset, indent=4))
|
||||
|
||||
# calculate the anomalies in the data
|
||||
analysis_results = json.loads(lyagushka(dataset, 3.0, 7))
|
||||
analysis_results = json.loads(lyagushka(dataset, 4.0, 7))
|
||||
analysis_results = filter_by_z_score(analysis_results, 1.0)
|
||||
|
||||
with open('result.json', 'w') as r:
|
||||
r.write(json.dumps(analysis_results, indent=4))
|
||||
|
||||
# Initialize plot
|
||||
plt.figure(figsize=(10, 6))
|
||||
|
|
@ -45,15 +56,12 @@ colors = plt.cm.jet(np.linspace(0, 1, len(analysis_results)))
|
|||
# Plot dataset points and assign colors based on cluster membership
|
||||
for i, result in enumerate(analysis_results):
|
||||
if result['num_elements'] > 0: # It's a cluster
|
||||
points_in_cluster = [point for point in dataset if
|
||||
result['centroid'] - result['span_length'] / 2 <= point <=
|
||||
result['centroid'] + result['span_length'] / 2]
|
||||
for point in points_in_cluster:
|
||||
for point in dataset:
|
||||
plt.plot(point, 0, 'o', color=colors[i]) # Plot points in cluster with the same color
|
||||
|
||||
# Plot a line segment for the cluster/gap Z-score in the same color
|
||||
start = result['centroid'] - result['span_length'] / 2
|
||||
end = result['centroid'] + result['span_length'] / 2
|
||||
start = result['start']
|
||||
end = result['end']
|
||||
z_score = result['z_score'] if result['z_score'] is not None else 0
|
||||
plt.plot([start, end], [z_score, z_score], color=colors[i], linewidth=2)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue