查看与发动机转速相关的车速,不同的坡度应该给出不同的档位。
我最初的反应是说这是一个线性回归问题。你没有足够的数据来做其他事情。但是,查看数据,我们可以看到它实际上是两个线性回归问题:
[![发动机转速与车速][2]][2]
在 700 转左右有一个拐点,因此您应该设计一个截止点,选择两条回归线之一,具体取决于您是高于还是低于截止点。
要确定 Python 中的回归,您可以使用任意数量的包。在 scikit-learn 中,它看起来像这样:
https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LinearRegression.html
这里给出的示例,使用 Python 控制台,是
>>> import numpy as np
>>> from sklearn.linear_model import LinearRegression
>>> X = np.array([[1, 1], [1, 2], [2, 2], [2, 3]])
>>> # y = 1 * x_0 + 2 * x_1 + 3
>>> y = np.dot(X, np.array([1, 2])) + 3
>>> reg = LinearRegression().fit(X, y)
>>> reg.score(X, y)
1.0
>>> reg.coef_
array([1., 2.])
>>> reg.intercept_
3.0000...
>>> reg.predict(np.array([[3, 5]]))
array([16.])
显然,您需要将自己的数据放在 X 和 y 中,实际上您需要两个数组用于图表的两个部分。您还将有两个 reg = LinearRegression().fit(X, y) 表达式和一个 if 语句来决定使用哪个 reg,具体取决于输入。拐点在两条回归线的交点处。
两条回归线的形式为 y = m1 x + c1 和 y = m2 x + c2,其中 m1、m2 是线的梯度,c1、c2 是截距。在交点 m1x + c1 = m2x + c2。如果你不想做数学,那么你可以使用 Shapely:
import shapely
from shapely.geometry import LineString, Point
line1 = LineString([A, B])
line2 = LineString([C, D])
int_pt = line1.intersection(line2)
point_of_intersection = int_pt.x, int_pt.y
print(point_of_intersection)
(取自 Stack Overflow 上的这个答案:How do I compute the intersection point of two lines?)
与 Sanjiv 讨论后,这里是更新的代码(改编自这里:https://machinelearningmastery.com/clustering-algorithms-with-python/)
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib
from sklearn.cluster import KMeans
matplotlib.use('TkAgg')
df = pd.read_excel("GearPredictionSanjiv.xlsx", sheet_name='FullData')
x = []
y = []
x = round(df['Engine_speed'])
y = df['Vehicle_speed']
if 'Ratio' not in df.columns or not os.path.exists('dataset.xlsx'):
df['Ratio'] = round(x/y)
model = KMeans(n_clusters=5)
# Fit the model
model.fit(X)
# Assign a cluster to each example
yhat = model.predict(X)
# Plot
plt.scatter(yhat, X['Ratio'], c=yhat, cmap=plt.cm.coolwarm)
# Show the plot
plt.show()