def make_prediction():
    model = joblib.load('model.pkl')
    if request.method == 'POST':

        entered_li = []

        month = request.form['Month']
        #day = int(request.form['Day'])
        promo = int(request.form['Promo'])
        #promo2 = int(request.form['Promo2'])
        stateH = int(request.form['StateH'])
        schoolH = int(request.form['SchoolH'])
        assortment = int(request.form['Assortment'])
        storeType = int(request.form['StoreType'])
        store = int(request.form['store'])

        date_entry = month
        year, month, day = map(int, date_entry.split('-'))

        # one-hot encode categorical variables
        stateH_encode = onehotCategorical(stateH, 4)
        assortment_encode = onehotCategorical(assortment, 3)
        storeType_encode = onehotCategorical(storeType, 4)
        store_encode = onehotCategorical(store, 1115, store=1)

        comp_dist = 5458.1
        entered_li.extend(store_encode)
        entered_li.extend(storeType_encode)
        entered_li.extend(assortment_encode)
        entered_li.extend(stateH_encode)
        entered_li.extend([comp_dist])
        #entered_li.extend([promo2])
        entered_li.extend([promo])
        entered_li.extend([day])
        entered_li.extend([month])
        entered_li.extend([schoolH])

        data = [[
            store, 1270, promo, schoolH, storeType, assortment, stateH, 6, day,
            month, year, 50, 132, 0, 0
        ]]
        df = pd.DataFrame(data,
                          columns=[
                              'Store', 'CompetitionDistance', 'Promo',
                              'SchoolHoliday', 'StoreType', 'Assortment',
                              'StateHoliday', 'DayOfWeek', 'Month', 'Day',
                              'Year', 'WeekOfYear', 'CompetitionOpen',
                              'PromoOpen', 'IsPromoMonth'
                          ])
        #data = [[1,1270,1,0,3,1,0,6,9,15,2019,37,132,0,0]]
        #df = pd.DataFrame(data,columns=['Store','CompetitionDistance','Promo','SchoolHoliday','StoreType','Assortment','StateHoliday','DayOfWeek','Month','Day','Year','WeekOfYear','CompetitionOpen','PromoOpen','IsPromoMonth'])
        prediction = model.predict(xgb.DMatrix(df))
        prediction = np.expm1(prediction)
        #prediction = model.predict(entered_li.values.reshape(1, -1))
        label = "$" + str(np.squeeze(prediction.round(2)))
        return render_template('index.html', label=label)
Exemplo n.º 2
0
def make_prediction():
    if request.method == 'POST':

        entered_li = []

        # ========== Part 2.3 ==========
        # YOUR CODE START HERE
        data_month = request.form['month']
        data_promo = request.form['promo']
        data_stateHoliday = request.form['state_holiday']
        data_assortment = request.form['assortment']
        data_store = request.form['store']
        data_dayOfWeek = request.form['day_of_the_week']
        data_promo2 = request.form['promo2']
        data_schoolHoliday = request.form['school_holiday']
        data_storeType = request.form['store_type']

        # get request values

        # one-hot encode categorical variables (only for categorical variables)
        data1 = onehotCategorical(int(data_store), 1112, 0)
        data2 = onehotCategorical(int(data_storeType), 4, 1)
        data3 = onehotCategorical(int(data_assortment), 3, 1)
        data4 = onehotCategorical(int(data_stateHoliday), 4, 1)

        # manually specify competition distance
        comp_dist = 5458.1

        # build 1 observation for prediction
        temp1 = np.append(data1, data2)
        temp1 = np.append(temp1, data3)
        temp1 = np.append(temp1, data4)
        temp2 = np.array([
            comp_dist,
            int(data_promo2),
            int(data_promo),
            int(data_dayOfWeek),
            int(data_month),
            int(data_schoolHoliday)
        ])
        entered_li = np.append(temp1, temp2)

        # ========== End of Part 2.3 ==========

        # make prediction
        prediction = model.predict(np.array(entered_li).reshape(1, -1))
        label = str(np.squeeze(prediction.round(2)))

        return render_template('index.html', label=label)
Exemplo n.º 3
0
def make_prediction():
    if request.method=='POST':

        entered_li = []

        # YOUR CODE FOR PART 2.2
        # get request values
        month = int(request.form['Month'])
        day = int(request.form['Day'])
        promo = int(request.form['Promo'])
        promo2 = int(request.form['Promo2'])
        stateH = int(request.form['StateH'])
        schoolH = int(request.form['SchoolH'])
        assortment = int(request.form['Assortment'])
        storeType = int(request.form['StoreType'])
        store = int(request.form['store'])

        # one-hot encode categorical variables
        stateH_encode = onehotCategorical(stateH, 4)
        assortment_encode = onehotCategorical(assortment, 3)
        storeType_encode = onehotCategorical(storeType, 4)
        store_encode = onehotCategorical(store, 1115, store=1)

        # manually specify competition distance
        comp_dist = 5458.1

        # engineer 1 observation for prediction
        # YOUR CODE START HERE
        entered_li.extend(store_encode)
        entered_li.extend(storeType_encode)
        entered_li.extend(assortment_encode)
        entered_li.extend(stateH_encode)
        entered_li.extend([comp_dist])
        entered_li.extend([promo2])
        entered_li.extend([promo])
        entered_li.extend([day])
        entered_li.extend([month])
        entered_li.extend([schoolH])
        
        #pkl_file = open('lr.pkl', 'rb')
        #model = pickle.load(pkl_file)
        model = joblib.load('lr.pkl')
        prediction = model.predict(np.array(entered_li).reshape(1, -1))
        #prediction = model.predict(entered_li.values.reshape(1, -1))
        label = str(np.squeeze(prediction.round(2)))

        return render_template('index.html', label=label)
Exemplo n.º 4
0
def make_prediction():
    if request.method == 'POST':

        entered_li = []

        # ========== Part 2.3 ==========
        # YOUR CODE START HERE

        # get request values
        store = int(request.form['store'])
        month = int(request.form['month'])
        promo = int(request.form['promo'])
        state_holiday = int(request.form['state_holiday'])
        assortment = int(request.form['assortment'])
        day_of_the_week = int(request.form['day_of_the_week'])
        promo2 = int(request.form['promo2'])
        school_holiday = int(request.form['school_holiday'])
        store_type = int(request.form['store_type'])

        # one-hot encode categorical variables
        onehotStore = onehotCategorical(store, 945, 1)
        onehotStore_type = onehotCategorical(store_type, 4)
        onehotAssortment = onehotCategorical(assortment, 3)
        onehotState_holiday = onehotCategorical(state_holiday, 4)

        # manually specify competition distance
        comp_dist = 5458.1

        # build 1 observation for prediction
        entered_li.extend(onehotStore)
        entered_li.append(day_of_the_week)
        entered_li.append(promo)
        entered_li.extend(onehotState_holiday)
        entered_li.append(school_holiday)
        entered_li.extend(onehotStore_type)
        entered_li.extend(onehotAssortment)
        entered_li.append(comp_dist)
        entered_li.append(promo2)
        entered_li.append(month)

        # ========== End of Part 2.3 ==========

        # make prediction
        prediction = model.predict(np.array(entered_li).reshape(1, -1))
        label = str(np.squeeze(prediction.round(2)))

        return render_template('index.html', label=label)
Exemplo n.º 5
0
def make_prediction():
    if request.method == 'POST':

        # ========== Part 2.3 ==========
        # YOUR CODE START HERE

        #******NOTE_******** I was unableto one-hot encode the 'Store' feature in my ipython notebook model so
        #I did not use the onehotCategorical function on the 'store' user input_
        #NOTE_: I extracted user inputs and one-hot encoded in the same line
        # get request values
        # one-hot encode categorical variables
        store = request.form.get("store")
        store_type = onehotCategorical(int(request.form.get("store_type")),
                                       4).astype(str)
        assortment = onehotCategorical(int(request.form.get("assortment")),
                                       3).astype(str)
        state_holliday = onehotCategorical(
            int(request.form.get("state_holliday")), 4).astype(str)
        promo2 = request.form.get("promo2")
        promo = request.form.get("promo")
        day_of_the_week = request.form.get("day_of_the_week")
        month = request.form.get("month")
        school_holliday = request.form.get("school_holliday")

        # manually specify competition distance
        comp_dist = '5458.1'

        # build 1 observation for prediction
        entered_li = np.hstack([
            store, store_type, assortment, state_holliday, comp_dist, promo2,
            promo, day_of_the_week, month, school_holliday
        ]).tolist()

        # ========== End of Part 2.3 ==========

        # make prediction
        prediction = model.predict(np.array(entered_li).reshape(1, -1))
        label = "$" + str(np.squeeze(prediction.round(2)))

        return render_template('index.html', label=label)
Exemplo n.º 6
0
def make_prediction():
    model = joblib.load('model.pkl')
    data = joblib.load('traindatapkl.pkl')
    global day
    global month
    global store
    global predictedvalue
    if request.method == 'POST':
        entered_li = []
        month = request.form['Month']
        date_entry = month
        year, month, day = map(int, date_entry.split('-'))
        promo = int(request.form['Promo'])
        stateH = int(request.form['StateH'])
        schoolH = int(request.form['SchoolH'])
        assortment = int(request.form['Assortment'])
        storeType = int(request.form['StoreType'])
        store = int(request.form['store'])

        # one-hot encode categorical variables
        stateH_encode = onehotCategorical(stateH, 4)
        assortment_encode = onehotCategorical(assortment, 3)
        storeType_encode = onehotCategorical(storeType, 4)
        store_encode = onehotCategorical(store, 1115, store=1)

        comp_dist = 5458.1
        entered_li.extend(store_encode)
        entered_li.extend(storeType_encode)
        entered_li.extend(assortment_encode)
        entered_li.extend(stateH_encode)
        entered_li.extend([comp_dist])
        #entered_li.extend([promo2])
        entered_li.extend([promo])
        entered_li.extend([day])
        entered_li.extend([month])
        entered_li.extend([schoolH])

        data = [[
            store, 1270, promo, schoolH, storeType, assortment, stateH, 6, day,
            month, year, 50, 132, 0, 0
        ]]
        df = pd.DataFrame(data,
                          columns=[
                              'Store', 'CompetitionDistance', 'Promo',
                              'SchoolHoliday', 'StoreType', 'Assortment',
                              'StateHoliday', 'DayOfWeek', 'Month', 'Day',
                              'Year', 'WeekOfYear', 'CompetitionOpen',
                              'PromoOpen', 'IsPromoMonth'
                          ])
        #data = [[1,1270,1,0,3,1,0,6,9,15,2019,37,132,0,0]]
        #df = pd.DataFrame(data,columns=['Store','CompetitionDistance','Promo','SchoolHoliday','StoreType','Assortment','StateHoliday','DayOfWeek','Month','Day','Year','WeekOfYear','CompetitionOpen','PromoOpen','IsPromoMonth'])
        prediction = model.predict(xgb.DMatrix(df))
        prediction = np.expm1(prediction)
        #prediction = model.predict(entered_li.values.reshape(1, -1))

        predictedvalue = str(np.squeeze(prediction.round(2)))
        predvalueint = np.squeeze(prediction.round(2))
        label = "$" + predictedvalue
        ########################################################
    meansales, meansales16 = meansales_of_past_years(store, month)
    percentincint = ((predvalueint - meansales) / (predvalueint)) * 100
    rawvalue = percentincint.item()
    percentinc = str(np.squeeze(percentincint.round(2))) + "%"
    if rawvalue > 0:
        positive = 1
    else:
        positive = 0
    return render_template('index.html',
                           label=label,
                           label1=percentinc,
                           label2=positive)
Exemplo n.º 7
0
def add_user(username):
    form = AddUserForm()
    #session_user = User.query.filter_by(username=session['username']).first()
    session_user = User.query.filter(User.username == username).first()
    if request.method == 'GET':
        return render_template('add_user.html',
                               form=form,
                               title='Add User Information',
                               session_username=session_user.username)
    else:
        #session_user.first_name= 'first_name'
        #session_user.last_name= 'last_name'
        #db.session.commit()
        #return redirect('/index')
        #session_user = User.query.filter_by(username=session['username']).first()
        if form.validate_on_submit():
            first_name = request.form['first_name']
            last_name = request.form['last_name']
            state = request.form['state']
            zipcode = request.form['zipcode']
            age = request.form['age']
            gender = request.form['gender']
            drug_type = request.form['drug_type']
            use_duration = request.form['use_duration']
            med_insurer = request.form['med_insurer']
            year_signed_up = request.form['year_signed_up']
            years_from_first_litigation = request.form[
                'years_from_first_litigation']
            copay_or_coinsurance = request.form['copay_or_coinsurance']
            personal_spending_per_year = request.form[
                'personal_spending_per_year']
            income = request.form['income']
            if copay_or_coinsurance == 'copay':
                total_spending = float(personal_spending_per_year) / .11
            else:
                total_spending = float(personal_spending_per_year) / .15
            #return total_spending
            session_user.first_name = first_name
            session_user.last_name = last_name
            session_user.state = state
            session_user.zipcode = zipcode
            session_user.age = age
            session_user.gender = gender
            session_user.drug_type = drug_type
            session_user.use_duration = use_duration
            session_user.med_insurer = med_insurer
            session_user.year_signed_up = year_signed_up
            session_user.years_from_first_litigation = years_from_first_litigation
            session_user.copay_or_coinsurance = copay_or_coinsurance
            session_user.personal_spending_per_year = personal_spending_per_year
            session_user.total_spending = total_spending
            session_user.income = income

            if gender == "M":
                gender_1hot = np.array([0, 1])
            else:
                gender_1hot = np.array([1, 0])

            state_1hot = onehotState(state)

            years_int = int(years_from_first_litigation)
            years_1hot = onehotCategorical(years_int, 20)
            print(state_1hot)
            #test_pred = np.array([0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 36, 56361, 305.5437079])
            #test_pred = np.hstack([gender_1hot, [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], years_1hot, [age, income, total_spending]])
            test_pred = np.hstack([
                gender_1hot, state_1hot, years_1hot,
                [age, income, total_spending]
            ])
            #prediction = model.predict(test_pred.reshape(1, -1))*473.5694972
            prediction = model.predict(test_pred.reshape(1,
                                                         -1)) * total_spending
            session_user.est_settle = float(np.squeeze(prediction.round(2)))
            #settle = "$" + str(np.squeeze(prediction.round(2)))
            db.session.commit()
            #return redirect('/index')
            return redirect(url_for('profile', username=session_user.username))
Exemplo n.º 8
0
def make_prediction():
    if request.method == 'POST':

        entered_li = []

        # YOUR CODE FOR PART 2.2
        # get request values
        quarter = int(request.form['Quarter'])
        minute = int(request.form['Minute'])
        second = int(request.form['Seconds'])
        score = int(request.form['Score'])
        fav = int(request.form['Favorite'])
        home = int(request.form['Home'])
        ps = int(request.form['PointSpread'])
        half = float(request.form['Half'])
        down = int(request.form['Down'])
        distance = int(request.form['Distance']) / 10
        field = abs(int(request.form['Field']) - 100) / 100

        time_left = (3600 - (quarter * 900) + (minute * 60) + second) / 3600
        if fav == 1:
            posteam_spread = -1 * (ps + half)
        else:
            posteam_spread = (ps + half)

        # one-hot encode categorical variables
        down_encode = onehotCategorical(down, 4)

        # engineer 1 observation for prediction
        # YOUR CODE START HERE
        entered_li.extend(down_encode)
        entered_li.extend([distance])
        entered_li.extend([field])
        entered_li.extend([score])
        entered_li.extend([home])
        entered_li.extend([time_left])
        entered_li.extend([posteam_spread])

        #pkl_file = open('lr.pkl', 'rb')
        #model = pickle.load(pkl_file)
        model = joblib.load('lr.pkl')
        prediction_o = model.predict_proba(
            np.array(entered_li).reshape(1, -1))[0][1] * 100
        prediction_d = 100 - prediction_o
        #prediction = model.predict(entered_li.values.reshape(1, -1))

        if (time_left == 0) & (score > 0):
            label_o = '100.00'
            label_d = '00.00'

        elif (time_left == 0) & (score < 0):
            label_o = '00.00'
            label_d = '100.00'

        elif (0 < time_left < (42 / 3600)) & (score > 0):
            label_o = '99.99'
            label_d = '0.01'

        elif (0 < time_left < (61 / 3600)) & (score > 0):
            label_o = '99.49'
            label_d = '0.51'

        elif (0 < time_left < (42 / 3600)) & (score < -8):
            label_o = '0.01'
            label_d = '99.99'

        elif (0 < time_left < (61 / 3600)) & (score < -8):
            label_o = '0.51'
            label_d = '99.49'

        else:
            label_o = str(np.squeeze(prediction_o.round(2)))
            label_d = str(np.squeeze(prediction_d.round(2)))
            #label_o = str(prediction_o * 100)[:5]
            #label_d = str(prediction_d * 100)[:5]

        return render_template('index.html', labelo=label_o, labeld=label_d)