scipy polyBfgs 在一定范围内寻找局部最小值主要是注意参数范围n
聚类多个局部最小值,通过elbow寻找最优k个全局最小
每个类别中选出最小值,组成趋势
一种趋势线绘制方式思路¶
- scipy polyBfgs 在一定范围内寻找局部最小值主要是注意参数范围n(优化方式也可以使用np.polynomial.Chebyshev先反出多项式,再optimize.fmin_bfgs速度回快点)
- 聚类多个局部最小值,通过elbow寻找最优k个全局最小(聚类效果如下所示)
- 每个类别中选出最小值,组成趋势
TLineAnalyse.findMinLocal(x, y, show=True)
最后趋势线的确定需要通过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)
TLineTrend.plotUpTrend(x, y, TLineTrend.K_TREND_KMEAN)