0

从 scikit-learn 计算 mean_squared_error 时出现错误

def __init__(self, learning_rate=0.001, n_iters=1000):
    self.lr = learning_rate
    self.n_iters = n_iters
    self.weights = None
    self.bias = None

def fit(self, X, y):
    n_samples, n_features = X.shape  # shape of X is (100,1) so there are 100 samples and only 1 feature

    self.weights = np.zeros(n_features)
    self.bias = 0

    for _ in range(self.n_iters):
        y_predicted = np.dot(X, self.weights) + self.bias

        dw = (1 / n_samples) * np.dot(X.T, (y_predicted - y))
        db = (1 / n_samples) * np.sum(y_predicted - y)
        self.weights = self.lr * dw - self.weights
        self.bias = self.lr * db - self.bias

def predict(self, X):
    return X.dot(self.weights) + self.bias

'''

 X = np.random.uniform(0, 1, 100)
 X = X.reshape(-1, 1)
 print(X.shape)

 y = [(-19 * i - 9) for i in X]
 y = np.array(y)

 # adding noise to the data
 noise = np.random.normal(1, 0)  # Adding guassian noise to y with mean 1 and std 0
 y = y + noise

X_train, X_test, y_train, y_test = train_test_split(X, y, 
test_size=0.2, random_state=1234)
print(X_test.shape)
regression = LinearRegression(learning_rate=0.01)
regression.fit(X_train, y_train)
y_pred = regression.predict(X_test)
print(y_pred.shape)
mean_squared_error(y_test,y_pred)

最后一行抛出错误:

ValueError: y_true and y_pred have different numbers of output (1!=80)

这是我的变量的形状:

(100, 1)  # X.shape
(20, 1)   # y_test.shape
(20, 80)  # y_pred.shape
4

0 回答 0