'trouble with linear regression plotting
hi so im trying to use the reg.fit function to plot a graph of the co2 emitted from a diet. the code runs ok but the graph that is plotted shows all of the y values bunched up at the right side of the graph so I think theres an issue with the way the x values are being passed.
this is my code
data = pd.read_csv("food.csv")
plt.figure(figsize=[15, 10])
plt.rc('xtick', labelsize=3)
imp_mean = SimpleImputer(missing_values=np.nan, strategy='constant', fill_value=0)
x= data['Date'].values
x = pd.to_datetime(x, errors="coerce")
print(x)
x = x.values.astype("float64").reshape(-1,1)
y = data['TOTAL'].values
imputer = imp_mean.fit([y])
y = imputer.transform([y])
print(y)
y = y.reshape(-1,1)
reg = LinearRegression()
reg.fit(x,y)
print(f"The slope is {reg.coef_[0][0]} and the intercept is
{reg.intercept_[0]}")
predictions = reg.predict(x)
plt.scatter(x, y,c='black')
plt.plot(x, predictions, c='blue', linewidth=2)
plt.title("CO2eq emitted from diet")
plt.ylabel('CO2eq');
plt.xlabel('Date');
plt.show()
this is the graph
https://imgur.com/a/BNW2OZ3
the csv file is structured like this
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
