'x and y must have same first dimension numpy
Im trying to plot a graph which converts dates into floats to be used in linear regression algorithm and then uses the original dates as strings for the x axis labels. When I plot the actual values from the csv file the program runs ok however when I plot the regression values i get the error raise ValueError(f"x and y must have same first dimension, but " ValueError: x and y must have same first dimension, but have shapes (60,) and (1,)
here is my code:
scaler = StandardScaler()
data = pd.read_csv('food.csv')
X = data['Date'].values
X = pd.to_datetime(X, errors="coerce")
X = X.values.astype("float64").reshape(-1,1)
Y = data['TOTAL'].values.reshape(-1,1)
mean_x = np.mean(X)
mean_y = np.mean(Y)
m = len(X)
numer = 0
denom = 0
for i in range(m):
numer += (X[i] - mean_x) * (Y[i] - mean_y)
denom += (X[i] - mean_x) ** 2
m = numer / denom
c = mean_y - (m * mean_x)
print (f'm = {m} \nc = {c}')
max_x = np.max(X) + 100
min_x = np.min(X) - 100
x = np.linspace (min_x, max_x, 100)
y = c + m * x
X= data['Date'].astype('str')
x= data['Date'].astype('str')
print(X.shape)
print(y.shape)
newY = Y.transpose()[0]
newy = y.transpose()[0]
plt.scatter(X, newY, c='#ef5423', label='data points')
plt.plot(x, newy, color='#58b970', label='Regression Line')
plt.show()
Solution 1:[1]
It's difficult to answer without data, but by the looks of your code x, and therefore y have shape (100,) (from the x = np.linspace(...) command).
So you probably don't want to pick just the 0th element in the line
newy = y.transpose()[0]
Because then newy is only a scalar value. What happens if you omit [0] and just do this?
newy = y.transpose()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jan Bijster |
