Exemple #1
0
    X_test = features[mid + 1:high - 1]
    y_test = result[mid + 2:high]
    dates = dates[mid + 2:high]

    reg.fit(X_train, y_train)

    # predict data #
    y_pred = reg.predict(X_test)

    print("[INFO] %s: %3.2f%%" %
          (symbol, round(reg.score(X_test, y_test) * 100, 2)),
          file=sys.stderr)

    # save last 100 days' prediction data
    sql_data = []
    for i in range(0, len(y_pred)):
        save_date = dates[i][0:4] + "-" + dates[i][4:6] + "-" + dates[i][6:8]
        sql_data.append((save_date, str(y_pred[i])))

    saveData.saveMultipleData(symbol, "SGDRegression", sql_data)

    # Predict the next day
    today = datetime.strptime(dates[len(dates) - 1], '%Y%m%d')
    next_day = today + timedelta(days=7 -
                                 today.weekday() if today.weekday() > 3 else 1)
    next_day = next_day.strftime('%Y-%m-%d')
    next_price = str(reg.predict([features[len(features) - 1]])[0])
    saveData.saveMultipleData(symbol, "SGDRegression",
                              [tuple((next_day, next_price))])
    print(next_day + ": " + next_price)
    y_train = result[low + 1:mid + 1]
    X_test = features[mid + 1:high - 1]
    y_test = result[mid + 2:high]
    dates = dates[mid + 2:high]

    reg.fit(X_train, y_train)
    # predict data #
    y_pred = reg.predict(X_test)

    # print the result
    print("[INFO] %s: %3.2f%%" %
          (symbol, round(reg.score(X_test, y_test) * 100, 2)),
          file=sys.stderr)

    # save last 100 days' prediction data
    sql_data = []
    for i in range(0, len(y_pred)):
        save_date = dates[i][0:4] + "-" + dates[i][4:6] + "-" + dates[i][6:8]
        sql_data.append((save_date, str(y_pred[i])))

    # Predict the next day
    today = datetime.strptime(dates[len(dates) - 1], '%Y%m%d')
    next_day = today + timedelta(days=7 -
                                 today.weekday() if today.weekday() > 3 else 1)
    next_day = next_day.strftime('%Y-%m-%d')
    next_price = str(reg.predict([features[len(features) - 1]])[0])
    sql_data.append((next_day, next_price))

    saveData.saveMultipleData(symbol, "LASSORegression", sql_data)
    print(next_day + ": " + next_price)
Exemple #3
0
    # create classifier
    my_classifier = SGDClassifier(loss="log", penalty="elasticnet") 

    # train the classifier
    my_classifier.fit(X_train, y_train)

    # do prediction
    predictions = my_classifier.predict(X_test)

    # print the result
    print("[INFO] %s: %3.2f%%" % (symbol, accuracy_score(y_test, predictions)*100), file=sys.stderr)
   
    # save last 100 days' prediction data
    sql_data = []
    for i in range(0, len(predictions)):
        save_date = dates[i][0:4] + "-" + dates[i][4:6] + "-" + dates[i][6:8]
        sql_data.append((save_date, str(predictions[i])))

    saveData.saveMultipleData(symbol, "SGDLinear", sql_data)

    # Predict the next day
    today = datetime.strptime(dates[len(dates)-1], '%Y%m%d')
    next_day = today + timedelta(days= 7-today.weekday() if today.weekday()>3 else 1)
    next_day = next_day.strftime('%Y-%m-%d')
    next_price = str(my_classifier.predict([features[len(features)-1]])[0])
    saveData.saveMultipleData(symbol, "SGDLinear", [tuple((next_day, next_price))])
    print(next_day + ": " + next_price)



    y_train = result[1:901]
    X_test = features[901:998]
    y_test = result[902:999]
    
    reg = SGDRegressor(max_iter=100000, loss='squared_loss', penalty='l2', shuffle=False, tol=1e-3, eta0=0.0001).fit(X_train, y_train)
    
    # predict data #
    y_pred = reg.predict(X_test)
   
    # accuracy score: returns the coefficient of determination R^2 of the prediction
    # The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse)
    accuracy[symbol].append(str(round(reg.score(X_test, y_test)*100, 2))+'%')

    # The square of the difference between the original values and the predicted values
    # It gives us the measure of how far the predictions were from the actual output
    meanSquaredError[symbol].append(str(round(mean_absolute_error(y_test, y_pred)))+'%')
   
    print("[INFO] %s: %3.2f%%" %
            (symbol, round(reg.score(X_test, y_test)*100, 2)), file=sys.stderr)  
    print(symbol + ', ' + '(score accuracy)' + ', '.join(accuracy[symbol]), file=results)
    print(symbol + ', ' + '(mean squared error)' + ', '.join(meanSquaredError[symbol]), file=results) 
    
    # save last 100 days' prediction data, date save as the date of 'yesterday'
    for i in range(1000, 900, -1):
        res = reg.predict([features[i]])
        save_date = dates[i][0:4] + "-" + dates[i][4:6] + "-" + dates[i][6:8]
        print(save_date)
        print(res[0])
   
        saveData.saveMultipleData(symbol, "SGDRegression", [tuple((save_date, str(res[0])))])
    y_test = result[mid+1:high]
    dates = dates[mid+2:high]
        
    # create classifier
    my_classifier = tree.DecisionTreeClassifier()

    # train the classifier
    my_classifier.fit(X_train, y_train)
    # do prediction
    predictions = my_classifier.predict(X_test)

    # print the result
    print("[INFO] %s: %3.2f%%" %(symbol, accuracy_score(y_test, predictions)*100), file=sys.stderr)

    # save last 100 days' prediction data
    sql_data = []
    for i in range(0, len(predictions)):
        save_date = dates[i][0:4] + "-" + dates[i][4:6] + "-" + dates[i][6:8]
        sql_data.append((save_date, str(predictions[i])))

    saveData.saveMultipleData(symbol, "DTree", sql_data)

    # Predict the next day
    today = datetime.strptime(dates[len(dates)-1], '%Y%m%d')
    next_day = today + timedelta(days= 7-today.weekday() if today.weekday()>3 else 1)
    next_day = next_day.strftime('%Y-%m-%d')
    next_price = str(my_classifier.predict([features[len(features)-1]])[0])
    saveData.saveMultipleData(symbol, "DTree", [tuple((next_day, next_price))])
    print(next_day + ": " + next_price)

Exemple #6
0
    # create train and test data set #
    X_train = features[0:900]
    y_train = result[1:901]
    X_test = features[901:998]
    y_test = result[902:999]
        
    # create classifier
    my_classifier = SGDClassifier(loss="log", penalty="elasticnet") 

    # train the classifier
    my_classifier.fit(X_train, y_train)

    # do prediction
    predictions = my_classifier.predict(X_test)

    accuracy[symbol].append(str(round(accuracy_score(y_test, predictions)*100, 2))+'%')

    # print the result
    print("[INFO] %s: %3.2f%%" %
        (symbol, accuracy_score(y_test, predictions)*100), file=sys.stderr)
   
    # save the results to db
    for i in range(1000, 900, -1):
        res = my_classifier.predict([features[i]])
        save_date = dates[i][0:4] + "-" + dates[i][4:6] + "-" + dates[i][6:8]
        saveData.saveMultipleData(symbol, "SGDLinear", [tuple((save_date, str(res[0])))])