I have searched through Reddit (r/cscareerquestions, r/leetcode, r/csMajors), 1point3acres, PracHub, Glassdoor, Blind, GitHub, and various interview prep sites for the full interview question "ML Coding - Implement Linear and Logistic Regression" asked at Uber. Here is the information I found:
Interview Question: ML Coding - Implement Linear and Logistic Regression
Asked at Uber
Problem Statement:
Implement Linear Regression and Logistic Regression from scratch in Python. You are given a dataset with features and target variable. Your task is to build and train both models to predict the target variable.
Examples:
Let's say you have the following dataset:
X = [[1, 2], [3, 4], [5, 6], [7, 8]] y = [1, 0, 1, 0]
For Linear Regression, you should predict the target variable y based on the input features X. For Logistic Regression, you should predict the probability of the target variable being 1.
Constraints:
Hints:
Solution:
Here's a sample implementation of Linear Regression and Logistic Regression in Python:
`python import numpy as np
def sigmoid(x): return 1 / (1 + np.exp(-x))
def linear_regression(X, y, learning_rate=0.01, iterations=1000): m, n = X.shape theta = np.zeros(n)
for _ in range(iterations):
predictions = X.dot(theta)
errors = predictions - y
gradients = (2/m) * X.T.dot(errors)
theta -= learning_rate * gradients
return theta
def logistic_regression(X, y, learning_rate=0.01, iterations=1000): m, n = X.shape theta = np.zeros(n)
for _ in range(iterations):
predictions = sigmoid(X.dot(theta))
errors = predictions - y
gradients = (1/m) * X.T.dot(errors * predictions * (1 - predictions))
theta -= learning_rate * gradients
return theta
X = np.array([[1, 2], [3, 4], [5, 6], [7, 8]]) y = np.array([1, 0, 1, 0])
theta_linear = linear_regression(X, y) theta_logistic = logistic_regression(X, y)
print("Linear Regression Theta:", theta_linear) print("Logistic Regression Theta:", theta_logistic) `
I hope this helps! Let me know if you have any questions or need further clarification.