Beispiel #1
0
def __fts(train, test, model_type='chen'):
    fs = Grid.GridPartitioner(data=train, npart=10)

    if model_type == 'chen':
        model = chen.ConventionalFTS(partitioner=fs)
    elif model_type == 'cheng':
        model = cheng.TrendWeightedFTS(partitioner=fs)
    else:
        model = chen.ConventionalFTS(partitioner=fs)

    model.fit(train)
    forecasts = model.predict(test)
    er = rmse(train, forecasts)

    # print(train)
    data = {
        'train': train,
        'test': test,
        'forecast': forecasts,
        # 'rmse': er
    }
    return JsonResponse(data)
Beispiel #2
0
 def __init__(self, trainData, parts, fuzzyMethod, fuzzyMode, order=1):
     self.order = order
     self.trainData = trainData
     self.fs = Grid.GridPartitioner(data=self.trainData, npart=parts)
     self.fuzzyfied = self.fs.fuzzyfy(self.trainData,
                                      method=fuzzyMethod,
                                      mode=fuzzyMode)
     self.patterns = FLR.generate_non_recurrent_flrs(self.fuzzyfied)
     if self.order > 1:
         self.modelHO = pyFTS.models.hofts.HighOrderFTS(order=self.order,
                                                        partitioner=self.fs)
         self.modelHO.fit(self.trainData)
     else:
         self.model = chen.ConventionalFTS(partitioner=self.fs)
         self.model.fit(self.trainData)
Beispiel #3
0
def index():
    train = [Enrollments.get_data()]
    test = [Enrollments.get_data()]
    fs = Grid.GridPartitioner(data=train, npart=10)
    model = chen.ConventionalFTS(partitioner=fs)
    model.fit(train)
    forecasts = model.predict(test)

    data = {
        'train': train,
        'test': test,
        'forecast': forecasts,
        # 'rmse': er
    }

    # return jsonify({'message': 'Hello World'})
    return jsonify(data)
Beispiel #4
0
def FTS(train, test):
    # Universe of Discourse Partitioner
    partitioner = Grid.GridPartitioner(data=train, npart=75)

    # Create an empty model using the Chen(1996) method
    model = chen.ConventionalFTS(partitioner=partitioner)

    # The training procedure is performed by the method fit
    model.fit(train)

    # The forecasting procedure is performed by the method predict
    forecasts = model.predict(test)

    # Plot
    plt.plot(test, color='red', label='Real Stock Price')
    plt.plot(forecasts, color='blue', label='Predicted Stock Price')
    plt.title(' Stock Price Prediction using fuzzy logic')
    plt.xlabel('Time')
    plt.ylabel('Stock Price')
    plt.legend()
    plt.show()
    print("For Fuzzy time series The mean squared error is:")
    print(mean_squared_error(test, forecasts))
    print("For fuzzy time series The R squared error is:")
    print(r2_score(test, forecasts))
    plt.style.use('fivethirtyeight')
    plt.scatter(train,
                train - model.predict(train),
                color="green",
                s=10,
                label='Train data')
    plt.scatter(test, test - forecasts, color="blue", s=10, label='Test data')
    plt.hlines(y=0, xmin=0, xmax=250, linewidth=2)
    plt.legend(loc='upper right')
    plt.title("Residual errors for FTS")
    plt.show()
Beispiel #5
0
dataset = TAIEX.get_data()
#dataset = SP500.get_data()[11500:16000]
#dataset = NASDAQ.get_data()
#print(len(dataset))

from pyFTS.partitioners import Grid, Util as pUtil
partitioner = Grid.GridPartitioner(data=dataset[:800], npart=10, transformation=tdiff)


from pyFTS.common import Util as cUtil
from pyFTS.benchmarks import benchmarks as bchmk, Util as bUtil, Measures, knn, quantreg, arima, naive

from pyFTS.models import pwfts, song, chen, ifts, hofts
from pyFTS.models.ensemble import ensemble

model = chen.ConventionalFTS(partitioner=partitioner)
#model = hofts.HighOrderFTS(partitioner=partitioner,order=2)
model.append_transformation(tdiff)
model.fit(dataset[:800])

cUtil.plot_rules(model, size=[20,20], rules_by_axis=5, columns=1)

print(model)

print("fim")



'''
model = knn.KNearestNeighbors(order=3)
#model = ensemble.AllMethodEnsembleFTS("", partitioner=partitioner)
Beispiel #6
0
from pyFTS.partitioners import Grid
from pyFTS.models import chen
from pyFTS.common import FLR
from pyFTS.common import Util
import numpy as np
from flask import Flask
from flask import render_template
from flask import request

data = pd.read_csv('4.csv')
data = data['4h'].values

fuzzy = Grid.GridPartitioner(data = data, npart = 11)
fuzzyfied = fuzzy.fuzzyfy(data, method = 'maximum', mode = 'sets')

model = chen.ConventionalFTS(partitioner = fuzzy)
model.fit(data)

app = Flask(__name__)

@app.route('/')
def home():
    return render_template('index.php')

@app.route('/predict', methods=['POST'])
def predict():
    features = [float(x) for x in request.form.values()]
    final_features = [np.array(features)]
    prediction = model.predict(final_features)

    output = round(prediction[0], 2)
Beispiel #7
0
plt.plot(data['Date'], data['avg'])
'''getting target variable'''

temp = TAIEX.get_data()
train = temp[1:4000]
test = temp[4000:5000]
'''Universe of Discourse Partitioner'''

fig, ax = plt.subplots(nrows=1, ncols=1, figsize=[10, 5])

partitioner = Grid.GridPartitioner(data=train, n=10)
partitioner.plot(ax)
plt.show()
'''creating the chen's model'''

model = chen.ConventionalFTS(name="a", partitioner=partitioner)
'''fitting data for training'''
model.fit(train)
''' Time series forecasting'''
forecasts = model.predict(test)
'''visualising the result for having rough idea of accuracy'''
plt.plot(data['Date'].dt.year[4000:5000], test)
plt.plot(data['Date'].dt.year[4000:5000], forecasts)


def rmse(predictions, targets):
    return np.sqrt(((predictions - targets)**2).mean())


rmse_value = rmse(forecasts, test)
print(rmse_value)