def logistic_L0_gradient(y, X, w, lamb=0): """ Computes the gradient for regularized logistic regression subject to an L0 regularizer. Parameters ========== y: np.ndarray N-vector describing prediction. X: np.ndarray N x D matrix describing features. w: np.ndarray D-vector describing model parameters. lamb: np.float64 Regularization factor. Returns ======= res: np.ndarray D-vector of gradient (1 dimension for each model parameter) """ N, D = X.shape grad = (1 / N) * X.T.dot(logistic.sigmoid(X, w) - y) return grad
def classify_vector(inx, weights): result = logistic.sigmoid(sum(inx * weights)) assert isinstance(result, float) if result >= 0.5: # print "The classes label is 1" return 1.0 else: # print "The classes label is 0" return 0.0
def classifyVector(inX, weights): """ 分类 """ prob = logistic.sigmoid(sum(inX * weights)) if prob > 0.5: return 1.0 else: return 0.0
def stocGradAscent0(dataMatrix, classLabels): m, n = shape(dataMatrix) alpha = 0.5 weights = ones(n) # initialize to all ones weightsHistory = zeros((500 * m, n)) for j in range(500): for i in range(m): h = logistic.sigmoid(sum(dataMatrix[i] * weights)) error = classLabels[i] - h weights = weights + alpha * error * dataMatrix[i] weightsHistory[j * m + i, :] = weights return weightsHistory
def predict(X_test, m1, m0, sigma, n1, n0): print "Predicting..." sig_inv = np.linalg.inv(sigma) x = X_test.T w = np.dot((m1 - m0), sig_inv) b = (-0.5) * np.dot(np.dot([m1], sig_inv), m1) + (0.5) * np.dot( np.dot([m0], sig_inv), m0) + np.log(float(n1) / n0) a = np.dot(w, x) + b y = sigmoid(a) out = [] for i in range(len(y)): if y[i] >= 0.5: out.append(1) else: out.append(0) return out
def stocGradAscent1(dataMatrix, classLabels): m, n = shape(dataMatrix) weights = ones(n) # initialize to all ones weightsHistory = zeros((40 * m, n)) for j in range(40): dataIndex = list(range(m)) for i in range(m): alpha = 4 / (1.0 + j + i) + 0.01 randIndex = int(random.uniform(0, len(dataIndex))) h = logistic.sigmoid(sum(dataMatrix[randIndex] * weights)) error = classLabels[randIndex] - h # print error weights = weights + alpha * error * dataMatrix[randIndex] weightsHistory[j * m + i, :] = weights del (dataIndex[randIndex]) print(weights) return weightsHistory
def prediction_accuracy(y, X, w): """ Returns a K-fold cross validation generator. Each item obtained from the generator consists of (train_y, train_X, test_y, test_X) tuples. The proportion of test samples is N // fold_count. The proportion of training samples is N - (N // fold_count). Parameters ========== y: np.ndarray N-vector describing prediction. X: np.ndarray N x D matrix describing features. w: np.ndarray D-vector describing model parameters. Returns ======= correct_count: int Number of correct predictions. total_count: int Total number of samples. """ N, D = X.shape y_probability = logistic.sigmoid(X, w) category_0_condition = y_probability < 0.5 category_1_condition = np.logical_not(category_0_condition) y_predict = np.zeros(N) y_predict[category_0_condition] = 0 y_predict[category_1_condition] = 1 correct_count = np.sum(y_predict == y) total_count = y.shape[0] return (correct_count, total_count)
import numpy as np import logistic as log feature_num = 34 rows = 351 dataset = np.genfromtxt('ionosphere2.csv', delimiter=',') np.random.shuffle(dataset) features = np.append(np.ones((rows, 1)), dataset, axis=1) features = dataset[:, 0:feature_num+1] target = dataset[:, feature_num].reshape((rows, 1)) train_feature, train_target, test_feature, test_target = log.get_data(features, target, 150, rows) b = np.ones((feature_num+1, 1)) b = log.descent(train_feature, train_target, b) test = log.sigmoid(test_feature, b) output = np.append(test, test_target, axis=1) count = 0.0 correct = 0.0 for i in output: if i[0] > 0.5 and i[1] == 1: correct += 1 if i[0] <= 0.5 and i[1] == 0: correct += 1 count += 1 print "{}%".format((correct/count)*100)
print("Training set accuracy:") print(logistic.compute_accuracy(solution.x, train_x, train_y)) print(logistic.compute_logistic_loss(solution.x, train_x, train_y)) print() print("Validation set accuracy:") print(logistic.compute_accuracy(solution.x, test_x, test_y)) print(logistic.compute_logistic_loss(solution.x, test_x, test_y)) print() rec = Recording("data/ur10e_guiding/%s.csv" % NAMES[0]) freqs = compute_windowed_fourier(rec.force_xyz, width=width) data = preprocess(freqs[:, :, :snipoff]) # data = preprocess(rec.force_xyz) logodds = logistic.biasdot(solution.x, data) probs = logistic.sigmoid(logodds) # figure, (top, bot) = plt.subplots(figsize=(12, 8), nrows=2, sharex=True) # top.plot(rec.force_xyz, ".-", alpha=0.5) # bot.plot(probs, ".-", lw=3) # plt.tight_layout() # plt.show() figure, axlist = plt.subplots(figsize=(12, 8), nrows=4, sharex=True) for i, axes in enumerate(axlist[:3]): powers = freqs[:, :, 0].T**2 powers /= powers.max(axis=0) axes.imshow(powers, aspect="auto") axlist[-1].plot(probs, ".-") plt.tight_layout() plt.show()
for index, time in enumerate(times): if time > 21*24: indices.append(index) elif lof(index) > 100: indices.append(index) print('Removing {} outliers'.format(len(indices))) times = [time for index, time in enumerate(times) if index not in indices] survivals = [survival for index, survival in enumerate(survivals) if index not in indices] alpha = np.log(1/0.99 - 1) beta = pm.Beta('beta', 1, 2, 1e-3) prob = pm.Lambda('prob', lambda t=times, a=alpha, b=beta: sigmoid(t, a, b)) survival = pm.Bernoulli('survival', prob, value=survivals, observed=True) model = pm.Model([survival, beta]) mcmc = pm.MCMC(model) mcmc.sample(12000, 10000, 2) beta_samples = mcmc.trace('beta')[:, None] label = 'N = {}'.format(subquery.count()) y = sigmoid(time_ticks.T, alpha, beta_samples) line, *_ = ax.plot(time_ticks, y.mean(axis=0), label=label) quantiles = mquantiles(y, [0.025, 0.975], axis=0) ax.fill_between(time_ticks[:, 0], *quantiles, alpha=0.25,
def classifyVector(inX, weights): prob = lg.sigmoid(sum(inX * weights)) #prob = 0 if prob > 0.5: return 1.0 else: return 0.0