pythonrandomsgdstochastic-gradient

Random sampling from multiple vectors in python


So I have an assignment to code Stoachastic gradient decent and basically i am finding it a bit of a problem to randomly sample from multiple vectors while keeping the order intact. My code follows:

import numpy as np 
import matplotlib.pyplot as plt
import random

x = np.array([0.,0.,0.,100.,100.,300.,300.,900.,900.,900.])
y = np.array([0.,0.,1.,0.,1.,1.,1.,0.,1.,1.])


def f(b0,b1,x,y):
    vec = [y[i]*np.log(1/(1+np.exp(-b0-b1*x[i]))) + (1-y[i])*np.log(1 - (1/(1+np.exp(-b0-b1*x[i])))) for i in range(len(y))]
    return sum(vec)

def dervf0(b0,b1,x,y):
    vec = [-y[i] + (1/(1+np.exp(-b0-b1*x[i]))) for i in range(len(y))]
    return sum(vec)
def dervf1(b0,b1,x,y):
    vec = [-x[i]*(y[i]-(1/(1+np.exp(-b0-b1*x[i])))) for i in range(len(y))]
    return sum(vec)

def SGD(v,x,y,tol,maxiter):
    x = #random selection
    y= #random selection
    for i in range(maxiter):
        theta_new = v - 0.001*np.array(
            [dervf0(v[0], v[1], x, y),
             dervf1(v[0], v[1], x, y)])
        if np.linalg.norm(theta_new - v) < tol: 
            break
        else:
            v = theta_new
            #print('i\t{}\tv\t{}\ttheta_new\t{}'.format(i, v, theta_new))
    return theta_new,i

As you can see I have 2 vectors, x and y, and they are linked for example x[0] is an experiment which gave us y[0] = 0. It makes no sense to randomly sample without structure when here in my opinion. What i am struggling to do is in the SGD function, where i want n-points of x and n-points of y but structured correctly! any help is appreciated!

Y


Solution

  • You can get a list of indices to sample using the following-

    import random
    
    x = ['This', 'is', 'a', 'random', 'sampling', 'example']
    
    n = len(x)
    k = 5
    indices_to_sample = sorted(random.sample(range(n),k)) # Chooses k out of n indices and sorts them
    for i in indices_to_sample:
        print(x[i]) # Gets x at index i
    

    Read more at random.sample docs