0
import numpy as np

    m = 100
    X = 6 * np.random.rand(m, 1) - 3
    y = 0.5 * X**2 + X + 2 + np.random.randn(m, 1)

from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split

    def plot_learning_curves(model, X, y):
    X_train, X_val, y_train, y_val = train_test_split(X, y, test_size=0.2, random_state=10)
    train_errors, val_errors = [], []
    for m in range(1, len(X_train)):
        model.fit(X_train[:m], y_train[:m])
        y_train_predict = model.predict(X_train[:m])
        y_val_predict = model.predict(X_val)
        train_errors.append(mean_squared_error(y_train[:m], y_train_predict))
        val_errors.append(mean_squared_error(y_val, y_val_predict))

    plt.plot(np.sqrt(train_errors), "r-+", linewidth=2, label="train")
    plt.plot(np.sqrt(val_errors), "b-", linewidth=3, label="val")
    plt.legend(loc="upper right", fontsize=14)   
    plt.xlabel("Training set size", fontsize=14) 
    plt.ylabel("RMSE", fontsize=14)

    lin_reg = LinearRegression()
    plot_learning_curves(lin_reg, X, y)
    plt.show() 

How to add a loop counter in here?

1
  • There's something wrong with your code, indents are incorrect. Where exactly you want to have this loop counter? What have you tried so far?
    – Tupteq
    Commented Apr 4, 2020 at 0:26

1 Answer 1

1

You can use enumerate for your loop. Example:

for idx, num in enumerate(range(5)):
  print(idx, num)

yields

0 0
1 1
2 2
3 3
4 4

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.