最后趋势线的确定需要通过LinearRegression联合Cluster共同确定¶
elif mode == K_TREND_KMEAN:
for xy in other:
if len(xy) == 2:
xPred = xy[0]
yPred = xy[1]
if xPred.shape[0] == 2:
pass
elif xPred.shape[0] == 3:
d = np.array([np.ravel(xPred), np.ravel(yPred)]).T
dPd = pd.DataFrame(d, columns=['X', 'Y'])
dPd.sort(['X'])
xPred = dPd.iloc[0:2, 0:1].values
yPred = dPd.iloc[0:2, 1:2].values
else:
ZLog.info('KMeans effect')
select_k = 2
kmeans = KMeans(n_clusters=select_k, init='random')
d = np.array([np.ravel(xPred), np.ravel(yPred)]).T
kmeans.fit(d)
y_kmeans = kmeans.predict(d)
dPd = pd.DataFrame(d, columns=['X', 'Y'])
dPd['Cluster'] = y_kmeans
dPdMin = dPd.groupby(['Cluster', 'Y', 'X'])['X', 'Y'].min()
xPred = [dPdMin.loc[x, :].values[0][0] for x in xrange(0, select_k)]
yPred = [yOrg[x] for x in xPred]
xPred = np.array(xPred).reshape(-1, 1)
yPred = np.array(yPred).reshape(-1, 1)
linreg = LinearRegression()
linreg.fit(xPred, yPred)
xPred = np.array(xOrg).reshape(-1, 1)
yPred = linreg.predict(xPred)
xPred = np.ravel(xPred)
yPred = np.ravel(yPred)