# -*- coding: utf-8 -*-
"""
Created on Fri Jun 17 17:32:45 2016

@author: atabak
"""

import tensorflow as tf
from LinearRegression import linearRegression
import numpy as np
import os

trX = np.linspace(-1, 1, 101)
trY = 2 * trX + np.random.randn(*trX.shape) * 0.33

with tf.Session() as sess:
    LR = linearRegression(sess)
    for i in range(100):
        for (x, y) in zip(trX, trY):
            LR.fit(x, y)
    print(sess.run(LR.w))
    path = LR.saver.save(
        LR.sess, os.path.join(os.path.dirname(__file__), "simple.ckpt"))
    print("Saved:", path)
Ejemplo n.º 2
0
# -*- coding: utf-8 -*-
"""
Created on Fri Jun 17 17:32:45 2016

@author: atabak
"""

import tensorflow as tf
from LinearRegression import linearRegression
import numpy as np
import os

trX = np.linspace(-1, 1, 101)
trY = 2 * trX + np.random.randn(*trX.shape) * 0.33

with tf.Session() as sess:
    LR = linearRegression(sess)
    for i in range(100):
        for (x, y) in zip(trX, trY):
            LR.fit(x,y)
    print(sess.run(LR.w))
    path = LR.saver.save(LR.sess, os.path.join(os.path.dirname(__file__), "simple.ckpt"))
    print("Saved:", path)      

Ejemplo n.º 3
0
for i in range(test_data.index.size - 9):
    tmp_list = test_data[i:i + 9].values.reshape(1, test_data.columns.size *
                                                 9)[0].tolist()
    tmp_list.append(1)
    test_data_X.append(tmp_list)
print("---------------\n")
test_data_X = np.array(test_data_X)
test_label_y = np.array(test_label_y)

# scale the train data
mm = preprocessing.MinMaxScaler()
train_data_X = mm.fit_transform(train_data_X)
test_data_X = mm.fit_transform(test_data_X)

# train the model
linear_classifer = linearRegression(train_data.columns.size * 9, 1000, 1)
#print(train_data_X)
#print(train_label_y)

x_train, x_test, y_train, y_test = train_test_split(train_data_X,
                                                    train_label_y,
                                                    test_size=0.3)

# use the sklearn's linear regression
classifier = LinearRegression()
classifier.fit(x_train, y_train)
predict_y = classifier.predict(x_test)

# predict the test data and print out score
result_file = open("result.csv", 'w')
result_file.write("id,PM2.5,\n")
def predictFromTimeSeries(attributes, target, testAttributes, testTarget,
                          nCpuCores):
    '''
        Predict stock close prices using the past data of stock after doing feature extraction
    :param attributes: the data frame of attributes(candidates for features) for training
    :param target: the data frame of target variable(close price) for training
    :param testAttributes: the data frame of attributes for testing
    :param testTarget: the data frame of target variable for testing
    :param nCpuCores: no of cores to be used (refer to ParseArgs() in main.py)
    :return: dictionaries of errors and predicted values of stock closing price as predicted by all models
    '''

    #dictionary have error values of all the models
    scores = {}
    predictions = {}
    pValues = {}
    Identifier = "Time Series"

    #Feature Selection
    featureSelector = FeatureSelector()

    #Feature Selection for Bayesian Ridge
    attributesBR, targetBR = featureSelector.featureSelection(
        attributes, target, 6, modelName=AllowedModels.BAYESIAN_RIDGE)
    testAttributesBR = testAttributes[attributesBR.columns]

    #Training and testing Bayesian Ridge model
    error, predictedValues, pvalue = bayesianRidge(attributesBR, targetBR,
                                                   testAttributesBR,
                                                   testTarget, Identifier)
    scores['Bayesian Ridge'] = error
    predictions['Bayesian Ridge'] = predictedValues
    pValues['Bayesian Ridge'] = pvalue

    #Feature Selection for Decision Trees Regression
    attributesDT, targetDT = featureSelector.featureSelection(
        attributes, target, 6, modelName=AllowedModels.DECISION_TREE)
    testAttributesDT = testAttributes[attributesDT.columns]

    #Training and testing Decision Tree Regression model
    error, predictedValues, pvalue = decisionTree(attributesDT, targetDT,
                                                  testAttributesDT, testTarget,
                                                  nCpuCores, Identifier)
    scores['Decision Tree'] = error
    predictions['Decision Tree'] = predictedValues
    pValues['Decision Tree'] = pvalue

    #Training and testing Gaussian Process model (Feature selection not possible for Gaussian Process using RFE)
    error, predictedValues, pvalue = gaussianProcess(attributes, target,
                                                     testAttributes,
                                                     testTarget, Identifier)
    scores['Gaussian Process'] = error
    predictions['Gaussian Process'] = predictedValues
    pValues['Gaussian Process'] = pvalue

    #Feature Selection for Gradient Boosting Regression
    attributesGB, targetGB = featureSelector.featureSelection(
        attributes, target, 6, modelName=AllowedModels.GRADIENT_BOOST)
    testAttributesGB = testAttributes[attributesGB.columns]

    #Training and testing Gradient Boosting Regression model
    error, predictedValues, pvalue = gradientBoost(attributesGB, targetGB,
                                                   testAttributesGB,
                                                   testTarget, nCpuCores,
                                                   Identifier)
    scores['Gradient Boost'] = error
    predictions['Gradient Boost'] = predictedValues
    pValues['Gradient Boost'] = pvalue

    #Training and testing K Neighbor model (Feature selection not possible for K Neighbors using RFE)
    error, predictedValues, pvalue = kNeighbor(attributes, target,
                                               testAttributes, testTarget,
                                               nCpuCores, Identifier)
    scores['K Neighbor'] = error
    predictions['K Neighbor'] = predictedValues
    pValues['K Neighbor'] = pvalue

    #Feature Selection for Linear Regression
    attributesLR, targetLR = featureSelector.featureSelection(
        attributes, target, 6, modelName=AllowedModels.LINEAR_REGRESSION)
    testAttributesLR = testAttributes[attributesLR.columns]

    #Training and testing Linear Regression model
    error, predictedValues, pvalue = linearRegression(attributesLR, targetLR,
                                                      testAttributesLR,
                                                      testTarget, nCpuCores,
                                                      Identifier)
    scores['Linear Regression'] = error
    predictions['Linear Regression'] = predictedValues
    pValues['Linear Regression'] = pvalue

    #Feature Selection for Random Forests Regression
    attributesRF, targetRF = featureSelector.featureSelection(
        attributes, target, 6, modelName=AllowedModels.RANDOM_FOREST)
    testAttributesRF = testAttributes[attributesRF.columns]

    #Training and testing Random Forests Regression model
    error, predictedValues, pvalue = randomForest(attributesRF, targetRF,
                                                  testAttributesRF, testTarget,
                                                  nCpuCores, Identifier)
    scores['Random Forest'] = error
    predictions['Random Forest'] = predictedValues
    pValues['Random Forest'] = pvalue

    #Feature Selection for SVR
    attributesSVR, targetSVR = featureSelector.featureSelection(
        attributes, target, 6, modelName=AllowedModels.SVR)
    testAttributesSVR = testAttributes[attributesSVR.columns]

    #Training and testing SVR model
    error, predictedValues, pvalue = svr(attributesSVR, targetSVR,
                                         testAttributesSVR, testTarget,
                                         nCpuCores, Identifier)
    scores['SVR'] = error
    predictions['SVR'] = predictedValues
    pValues['SVR'] = pvalue

    print("Error scores in time series prediction = {}".format(scores))
    print("P-Values in time series prediction = {}".format(pValues))
    return scores, predictions
Ejemplo n.º 5
0
 def regressionAnalysis(self, sDatasetCsv):
     aRegEst, aCovMat, aStdErr, aTStats = linearRegression(sDatasetCsv)
     print(f"Regression Estimates: {aRegEst}")
     print(f"   Covariance Matrix: {aCovMat}")
     print(f"     Standard Errors: {aStdErr}")
     print(f"        T-Statistics: {aTStats}")