y = np.matrix((allData[:, -1])).T n, d = X.shape # Add a row of ones for the bias term X = np.c_[np.ones((n, 1)), X] # initialize the model init_theta = np.matrix( np.ones((d + 1, 1)) ) * 10 # note that we really should be initializing this to be near zero, but starting it near [10,10] works better to visualize gradient descent for this particular problem n_iter = 1500 alpha = 0.01 # Instantiate objects lr_model = LinearRegression(init_theta=init_theta, alpha=alpha, n_iter=n_iter) plotData1D(X[:, 1], y) lr_model.fit(X, y) plotRegLine1D(lr_model, X, y) # Visualize the objective function convex shape theta1_vals = np.linspace(-10, 10, 100) theta2_vals = np.linspace(-10, 10, 100) visualizeObjective(lr_model, theta1_vals, theta2_vals, X, y) # Compute the closed form solution in one line of code theta_closed_form = 0 # TODO: replace "0" with closed form solution print "theta_closed_form: ", theta_closed_form
file = open(filePath,'r') allData = np.loadtxt(file, delimiter=',') X = np.matrix(allData[:,:-1]) y = np.matrix((allData[:,-1])).T n,d = X.shape # Standardize mean = X.mean(axis=0) std = X.std(axis=0) X = (X - mean) / std # Add a row of ones for the bias term X = np.c_[np.ones((n,1)), X] # initialize the model init_theta = np.matrix(np.random.randn((d+1))).T n_iter = 2000 alpha = 0.01 # Instantiate objects lr_model = LinearRegression(init_theta = init_theta, alpha = alpha, n_iter = n_iter) lr_model.fit(X,y) # Compute the closed form solution in one line of code thetaClosedForm = linalg.inv((X.T*X))*X.T*y # TODO: replace "0" with closed form solution print "thetaClosedForm: ", thetaClosedForm
for i in range(my): img[:,i] = img[:,i]/np.max(img[:,i]) xs = [] ys = [] for x in counts: for y in counts[x]: xs.append(x) ys.append(y) xs = np.array(xs) ys = np.array(ys) xs = xs.reshape((len(xs), 1)) ys = ys.reshape((len(ys), 1)) print ys.shape, xs.shape x = LinearRegression() y = x.fit(xs,ys)#,weight) fig = pplot.figure() ax = fig.add_subplot(111) ax.imshow(img, interpolation='nearest',cmap=Reds,vmin=0,vmax=1,origin='lower') ax.set_title("Distribution of degree per avalanche size") ax.set_xlabel("Avalanche size s") ax.set_ylabel("Degree of first defaulting node") pplot.show()