import ddbscan
import numpy as np
import matplotlib.pyplot as plt
import os
import pandas as pd
os.chdir(r'F:\转矩寻优\风电功率异常数清洗\ddbscan-master')
file = '数据(程序) - 副本 - 副本.xlsx'
dataframe = pd.read_excel(file)
dataframe = dataframe[['1秒平均风速[m/s]', '有功功率[kW]']]
data = dataframe.values.tolist()
# Test with parameters eps=5 and min_pts=5
# 对象半径
eps = 2
# 核心点的最小邻域数
min_pts = 20
scan = ddbscan.DDBSCAN(eps, min_pts)
for p in data:
scan.add_point(p, 1, "Desc")
print('scan.compute()')
scan.compute()
print('plt')
outliers_data = []
inliers_data = []
count_outliers = 0
for p in scan.points_data:
if p.cluster == -1:
nei = p.neighbourhood
index = nei[0]
outliers_data.append(scan.points[index])
count_outliers = count_outliers + p.count
else:
index = scan.points_data.index(p)
inliers_data.append(scan.points[index])
count_clusters = len(scan.clusters)
print(count_clusters)
inlier = np.array(inliers_data)
outliers = np.array(outliers_data)
title = 'ddbscan' + '_' + str(eps) + '_' + str(min_pts)
plt.title(title)
b1 = plt.scatter(inlier[:, 0], inlier[:, 1], c='green',
s=20, edgecolor='k')
c = plt.scatter(outliers[:, 0], outliers[:, 1], c='red',
s=20, edgecolor='k')
plt.axis('tight')
plt.xlabel('m/s')
plt.ylabel('kw')
plt.xticks(range(0, 21, 1))
plt.legend([b1, c],
["normal", "abnormal"],
loc="best")
plt.savefig(r'F:\转矩寻优\风电功率异常数清洗\ddbscan-master\photo/' + title + '.png')
plt.show()
出现这个问题,怎么解决哇
Traceback (most recent call last):
File "F:\转矩寻优\风电功率异常数清洗\ddbscan-master\ddbscan_wind.py", line 44, in
b1 = plt.scatter(inlier[:, 0], inlier[:, 1], c='green',
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed
这个错误提示是因为 inliers_data
和 outliers_data
中的数据没有被正确转换为多维数组。可以通过以下方法修改代码:
在导入 numpy
的时候,给它取一个别名,方便后续调用,比如把 import numpy as np
改为 import numpy as npy
。
当从 Pandas 的 dataframe 转换到 list 的时候,确保数据已经按照需要的数据类型格式正确排列。在此例中,数据应该是一个二维数组,包含两列:第一列为 1 秒平均风速,第二列为有功功率。可以使用 [:, 0]
和 [:, 1]
对数据进行索引。确保你使用了正确的索引方式。如果存在数据类型错误,例如 Excel 表格中出现空单元格或者非数值数据,也需要进行处理,否则可能会影响后续计算。
在 plt.scatter() 函数调用时,分别传入 inlier[:, 0]
和 inlier[:, 1]
以及 outliers[:, 0]
和 outliers[:, 1]
参数。注意到这些变量的维度应该是相同的,否则就会出现上述错误提示。
下面是修改后的代码示例:
import ddbscan
import numpy as npy
import matplotlib.pyplot as plt
import os
import pandas as pd
os.chdir(r'F:\转矩寻优\风电功率异常数清洗\ddbscan-master')
file = '数据(程序) - 副本 - 副本.xlsx'
dataframe = pd.read_excel(file)
dataframe = dataframe[['1秒平均风速[m/s]', '有功功率[kW]']]
data = dataframe.values.tolist()
eps = 2
min_pts = 20
scan = ddbscan.DDBSCAN(eps, min_pts)
for p in data:
scan.add_point(p, 1, "Desc")
print('scan.compute()')
scan.compute()
print('plt')
outliers_data = []
inliers_data = []
count_outliers = 0
for p in scan.points_data:
if p.cluster == -1:
nei = p.neighbourhood
index = nei[0]
outliers_data.append(scan.points[index])
count_outliers = count_outliers + p.count
else:
index = scan.points_data.index(p)
inliers_data.append(scan.points[index])
count_clusters = len(scan.clusters)
print(count_clusters)
inlier = npy.array(inliers_data)
outliers = npy.array(outliers_data)
title = 'ddbscan' + '_' + str(eps) + '_' + str(min_pts)
plt.title(title)
b1 = plt.scatter(inlier[:, 0], inlier[:, 1], c='green',
s=20, edgecolor='k')
c = plt.scatter(outliers[:, 0], outliers[:, 1], c='red',
s=20, edgecolor='k')
plt.axis('tight')
plt.xlabel('m/s')
plt.ylabel('kw')
plt.xticks(range(0, 21, 1))
plt.legend([b1, c],
["normal", "abnormal"],
loc="best")
plt.savefig(r'F:\转矩寻优\风电功率异常数清洗\ddbscan-master\photo/' + title + '.png')
plt.show()
希望这可以帮助你解决问题。