如何从图表中获得二阶导数/倾角或生成最佳每股收益值

2024-10-05 14:28:49 发布

您现在位置:Python中文网/ 问答频道 /正文

数据集如下所示

 ,id,revenue ,profit
0,101,779183,281257
1,101,144829,838451
2,101,766465,757565
3,101,353297,261071
4,101,1615461,275760
5,101,246731,949229
6,101,951518,301016
7,101,444669,430583

代码如下

import pandas as pd;
from sklearn.cluster import DBSCAN
import matplotlib.pyplot as plt
import numpy as np
from sklearn.preprocessing import StandardScaler
import seaborn as sns
from sklearn.neighbors import NearestNeighbors
df = pd.read_csv('1.csv',index_col=None)
df1 = StandardScaler().fit_transform(df)
dbsc = DBSCAN(eps = 2.5, min_samples = 20).fit(df1)
labels = dbsc.labels_

我的df形状是1999

我从下面的方法中得到了倾角值eps值,从图中可以清楚地看出,eps=2.5

enter image description here

下面是找到最佳每股收益值的方法

ns = 5
nbrs = NearestNeighbors(n_neighbors=ns).fit(df3)
distances, indices = nbrs.kneighbors(df3)
distanceDec = sorted(distances[:,ns-1], reverse=True)
plt.plot(indices[:,0], distanceDec)
#plt.plot(list(range(1,2000)), distanceDec)
  • 如何通过系统自动找到图形中的倾角,以预测最佳eps?在不查看图形的情况下,我的系统必须告诉besteps

Tags: fromimportdfasneighborspltsklearneps
1条回答
网友
1楼 · 发布于 2024-10-05 14:28:49

如果我理解正确,您正在寻找ε(x)图中出现的拐点的精确y值(应该在2.0左右),对吗

如果这是正确的,当你的曲线ε(x)时,问题将简化为:

  1. 计算曲线的二阶导数:ε“”(x)
  2. 求二阶导数的零:x0
  3. 恢复优化的ε值,只需将零插入曲线:ε(x0)

在此,我附上我的答案,基于这两个其他堆栈溢出答案: https://stackoverflow.com/a/26042315/10489040(计算数组的导数) https://stackoverflow.com/a/3843124/10489040(在数组中查找零)

import numpy as np
import matplotlib.pyplot as plt

# Generating x data range from -1 to 4 with a step of 0.01
x = np.arange(-1, 4, 0.01)

# Simulating y data with an inflection point as y(x) = x³ - 5x² + 2x
y = x**3 - 5*x**2 + 2*x

# Plotting your curve
plt.plot(x, y, label="y(x)")

# Computing y 1st derivative of your curve with a step of 0.01 and plotting it
y_1prime = np.gradient(y, 0.01)
plt.plot(x, y_1prime, label="y'(x)")

# Computing y 2nd derivative of your curve with a step of 0.01 and plotting it
y_2prime = np.gradient(y_1prime, 0.01)
plt.plot(x, y_2prime, label="y''(x)")

# Finding the index of the zero (or zeroes) of your curve
x_zero_index = np.where(np.diff(np.sign(y_2prime)))[0]

# Finding the x value of the zero of your curve
x_zero_value = x[x_zero_index][0]

# Finding the y value corresponding to the x value of the zero
y_zero_value = y[x_zero_index][0]

# Reporting
print(f'The inflection point of your curve is {y_zero_value:.3f}.')

enter image description here

在任何情况下,请记住拐点(2.0左右)与出现在2.5左右的“下倾”点不匹配

相关问题 更多 >