how to write unittest cases for stock prediction data

0 votes
import quandl as Quandl, math
import numpy as np
import pandas as pd
from sklearn import preprocessing, model_selection as cross_validation, svm
from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt
from matplotlib import style
import datetime
import pickle

style.use('ggplot")

def get_data():
    df = Quandl.get("WIKI/AMZN",api_key='tJbUksb_TeZXtwAgDeia')
    return df
df = get_data()
#print(df.head(5))

def preprocess_data(df):
    #taking Adj. Open,  Adj. High,  Adj. Low,  Adj. Close, Adj. Volume from dataframe
    df = df[['Adj. Open',  'Adj. High',  'Adj. Low',  'Adj. Close', 'Adj. Volume']]
    #calculating the high low percentage
    df['HL_PCT'] = (df['Adj. High'] - df['Adj. Low']) / df['Adj. Low'] * 100.0
    #calculating high low percentage
    df['PCT_change'] = (df['Adj. Close'] - df['Adj. Open']) / df['Adj. Open'] * 100.0
    #adding HL_PCT, PCT_change to the dataframe
    df = df[['Adj. Close', 'HL_PCT', 'PCT_change', 'Adj. Volume']]
    df.fillna(value=-99999, inplace=True)
    df.dropna(inplace=True)
    return df
df = preprocess_data(df.head(100))
#print(df)

def pred_split(df):
    forecast_col = 'Adj. Close'
    #calculating the forecast_out
    forecast_out = int(math.ceil(0.01 * len(df)))
    df['label'] = df[forecast_col].shift(-forecast_out)
    X = np.array(df.drop(['label'], 1))
    #assign the X_testtrain data
    X_testtrain= X[:-forecast_out]
    #assign the X_pred data
    X_pred= X[-forecast_out:]
    y = np.array(df['label'])
    #assign the y_testtrain data
    y_testtrain = y[:-forecast_out]
    #assign the y_pred data
    y_pred = y[-forecast_out:]
    return X_pred,y_pred,X_testtrain,y_testtrain
X_pred,y_pred,X_testtrain,y_testtrain = pred_split(df.head(100))
#print(len(X_pred))
#print(len(y_pred))
#print(len(X_testtrain))
#print(len(y_testtrain))

def split(X_testtrain,y_testtrain):
    X_train, X_test, y_train, y_test = cross_validation.train_test_split(X_testtrain, y_testtrain, test_size=0.2)
    return X_train, X_test, y_train, y_test
X_train, X_test, y_train, y_test = split(X_testtrain,y_testtrain)
#print(len(X_train))
#print(len(X_test))
#print(len(y_train))
#print(len(y_test)

def train(X_testtrain,y_testtrain):
    model = LinearRegression(n_jobs=-1)
    model.fit(X_testtrain,y_testtrain)
    m=model.coef_
    b=model.intercept_
    return m,b,model
m,b,model = train(X_testtrain,y_testtrain)
print("coeff,intercept",m,b)

#model = train(X_train,y_train)
#print(model)

def test(X_testtrain,y_testtrain):
    confidence = model.score(X_testtrain, y_testtrain)
    return confidence
confidence = test(X_testtrain,y_testtrain)
#print('Confidence:', confidence)

def predict(X_pred):
    #find the future prediction values
    y_predict = model.predict(X_pred)

    return y_predict
y_predict=predict(X_pred)
#print("predict values",y_predict)

i wrote above code

i need unittest cases for the above code for each function

please help me with test cases
Feb 20 in Python by Vasu
• 140 points
177 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Python

0 votes
1 answer

How to write inline if statement for print in Python?

Inline if-else expression must always contain the else ...READ MORE

answered Dec 4, 2018 in Python by Nymeria
• 3,520 points

edited Dec 6, 2018 by Nymeria 6,665 views
0 votes
1 answer

How to write data to a file in Python?

Refer to the below code. data=’whatever your data ...READ MORE

answered May 12, 2019 in Python by Shaam
78 views
0 votes
1 answer

How to write a python script for downloading a video from YouTube and saving it to a directory?

First download pytube using the following code  pip ...READ MORE

answered Sep 4, 2019 in Python by Neel
• 3,010 points
680 views
+2 votes
2 answers

How to use BeatifulSoup for webscraping?

your programme is fine until you start ...READ MORE

answered Apr 4, 2018 in Python by charlie_brown
• 7,780 points
147 views
+3 votes
5 answers

How to read multiple data files in python

Firstly we will import pandas to read ...READ MORE

answered Apr 6, 2018 in Python by DeepCoder786
• 1,720 points
8,333 views
+1 vote
4 answers

How to write nested dictionaries to a CSV file

Using DictWriter there is no need in ...READ MORE

answered Oct 18, 2018 in Python by Richard William
12,559 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 1,186 views
0 votes
0 answers
+4 votes
6 answers