I'm trying to plot a simple line fit using sklearn linear regression. Unfortunately, for some reason, the line fit itself is not drawn.
import numpy as np
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
def main():
#loading file
arr = np.genfromtxt("data.csv", dtype=float, delimiter=";", names=True)
#voltage
X = np.array([[i[0] for i in arr]])
print("Voltage")
print(X)
#left wheel
LEFT_Y = np.array([[i[1] for i in arr]])
print("Left wheel")
print(LEFT_Y)
#right wheel
RIGHT_Y = np.array([[i[2] for i in arr]])
print("Right wheel")
print(RIGHT_Y)
model = LinearRegression()
model.fit(X, LEFT_Y)
plt.scatter(X, LEFT_Y, color='blue')
plt.plot(X, model.predict(X), color='red')
plt.show()
if __name__ == "__main__":
main()
My problem was in creating incorrect NumPy arrays. They have to be created this way:
X = np.array([i[0] for i in arr]) #no second braket
The same for other arrays.