0

Experiment: effect of L1 regularization strength

Unsolved
Fundamentals

Difficulty: 5 | Problem written by peter.washington
Problem reported in interviews at

Amazon
Apple
Facebook
Google
Netflix

Return the mean and standard deviation (as a tuple) of the testing accuracy score (via the score scikit-learn method) on the handwrittetn digits dataset when training a logistic regression classifier with L1 regulariization with different regularization strengths. You will be provided a list of regularization strengths to use in the experiments as input.

Use the scikitlearn library. You can import the handwritten digits dataset and the logistic regression classifier with the following code:

from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression

X, y = load_digits(return_X_y=True)

Use the 'liblinear' solver for the Logistic Regression classifier. See LogisticRegression documentation here: https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html. Use a random state of 5.

This code may take a few seconds or minutes to run because the model will be trained across several conditions. 

Sample Input:
<class 'list'>
regularization_strengths: [0.1, 0.5, 0.9]

Expected Output:
<class 'tuple'>
(0.9894268224819144, 0.005675035630042081)

This is a premium problem, to view more details of this problem please sign up for MLPro Premium. MLPro premium offers access to actual machine learning and data science interview questions and coding challenges commonly asked at tech companies all over the world

MLPro Premium also allows you to access all our high quality MCQs which are not available on the free tier.

Not able to solve a problem? MLPro premium brings you access to solutions for all problems available on MLPro

Get access to Premium only exclusive educational content available to only Premium users.

Have an issue, the MLPro support team is available 24X7 to Premium users.

This is a premium feature.
To access this and other such features, click on upgrade below.

Log in to post a comment

Comments
Jump to comment-201
harish9 • 1 month, 1 week ago

0

I keep getting:

SyntaxError:invalid syntax

Line 2


Line 2 is the original code:

#!/usr/bin/python3


I tried deleting Line 2, left it as a blank line, etc. No matter what is in Line 2, it comes up with the same error. Any suggestions?

Jump to comment-203
antoreepjana • 1 month, 1 week ago

0

I'm able to obtain output. However, the output deviates from the expected test case outputs.


#!/usr/bin/python3

import numpy as np
import sklearn
from sklearn.datasets import load_digits
from sklearn.linear_model import LogisticRegression

# Please do not change the below function name and parameters
def regularization_strength_experiment(regularization_strengths):
    

    X, y = load_digits(return_X_y = True)
    
    scores = list()
    
    for i in regularization_strengths:
        clf = LogisticRegression(C = 1/i, penalty = 'l1', solver = 'liblinear', random_state = 5)
    
        clf.fit(X,y)
        
        scores.append(clf.score(X,y))
        
    mean = np.mean(scores)
    std = np.std(scores)
    
   return (mean, std)





Check it out and suggest what needs to be changed.

Ready.

Input Test Case

Please enter only one test case at a time
numpy has been already imported as np (import numpy as np)