Skip to content
Supervised Learning with scikit-learn
  • AI Chat
  • Code
  • Report
  • Spinner

    Supervised Learning with scikit-learn

    Run the hidden code cell below to import the data used in this course.

    Take Notes

    Add notes about the concepts you've learned and code cells with code you want to keep.

    Create a cross validation

    # Add your # Import the necessary modules
    from sklearn.model_selection import KFold, cross_val_score
    
    # Create a KFold object
    kf = KFold(n_splits=6, shuffle=True, random_state=5)
    
    reg = LinearRegression()
    
    # Compute 6-fold cross-validation scores
    cv_scores = cross_val_score(reg, X, y,cv=kf)
    
    # Print scores
    print(cv_scores)code snippets here

    Generating metrics

    # Print the mean
    print(np.mean(cv_results))
    
    # Print the standard deviation
    print(np.std(cv_results))
    
    # Print the 95% confidence interval
    print(np.quantile(cv_results, [0.025, 0.975]))

    Regularization with Ridge

    # Import Ridge
    from sklearn.linear_model import Ridge
    alphas = [0.1, 1.0, 10.0, 100.0, 1000.0, 10000.0]
    ridge_scores = []
    for alpha in alphas:
      
      # Create a Ridge regression model
      ridge = Ridge(alpha=alpha)
      
      # Fit the data
      ridge.fit(X_train,y_train)
      
      # Obtain R-squared
      score = ridge.score(X_test, y_test)
      ridge_scores.append(score)
    print(ridge_scores)

    Regularization with Lasso

    # Import Lasso
    from sklearn.linear_model import Lasso
    
    # Instantiate a lasso regression model
    lasso = Lasso(alpha=0.3)
    
    # Fit the model to the data
    lasso.fit(X,y)
    
    # Compute and print the coefficients
    lasso_coef = lasso.coef_
    print(lasso_coef)
    plt.bar(sales_columns, lasso_coef)
    plt.xticks(rotation=45)
    plt.show()