def predict_tweet(tweet, freqs, theta): """ :param tweet: a string :param freqs: a dictionary corresponding to the frequencies of each tuple (word, label) :param theta: (3,1) vector of weights :return y_pred: the probability fo a tweet being positive or negative """ # extract the features of the tweet and store it into x x = extract_features(tweet, freqs) # make the prediction using x and theta y_pred = sigmoid(np.dot(x, theta)) return y_pred
def memoryMolecule_gauss(vb, pos, width, p0, gammaC1_H, gammaW1_H, deltaE1_H, eta1_H, sigma1_H, gammaC2_H, gammaW2_H, deltaE2_H, eta2_H, sigma2_H, gammaL_L, gammaR_L, deltaE_L, eta_L, width_L): c = 0 vg = 0 T = 300 args_L = (gammaL_L, gammaR_L, deltaE_L, eta_L, width_L, c, vg, T) P_H = models.sigmoid(vb, pos, width) + (vb - 1) * p0 P_L = 1 - P_H I_H = models.tunnelmodel_2level(vb, c, vg, T, gammaC1_H, gammaW1_H, deltaE1_H, eta1_H, sigma1_H, gammaC2_H, gammaW2_H, deltaE2_H, eta2_H, sigma2_H) I_L = models.tunnelmodel_singleLevel(vb, *args_L) return P_H * I_H + P_L * I_L
def test_network_feed_forward_computes_hidden_units(): network = NeuralNetwork(layers=[2, 3, 2]) network.weights = [ np.array([[1, 1, 2], [1, 2, 1], [1, 1, 1]]), np.ones((2, 4)) ] a_, z_ = network.feed_forward(np.array([2, 3])) np.testing.assert_array_equal(a_[0], np.array([2, 3])) np.testing.assert_array_equal(z_[1], np.array([9, 8, 6])) np.testing.assert_array_equal( a_[1], np.array([sigmoid(9), sigmoid(8), sigmoid(6)])) value = sigmoid(9) + sigmoid(8) + sigmoid( 6) + 1 # weights of theta2 are 1's np.testing.assert_array_equal(z_[2], np.array([value, value]))
def fit_and_plot(X,Y,N,start=None,label='',C='',fig=None,ax=None): """ X: numerical days from start date Y: number of cumulative cases N: total number of days to forecast (from min(X) to max(X)+N) start: if provided, it will be considered as the starting date C: Color for the plot """ if fig==None or ax == None: fig, ax = plt.subplots() # Fit sigmoid popt, pcov = curve_fit(models.sigmoid, X,Y,maxfev = 8000) # future dates Xn = np.array(range(int(max(X)+N))) y = models.sigmoid(Xn,*popt) # Prepare plots if start != None: x = np.array([start + dt.timedelta(days=int(i)) for i in X]) x_forecast = np.array([start + dt.timedelta(days=int(i)) for i in Xn]) else: x = X x_forecast = Xn if len(label) > 0: label += f': {int(np.max(y))}' ax.plot(x, Y, f'{C}', lw=3,label=label) ax.plot(x_forecast, y, f'{C}--', alpha=0.8)
def test_sigmoid(): assert sigmoid(0) == 0.5 assert sigmoid(1000) == 1 assert sigmoid(-1000) == 0
def test_sigmoid_on_matrix(): value = np.array([[-1000, 0, 1000], [0, -2000, 0]]) np.testing.assert_array_equal(sigmoid(value), np.array([[0, 0.5, 1], [0.5, 0, 0.5]]))
def test_sigmoid_on_vector(): value = np.array([-1000, 0, 1000]) np.testing.assert_array_equal(sigmoid(value), np.array([0, 0.5, 1]))