コード例 #1
0
ファイル: app.py プロジェクト: smolgeat/Summer-2020-MWML
def home():
    start = time.time()
    ratings = {}
    movieId = m.title_to_id([
        'Toy Story', 'Toy Story 2', "Toy Story 3", "The Lion King",
        "A Bug's Life", "The Hunchback of Notre Dame"
    ], links)
    for movie in movieId:
        ratings[movie] = 5
    data = m.add_user(ratings, df)
    model = m.train_model(data)
    #get n movie suggestions
    user = df['userId'].max()
    n_movies = 5
    suggestions = np.array(m.make_predictions(user, df, model, n_movies))
    #select the movie id
    suggested_movie_id = suggestions[:, 0]
    #return titles to user
    titles = m.id_to_title(suggested_movie_id, links)
    suggested_titles = {}
    suggested_titles['Suggested Titles'] = titles
    suggested_titles['Watched Titles'] = [
        'Toy Story', 'Toy Story 2', "Toy Story 3", "The Lion King",
        "A Bug's Life", "The Hunchback of Notre Dame"
    ]
    end = time.time()
    minutes = round((end - start) / 60)
    seconds = get_seconds(start, end)
    suggested_titles['minutes'] = minutes
    suggested_titles['seconds'] = seconds
    return suggested_titles
コード例 #2
0
ファイル: app.py プロジェクト: KishanGM/Heroku-Demo
def predict():
    '''
    For rendering results on HTML GUI
    '''
    text = request.form['question']
    #_logger.info(f'Inputs: {text}')
    #final_features = [np.array(int_features)]
    prediction = make_predictions(text)

    output = prediction

    return render_template('index.html',
                           prediction_text='Sentiment is {}'.format(output))
コード例 #3
0
def submit(test_df, model):
    test_df = clean_data(test_df)
    X_test = test_df.iloc[:, 1:]
    test_ids = test_df.iloc[:, 0]
    X_test = np.array(X_test)
    X_test, test_ids = X_test.astype(str), test_ids.astype(int)
    h1n1_preds, seasonal_preds = make_predictions(model, X_test)

    result_df = concat([test_ids,
                        DataFrame(h1n1_preds, columns=['h1n1_vaccine']),
                        DataFrame(seasonal_preds, columns=['seasonal_vaccine'])],
                       axis=1)
    print(f'Exporting as pickle...')
    dump(model, open("classifier.pkl", "wb"))
    result_df.to_csv('Submissions/submission.csv', index=False)
    print('done')
def predict():
    model = load_model()
    files = request.files['images']
    image = read_image(files)
    image = preprocess(image)

    # response = np.array_str(np.argmax(out,axis=1))
    predicted = model(image)

    value = make_predictions(predicted)

    maxArg = tf.argmax(value, axis=1)
    print(maxArg)
    maxarg = maxArg.numpy()

    print("------------------")
    print(value)
    print("------------------")
    response = {
        'target': np.array_str(value.numpy()),
        'Result': f"The dress is {class_names[maxarg[0]]}."
    }

    return response
コード例 #5
0
def get_data(selected_dropdown_value):

    date = datetime.today()
    ts = datetime.timestamp(date)
    start = int(ts)

    tss = datetime.today() - timedelta(days=3650)
    tss = datetime.timestamp(tss)
    end = int(tss)
    end

    url = 'https://query1.finance.yahoo.com/v7/finance/download/{}?period1={}&period2={}&interval=1d&events=history'.format(
        selected_dropdown_value, end, start)
    c = urllib3.PoolManager()
    filename = "../dash/data/{}.csv".format(selected_dropdown_value)

    with c.request('GET', url,
                   preload_content=False) as res, open(filename,
                                                       'wb') as out_file:
        shutil.copyfileobj(res, out_file)

    data = pd.read_csv("../dash/data/{}.csv".format(selected_dropdown_value))

    #data = yf.download(selected_dropdown_value, start=datetime(2008, 5, 5), end=datetime.now())

    dff = pd.DataFrame(data)
    dfff = dff.set_index('Date')

    df = model.moving_avg(dfff)

    dff = model.make_predictions(dfff)

    return {
        'data': [{
            'x': df.index,
            'y': df['Close'],
            'name': 'Close'
        }, {
            'x': df.index,
            'y': df['MA10'],
            'name': 'MA10'
        }, {
            'x': df.index,
            'y': df['MA30'],
            'name': 'MA30'
        }, {
            'x': df.index,
            'y': df['MA50'],
            'name': 'MA50'
        }, {
            'x': df.index,
            'y': df['rets'],
            'name': 'Returns'
        }, {
            'x': dff.index,
            'y': dff['Forecast_reg'],
            'name': 'Regression',
        }, {
            'x': dff.index,
            'y': dff['Forecast_knn'],
            'name': 'KNN',
        }, {
            'x': dff.index,
            'y': dff['forecast_by'],
            'name': 'Bayesian',
        }],
        'layout': {
            'margin': {
                'l': 60,
                'r': 60,
                't': 30,
                'b': 30
            },
            'title': 'Stock Data Visualization',
            'align': 'center'
        }
    }
コード例 #6
0
                                              batch_size=256,
                                              shuffle=False,
                                              drop_last=False,
                                              pin_memory=True)
    train_pred_loader = torch.utils.data.DataLoader(train_dataset,
                                                    batch_size=256,
                                                    shuffle=False,
                                                    drop_last=False,
                                                    pin_memory=True)
    valid_pred_loader = torch.utils.data.DataLoader(valid_dataset,
                                                    batch_size=256,
                                                    shuffle=False,
                                                    drop_last=False,
                                                    pin_memory=True)

    y_test_pred, log_preds_test = model.make_predictions(test_loader, nn_model)
    y_train_pred, log_preds_train = model.make_predictions(
        train_pred_loader, nn_model)
    y_valid_pred, log_preds_valid = model.make_predictions(
        valid_pred_loader, nn_model)

    y_pred_binary = (y_test_pred > 0.5)
    y_pred_train_binary = (y_train_pred > 0.5)
    y_pred_valid_binary = (y_valid_pred > 0.5)

    X_train_df = pd.read_feather('../output/nn_predictions/train_df.feather')
    X_valid_df = pd.read_feather('../output/nn_predictions/valid_df.feather')
    X_test_df = pd.read_feather('../output/nn_predictions/test_df.feather')

    y_pred_binary = X_test_df['nn_predictions_binary'].values
    y_test_pred = X_test_df['nn_predictions_prob'].values
コード例 #7
0
    return ovr


if __name__ == '__main__':
    df = import_data(train=True)
    test_df = import_data(features='Datasets/test_set_features.csv', train=False)
    cols = list(df.columns)
    set_df_values(df)
    df = clean_data(df)
    x_train, x_val, y_train, y_val, train_ids, val_ids = split_dataset(df, test_size=0.1, seed=42)
    x_train, y_train = x_train.astype(str), y_train.astype(int)
    x_val, y_val = x_val.astype(str), y_val.astype(int)

    # model = fit_random_search_model(x_train, y_train)
    model = fit_model(x_train, y_train)
    h1n1_preds, seasonal_preds = make_predictions(model, x_train)
    h1n1_true, seasonal_true = y_train['h1n1_vaccine'].values.tolist(), y_train['seasonal_vaccine'].values.tolist()
    train_score = get_scores(h1n1_true, h1n1_preds, seasonal_true, seasonal_preds)
    print(f'Training Accuracy: {train_score}')

    h1n1_preds, seasonal_preds = make_predictions(model, x_val)
    h1n1_true, seasonal_true = y_val['h1n1_vaccine'].values.tolist(), y_val['seasonal_vaccine'].values.tolist()
    validation_score = get_scores(h1n1_true, h1n1_preds, seasonal_true, seasonal_preds)
    print(f'Validation Accuracy: {validation_score}')

    """
    0.8515016459915379 - epochs = 230, learning_rate=0.1
    0.8665831809413866 - epochs = 230, learning_rate=0.15, test_size = 0.05
    0.8679880585565651 - epochs = 230, learning_rate=0.05, test_size = 0.01
    """
    submit(test_df, model)
コード例 #8
0
def get_data(selected_dropdown_value):


    date = datetime.today()
    ts = datetime.timestamp(date)
    start = int(ts)

    tss = datetime.today() - timedelta(days=3650)
    tss = datetime.timestamp(tss)
    end = int(tss)
    end

    # # url = 'https://query1.finance.yahoo.com/v7/finance/download/{}?period1={}&period2={}&interval=1d&events=history'.format(selected_dropdown_value,end,start)
    # url = 'https://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=BSE:{}&apikey=DFS&outputsize=full&datatype=csv'.format(selected_dropdown_value)
    # c = urllib3.PoolManager()
    # filename = "../datasets/data/{}.csv".format(selected_dropdown_value)


    # with c.request('GET', url, preload_content=False) as res, open(filename, 'wb') as out_file:
    #     shutil.copyfileobj(res, out_file)

    data = pd.read_csv("../datasets/data/{}.csv".format(selected_dropdown_value), index_col = "timestamp")
    data.sort_values(["timestamp"], ascending = True, inplace=True)
    data.index = pd.to_datetime(data.index)



    #data = yf.download(selected_dropdown_value, start=datetime(2008, 5, 5), end=datetime.now())

    # dff = pd.DataFrame(data)
    # dfff = dff.set_index('timestamp')
    
    df = model.moving_avg(data)
    
    dff = model.make_predictions(data)
    

    return {
        'data': [{
            'x': df.index,
            'y': df['close'],
            'name': 'Close'
        },
        {
            'x': df.index,
            'y': df['MA10'],
            'name': 'MA10'

        },
        {
            'x': df.index,
            'y': df['MA30'],
            'name': 'MA30'

        },
        {
            'x': df.index,
            'y': df['MA44'],
            'name': 'MA44'

        },
        {
            'x': df.index,
            'y': df['MA50'],
            'name': 'MA50'

        },
        {
            'x': df.index,
            'y': df['rets'],
            'name': 'Returns'

        },
        {
            'x': dff.index,
            'y': dff['Forecast_reg'],
            'name': 'Regression',
            
        },
        {
            'x': dff.index,
            'y': dff['Forecast_knn'],
            'name': 'KNN',
            

        },
        {
            'x': dff.index,
            'y': dff['forecast_by'],
            'name': 'Bayesian',
            

        }
        ],
        'layout': {'margin': {'l': 60, 'r': 60, 't': 30, 'b': 30},'title': 'Stock Data Visualization', 'align':'center'}
    }
コード例 #9
0
    # print(ohe_cols)
    df = one_hot_encode(df, colnames=ohe_cols)

    x_train, x_val, y_train, y_val, train_ids, val_ids = split_dataset(df, test_size=0.3, seed=42)
    X_train, Y_train = np.array(x_train), np.array(y_train)
    X_val, Y_val = np.array(x_val), np.array(y_val)

    param_grid = {'C': [0.1]}
    model_to_set = OneVsRestClassifier(LogisticRegression(penalty='l2'))
    parameters = {
        "estimator__C": [0.1, 1],
        "estimator__max_iter": [1000, 5000, 10000, 2500, 4000],
        "estimator__solver": ['saga']
    }

    model_tunning = GridSearchCV(model_to_set, cv=5, param_grid=parameters, verbose=3)
    y_train = y_train.astype('int')
    model_tunning.fit(x_train, y_train)

    print(model_tunning.best_score_)
    print(model_tunning.best_params_)

    y_train['h1n1_vaccine'] = to_numeric(y_train['h1n1_vaccine'])
    y_train['seasonal_vaccine'] = to_numeric(y_train['seasonal_vaccine'])

    h1n1_preds, seasonal_preds = make_predictions(model_tunning, X_val)
    h1n1_true, seasonal_true = (Y_val[:, 0]).tolist(), Y_val[:, 1].tolist()

    score = get_scores(h1n1_true, h1n1_preds, seasonal_true, seasonal_preds)
    print('Validation Accuracy = ', score)