#73 Set Matrix Zeroes

Description

Given an m x n matrix. If an element is 0, set its entire row and column to 0. Do it in-place.

Follow up:

A straight forward solution using O(mn) space is probably a bad idea.
A simple improvement uses O(m + n) space, but still not the best solution.
Could you devise a constant space solution?

Examples

Example 1:

Input: matrix = [[1,1,1],[1,0,1],[1,1,1]]
Output: [[1,0,1],[0,0,0],[1,0,1]]

Example 2:

Input: matrix = [[0,1,2,0],[3,4,5,2],[1,3,1,5]]
Output: [[0,0,0,0],[0,4,5,0],[0,3,1,0]]

解题思路

那他要in-place的修改,又要求尽量少的额外数据存储空间,(我能想到的)就是把“0”的行列号存下来,然后统一处理。

代码

可以优化的地方就是需要改成“0”的行列序号的存储

class Solution {
    public void setZeroes(int[][] matrix) {
        List<Integer> column = new ArrayList<>();
        List<Integer> row = new ArrayList<>();
        
        int i, j;
        for(i = 0; i < matrix.length; i++)
            for(j = 0; j < matrix[0].length; j++){
                if(matrix[i][j] == 0){
                    column.add(j);
                    row.add(i);
                }
            }
        
        for(Integer c: column){
            for(i = 0; i < matrix.length; i++)
                matrix[i][c] = 0;
        }
        
        for(Integer r: row){
            for(j = 0; j < matrix[0].length; j++)
                matrix[r][j] = 0;
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
import numpy as np def sigmoid(x): # the sigmoid function return 1/(1+np.exp(-x)) class LogisticReg(object): def __init__(self, indim=1): # initialize the parameters with all zeros # w: shape of [d+1, 1] self.w = np.zeros((indim + 1, 1)) def set_param(self, weights, bias): # helper function to set the parameters # NOTE: you need to implement this to pass the autograde. # weights: vector of shape [d, ] # bias: scaler def get_param(self): # helper function to return the parameters # NOTE: you need to implement this to pass the autograde. # returns: # weights: vector of shape [d, ] # bias: scaler def compute_loss(self, X, t): # compute the loss # X: feature matrix of shape [N, d] # t: input label of shape [N, ] # NOTE: return the average of the log-likelihood, NOT the sum. # extend the input matrix # compute the loss and return the loss X_ext = np.concatenate((X, np.ones((X.shape[0], 1))), axis=1) # compute the log-likelihood def compute_grad(self, X, t): # X: feature matrix of shape [N, d] # grad: shape of [d, 1] # NOTE: return the average gradient, NOT the sum. def update(self, grad, lr=0.001): # update the weights # by the gradient descent rule def fit(self, X, t, lr=0.001, max_iters=1000, eps=1e-7): # implement the .fit() using the gradient descent method. # args: # X: input feature matrix of shape [N, d] # t: input label of shape [N, ] # lr: learning rate # max_iters: maximum number of iterations # eps: tolerance of the loss difference # TO NOTE: # extend the input features before fitting to it. # return the weight matrix of shape [indim+1, 1] def predict_prob(self, X): # implement the .predict_prob() using the parameters learned by .fit() # X: input feature matrix of shape [N, d] # NOTE: make sure you extend the feature matrix first, # the same way as what you did in .fit() method. # returns the prediction (likelihood) of shape [N, ] def predict(self, X, threshold=0.5): # implement the .predict() using the .predict_prob() method # X: input feature matrix of shape [N, d] # returns the prediction of shape [N, ], where each element is -1 or 1. # if the probability p>threshold, we determine t=1, otherwise t=-1
最新发布
07-22
以下是您提供的代码的完整实现,包括_param()和get_param()方法的实现: ```python import numpy as np def sigmoid(x): # the sigmoid function return 1 / (1 + np.exp(-x)) class LogisticReg(object): def __init__(self, indim=1): # initialize the parameters with all zeros # w: shape of [d+1, 1] self.w = np.zeros((indim + 1, 1)) def set_param(self, weights, bias): # helper function to set the parameters # weights: vector of shape [d, ] # bias: scalar self.w[:-1] = weights.reshape(-1, 1) self.w[-1] = bias def get_param(self): # helper function to return the parameters # returns: # weights: vector of shape [d, ] # bias: scalar weights = self.w[:-1].reshape(-1) bias = self.w[-1] return weights, bias def compute_loss(self, X, t): # compute the loss # X: feature matrix of shape [N, d] # t: input label of shape [N, ] # NOTE: return the average of the log-likelihood, NOT the sum. # extend the input matrix X_ext = np.concatenate((X, np.ones((X.shape[0], 1))), axis=1) # compute the log-likelihood z = X_ext @ self.w y = sigmoid(z) loss = -np.mean(t * np.log(y) + (1 - t) * np.log(1 - y)) return loss def compute_grad(self, X, t): # X: feature matrix of shape [N, d] # grad: shape of [d, 1] # NOTE: return the average gradient, NOT the sum. X_ext = np.concatenate((X, np.ones((X.shape[0], 1))), axis=1) z = X_ext @ self.w y = sigmoid(z) grad = np.mean(X_ext.T @ (y - t), axis=1) return grad def update(self, grad, lr=0.001): # update the weights # by the gradient descent rule self.w -= lr * grad.reshape(-1, 1) def fit(self, X, t, lr=0.001, max_iters=1000, eps=1e-7): # implement the .fit() using the gradient descent method. # args: # X: input feature matrix of shape [N, d] # t: input label of shape [N, ] # lr: learning rate # max_iters: maximum number of iterations # eps: tolerance of the loss difference # TO NOTE: # extend the input features before fitting to it. # return the weight matrix of shape [indim+1, 1] X_ext = np.concatenate((X, np.ones((X.shape[0], 1))), axis=1) prev_loss = np.inf for i in range(max_iters): loss = self.compute_loss(X, t) if np.abs(loss - prev_loss) < eps: break grad = self.compute_grad(X, t) self.update(grad, lr) prev_loss = loss return self.w def predict_prob(self, X): # implement the .predict_prob() using the parameters learned by .fit() # X: input feature matrix of shape [N, d] # NOTE: make sure you extend the feature matrix first, # the same way as what you did in .fit() method. # returns the prediction (likelihood) of shape [N, ] X_ext = np.concatenate((X, np.ones((X.shape[0], 1))), axis=1) z = X_ext @ self.w y = sigmoid(z) return y def predict(self, X, threshold=0.5): # implement the .predict() using the .predict_prob() method # X: input feature matrix of shape [N, d] # returns the prediction of shape [N, ], where each element is -1 or 1. # if the probability p > threshold, we determine t = 1, otherwise t = -1 prob = self.predict_prob(X) predictions = np.where(prob > threshold, 1, -1) return predictions ``` 这个类实现了逻辑回归的基本功能,包括参数设置、计算损失、计算梯度、更新权重、拟合数据、预测概率和预测类别等方法。您可以使用这个类来拟合二分类问题的数据,并进行预测。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值