コード例 #1
0
def upload_image():
    if request.method == "POST":
        if request.files:
            fasta = request.files["fasta"]
            if fasta.filename == "":
                #print("No filename")
                flash('Warning! Select a fasta file before upload and run')
                return redirect(request.url)
            if allowed_files(fasta.filename):
                filename = secure_filename(fasta.filename)
                path = os.path.join(app.config["UPLOAD_FOLDER"], filename)
                #print(path)
                fasta.save(path)
                #print("fasta saved")
                #data = handle_fa(path)
                prediction(path)
                if os.path.exists(path):
                    if filename != 'sample.fasta':
                        os.remove(path)
                filename = 'prediction.csv'
                return redirect('/downloadfile/'+ filename)
            else:
                #print("That file extension is not allowed")
                flash('Error! That file extension is not allowed, upload only fasta file')
                return redirect(request.url)
    return render_template('index.html')
コード例 #2
0
ファイル: main.py プロジェクト: dianapaula19/ami-ai-project
def main():

    data = Data('./data/train.csv', './data/test.csv')
    data.preprocess_data()
    x_train_sets, y_train_sets, x_test_sets, y_test_set = data.k_fold_train_test_sets(
    )

    models = ["LogisticRegression", "VotingClassifier"]

    for model in models:

        k_fold_mean_score(x_train_sets, y_train_sets, x_test_sets, y_test_set,
                          model)

    x_train, x_test = Data.train_test_vectors(data.TRAIN_DF.text,
                                              data.TEST_DF.text, [3000, 0.4],
                                              "TFIDF")
    y_train = data.TRAIN_DF.label

    print("Predictions for the LogisticRegression Model")
    y_pred = prediction(x_train, y_train, x_test, "LogisticRegression")

    write_submission(y_pred, "./submissions/lr_submission.csv")

    print("Predictions for the VotingClassifier Model")
    y_pred = prediction(x_train, y_train, x_test, "VotingClassifier")

    write_submission(y_pred, "./submissions/evc_submission.csv")
コード例 #3
0
ファイル: main.py プロジェクト: walkon302/MEMA_organization
def main():
    mode = sys.argv[1]
    if mode == 'predict':
        output_name = sys.argv[2]
        model_name = sys.argv[3]

        print '---------------------------------------------'
        print 'Predicting new images'
        print '---------------------------------------------'

        pred_array, pred_file = dp.CNNDataPreProcess.predict_prep('predict')
        pred_pre = dp.CNNDataPreProcess.cnn_preprocess(pred_array)
        pred_result = model.pred_model(pred_pre, model_name)

        model.prediction(pred_result, pred_file, output_name)

    else:

        print '---------------------------------------------'
        print 'Load image, black_white them, and resize them'
        print '---------------------------------------------'

        if mode == 'train':
            training_step = int(sys.argv[2])
            model_name = sys.argv[3]

            train_sample, train_label = (dp.CNNDataPreProcess.train_eval_prep(
                'train_organized', 'train_disorganized', 'train'))

            print '---------------------------------------------'
            print 'Train model'
            print '---------------------------------------------'

            model.train_model(train_sample, train_label, training_step,
                              model_name)

        if mode == 'eval':
            model_name = sys.argv[2]

            eval_sample, eval_label = (dp.CNNDataPreProcess.train_eval_prep(
                'eval_organized', 'eval_disorganized', 'eval'))

            print '---------------------------------------------'
            print 'Evaluate model'
            print '---------------------------------------------'
            print '---------------------------------------------'
            print 'Evaluation Accuracy'
            print '---------------------------------------------'

            print model.eval_model(eval_sample, eval_label, model_name)
コード例 #4
0
ファイル: app.py プロジェクト: sg23600/stock-dash
def forecast(n, n_days, val):
    if n == None:
        return [""]
    if val == None:
        raise PreventUpdate
    fig = prediction(val, int(n_days) + 1)
    return [dcc.Graph(figure=fig)]
コード例 #5
0
ファイル: server.py プロジェクト: alok217/ModelforEkheti
def home():
    # print(request.form)
    data = request.form.get('data')
    print(data)
    h = prediction(str(data))
    # print('hi',h)
    return str(h)
コード例 #6
0
def search():
    if "image" not in request.files:
        return jsonify({"error": "image file is required"})
    try:
        image_file = request.files["image"].read()
        npimg = np.fromstring(image_file, np.uint8)
        img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
        img_pil = Image.fromarray(img)
        image_feature = prediction(img_pil).squeeze().cpu().detach().numpy()
        close_list = main_search(image_feature)
        print(len(close_list))
        ret = []
        scores = []
        for i in close_list:
            ret.append(i["image"])
            scores.append(str(i["score"]))
        # print(ret)
        # return jsonify(close_list)
        # return json.dumps(close_list)
        return jsonify({"list": ret, "scores": scores})

        # return render_template('template.html', image_name=ret)
    except Exception as e:
        print("error in deploy.py line 41", str(e))
        return jsonify(
            {"error": "something went wrong with the uploaded file"})
コード例 #7
0
ファイル: main.py プロジェクト: dianapaula19/ami-ai-project
def k_fold_mean_score(x_train_sets, y_train_sets, x_test_sets, y_test_set,
                      model):

    print("Model: " + model)
    final_confusion_matrix = [[0, 0], [0, 0]]
    scores = []
    for x_train, y_train, x_test, y_test in zip(x_train_sets, y_train_sets,
                                                x_test_sets, y_test_set):
        x_train, x_test = Data.train_test_vectors(x_train, x_test, [3000, 0.4],
                                                  "TFIDF")
        y_pred = prediction(x_train, y_train, x_test, model)
        tn, fp, fn, tp = confusion_matrix(y_test, y_pred).ravel()
        final_confusion_matrix[0][0] += tn
        final_confusion_matrix[0][1] += fp
        final_confusion_matrix[1][0] += fn
        final_confusion_matrix[1][1] += tp
        scores.append(score_on_model(x_train, y_train, x_test, y_test, model))

    avg = np.mean(scores, axis=0)

    print("Score for train: " + str(avg[0]))
    print("Score for test: " + str(avg[1]))
    print(final_confusion_matrix)

    plt.figure(figsize=(10, 7))
    plt.title(model)
    sn.heatmap(final_confusion_matrix,
               annot=True,
               fmt='g',
               linewidths=10,
               cmap="YlGnBu")
    plt.savefig('./plots/confusion_matrix_' + model)
コード例 #8
0
def loadarguments():
    step_loss = []
    if args['L'] == False:
        pickle.dump(step_loss, open(args['loss'], 'wb'))
    #pickle.dump(step_loss, open(args['loss'], 'wb'))
    torch.set_default_tensor_type('torch.FloatTensor')
    torch.manual_seed(args['seed'])
    shared_model = Policy(3, args['action_space'])

    if args['curiosity']:
        final_dim = 6 * 6 * 32
        inverse_model = Inverse(3, args['action_space'])
        mapping_model = mapping(args['action_space'], final_dim)
        prediction_model = prediction(args['action_space'], final_dim)
    else:
        inverse_model = None
        mapping_model = None
        prediction_model = None

    if device == 'cuda':
        with torch.cuda.device(1):
            shared_model.cuda()
            if args['curiosity']:
                inverse_model.cuda()
                mapping_model.cuda()
                prediction_model.cuda()

    if args['L']:
        if device == 'cpu':
            shared_model.load_state_dict(
                torch.load(args['lmn'], map_location=torch.device('cpu')))
            if args['curiosity']:
                model_name = args['curiosity_mn']
                inverse_model.load_state_dict(
                    torch.load(model_name + '_inverse',
                               map_location=torch.device('cpu')))
                mapping_model.load_state_dict(
                    torch.load(model_name + '_mapping',
                               map_location=torch.device('cpu')))
                prediction_model.load_state_dict(
                    torch.load(model_name + '_prediction',
                               map_location=torch.device('cpu')))

        else:
            shared_model.load_state_dict(torch.load(args['lmn']))
            if args['curiosity']:
                model_name = args['curiosity_mn']
                inverse_model.load_state_dict(
                    torch.load(model_name + '_inverse'))
                mapping_model.load_state_dict(
                    torch.load(model_name + '_mapping'))
                prediction_model.load_state_dict(
                    torch.load(model_name + '_prediction'))
    shared_model.share_memory()
    if args['curiosity']:
        inverse_model.share_memory()
        mapping_model.share_memory()
        prediction_model.share_memory()
    return shared_model, inverse_model, mapping_model, prediction_model
コード例 #9
0
def result():
   if request.method == 'POST':
      sym1 = request.form.get('symptom 1')
      sym2 = request.form.get('symptom 2')
      sym3 = request.form.get('symptom 3')
      import model
      pred = model.prediction(sym1,sym2,sym3)
      return render_template("result.html",result =pred)
コード例 #10
0
def predict(id):
    words = request.args.get("words")

    np_words = np.array([words])

    test_data = {'instances': np_words.tolist()}
    data = json.dumps(test_data)
    response_json = prediction(np_words.tolist())

    return response_json
コード例 #11
0
def loadarguments():
    global env_conf
    global env
    global setup_json
    global shared_model
    global inverse_model
    global mapping_model
    global prediction_model
    global saved_state
    global optimizer
    global torch
    global step_loss

    #step_loss = pickle.load(open('TDW_A2C_step_loss_reward_sum.p', 'rb'))
    step_loss = []
    if args['L'] == False:
        pickle.dump(step_loss,
                    open('TDW_curiosity_PPO_step_loss_reward_sum.p', 'wb'))

    torch.set_default_tensor_type('torch.FloatTensor')
    torch.manual_seed(args['seed'])

    the_gpu_id = 1
    shared_model = Policy(3, action_space)

    final_dim = 800

    inverse_model = Inverse(3, action_space)
    mapping_model = mapping(action_space, final_dim)
    prediction_model = prediction(action_space, final_dim)

    if device == 'cuda' and True:
        with torch.cuda.device(the_gpu_id):
            shared_model.cuda()
            inverse_model.cuda()
            mapping_model.cuda()
            prediction_model.cuda()

    shared_model.share_memory()
    inverse_model.share_memory()
    mapping_model.share_memory()
    prediction_model.share_memory()

    if args['L']:
        shared_model.load_state_dict(torch.load('tdw_ppo_curiosity_model'))
        inverse_model.load_state_dict(
            torch.load('tdw_ppo_curiosity_model_inverse'))
        mapping_model.load_state_dict(
            torch.load('tdw_ppo_curiosity_model_mapping'))
        prediction_model.load_state_dict(
            torch.load('tdw_ppo_curiosity_model_prediction'))

    optimizer = None
コード例 #12
0
def makeprediction():
    result = model.prediction(fileName)
    print(result)
    result1 = np.amax(result[0])
    result = np.where(result[0] == result1)

    if result[0] == 0:
        predicted.configure(text='About')
        predicted.place(x=298, y=182.5)
    elif result[0] == 1:
        predicted.configure(text='Banks')
        predicted.place(x=298, y=182.5)
    elif result[0] == 2:
        predicted.configure(text='Called')
        predicted.place(x=298, y=182.5)
コード例 #13
0
def predict():
    if "image1" not in request.files:
        return jsonify({"error": "image file is required"})

    #print(request.files.get("image"))
    try:
        filestr = request.files['image1'].read()
        filestr2 = request.files['image2'].read()
        typ = request.form["type"]
        if typ is None:
            typ = "tripplet"
        npimg = np.fromstring(filestr, np.uint8)
        npimg2 = np.fromstring(filestr2, np.uint8)
        img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
        img2 = cv2.imdecode(npimg2, cv2.IMREAD_COLOR)
        img_pil = Image.fromarray(img)
        img_pil2 = Image.fromarray(img2)
        output1 = prediction(img_pil,
                             model_type=typ).squeeze().cpu().detach().numpy()
        output2 = prediction(img_pil2,
                             model_type=typ).squeeze().cpu().detach().numpy()
        # print(cosine_similarity(output1,output2))
        value = cosine_similarity(output1, output2)
        if value > 0.5:
            result = "same image"
        else:
            result = "different image"
        return jsonify({
            "similarity_score":
            str("Result :" + result + " " +
                " , \n\n Cosine Similarity Value :" + str(value))
        })
    except Exception as e:
        print("error in deploy.py line 63", str(e))
        return jsonify(
            {"error": "something went wrong with the uploaded file"})
コード例 #14
0
def predict():
    if request.method == "POST":
        location = request.form['location']
        bhk = request.form['bhk']
        bath = request.form['bath']
        balcony = request.form['balcony']
        sqft = request.form['sqft']
        area_type = request.form['area_type']
        availability = request.form['availability']

        pred = model.prediction(location, bhk, bath, balcony, sqft, area_type,
                                availability)

        return render_template("result.html", prediction=pred)

    else:
        return render_template('index.html')
コード例 #15
0
ファイル: gui.py プロジェクト: ziyedy/python-demo
def show_result():
    """ 显示结果 """
    img_path = e.get()
    if len(img_path) == 0:
        tkinter.messagebox.showinfo('提示', '请选择图片路径')
        return

    try:
        img = cv2.imread(img_path)
        test_data = imgProcess.process(img)
        result = model.prediction(test_data)
        plt.imshow(img)
        plt.axis('off')
        plt.title(result, fontsize=20)
        plt.show()
    except:
        tkinter.messagebox.showinfo('提示', '请选择图片路径')
コード例 #16
0
def predict_test():
    words = 'bangun tidur siang tengah aneh pindah texas milik konsentrasi halhal kerja rumah kelas 10 cepat jam ' \
            'dentang 4 henti mudah laku pindah kerja rumah tantang kerja sibuk putus habis berjamjam laku bayar ' \
            'perhati kelas barangbarang benarbenar keras tinggal lacak tahun malas jenius hei lambat baik benarbenar ' \
            'fokus tinggal kampus konsentrasi mudah sayang tinggal rumah awas ketat tua omel adik omel omel omel ' \
            'titik repot pergi jalan sekolah pergi pustaka ajar pindah memberitahu salah pindah pergi lindung milik ' \
            'khawatir dunia satusatunya jaga kamar bersih bantu bisnis uang ut hidup asrama apartemen semester pikir ' \
            'ambil untung off topik pergi jalan enam malam milik ledak cinta austin tinggal va pergi dc waktu milik ' \
            'ledak siswa lari malam bersenangsenang tanggung bersenangsenang prioritas lurus tinggal rumah kau harap ' \
            'laku tanggung adik kacau pergi gila pindah guru tinggi kacau karir guru tinggi pesta alas utama pergi ' \
            'bersenangsenang biar pergi jajah dunia india budaya india nilainilai india lawan bersenangsenang maksud ' \
            'temu orangorang pacar pesta bersenangsenang sekolah sulit pikir milik bebas tempat tekan buat sekolah ' \
            'tua harap senang tulis pergi tulis bantu pikir urut harap bersenangsenang baca untung ta '
    input = np.array([words])

    test_data = {'instances': input.tolist()}
    data = json.dumps(test_data)
    response_json = prediction(input.tolist())

    return response_json
コード例 #17
0
def run_prediction(
        data_path=None, 
        model_properties=None):
    '''
    Prediction's for the Model
    '''
    if data_path is None or model_properties is None:
        raise ValueError('Need Data path , Config pathand Model Properties as argument !')
    config_map = model_properties['config_map']
    data_to_predict = read_json_data(data_path)
    data = data_to_predict.copy()
    data = data.replace('NA', np.NaN)
    data[config_map['encode_columns']] = data[config_map['encode_columns']].astype(float)
    data[config_map['scale_columns']] = data[config_map['scale_columns']].astype(float)
    data = impute_data(data, config_map)
    data = one_hot_encoding(data, config_map['encode_columns'])
    data = hash_encoding(data, config_map['hash_columns'], 5, 'string')
    data = data[model_properties['X_train'].columns]
    predictions = prediction(data, model_properties)
    data_to_predict['predicted_delivery_seconds'] = list(predictions.flatten())
    predicted_data = data_to_predict[['delivery_id', 'predicted_delivery_seconds']]
    return predicted_data
コード例 #18
0
def predict():
    #These are for the API request
    TIME_DELTA = 1
    hours = 80 * 24  #DAYS PER HOURS

    #Retrieve form selected data
    selected_crypto = list(request.form.values())

    #Fetch market data
    crypto_series = fetch_market_data.get_historical_hourly_price(
        selected_crypto[0], 'USD', hours, TIME_DELTA)

    #Normalization data
    series_norm = normalization(crypto_series)
    pred = prediction(series_norm)

    #Este retorna el gráfico del api
    #return render_template('index.html', prediction_text='You selected: {}'.format(plot_close_price(crypto_series,selected_crypto[0])))

    #Este debería retornar el pred
    #INTEL MKL ERROR: El sistema operativo no puede ejecutar %1. mkl_intel_thread.dll.
    #Intel MKL FATAL ERROR: Cannot load mkl_intel_thread.dll.
    return render_template('index.html',
                           prediction_text='You selected: {}'.format(pred))
コード例 #19
0
def predict():
    input_features=[x for x in request.form.values()]
    gender=input_features[0]
    age=int(input_features[1])
    hypertension=int(input_features[2])
    disease=int(input_features[3])
    married=input_features[4]
    work=str(input_features[5])
    area=input_features[6]
    glucose=float(input_features[7])
    bmi=float(input_features[8])
    smoke=input_features[9]
    # l=[gender,age,hypertension,disease,married,work,area,glucose,bmi,smoke]

    predicted_value=model.prediction(gender,age,hypertension,disease,married,work,area,glucose,bmi,smoke)

    if predicted_value==0:
        predicted_value="Good News! Patient has no Stroke"
    elif predicted_value==1:
        predicted_value="Oops! Patient going with Stroke"



    return render_template('index.html',predicted_text=predicted_value)
コード例 #20
0
def predict():
    predicted_talks = prediction()
    return jsonify(predicted_talks), 200
コード例 #21
0
ファイル: flask_app.py プロジェクト: Shaheer-ahmad/plant
import json
import os
from flask import Flask, jsonify, request
from flask_cors import CORS
from model import prediction
import tensorflow as tf
import base64
import ast

app = Flask(__name__)
CORS(app)

predictionObj = prediction()


@app.route("/predict/", methods=['POST', 'GET'])
def predict():
    try:
        encoded_string = request.form.get('BASE64')
        # image = 'pi2235-03-3.jpg'
        # encoded_string = ""
        # with open(image, "rb") as image_file:
        #     encoded_string = base64.b64encode(image_file.read())

        ret, predicted_class, name, family, author = predictionObj.predict(
            encoded_string)

        if not ret:
            ret_dict = {'Error': True, 'Message': 'Error in prediction'}
            return jsonify(ret_dict)
コード例 #22
0
import pandas as pd
import datetime
import pickle
from model import prediction

today = datetime.date.today()
next_week = today + datetime.timedelta(days=7)

matches = pd.DataFrame.from_csv('future_matches.csv')
matches['date'] = pd.to_datetime(matches['date'])
next_matches = matches[matches['date'].between(today,next_week)]
while len(next_matches)== 0:
    next_week = next_week + datetime.timedelta(days=7)
    next_matches = matches[matches['date'].between(today, next_week)]

current_ratings = pickle.load(open('ratings_mid_2017_2.pickle','rb'))
predictions = []
for i,match in next_matches.iterrows():
    home_team = match['home_team']
    away_team = match['away_team']
    spread, home_win_prob = prediction(home_team,away_team,n_sims=1000,use_ratings=current_ratings)
    predictions.append([match['date'],home_team,away_team,spread,home_win_prob])
pd.DataFrame(predictions).to_csv('predictions_next_week.csv')


コード例 #23
0
def main():
    anssdic = {}

    front_up()
    try:
        st.write('User Logged in as', name_dict['name'])
        pk = login_dict["key"]
        na = name_dict['name']
        #st.write("primary key" ,pk)
        st.write("Name ", na)
    except:
        st.error(" Please Log in")

    st.title("SURVEY")

    if st.checkbox('Question I', key='q1'):
        st.info(" Did your child struggle to learn to count?")

        ansq1 = st.radio("Answer:",
                         ['Yes-frequently', 'Sometimes', 'No-never'],
                         key='q1')
        anssdic['ansq1'] = ansq1
        if st.button("SUBMIT", key='q1'):
            st.write(ansq1)
            marks1 = submit(ans=ansq1, question="1")
            st.write(marks1)
            diss.update({'marks1': marks1})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question II', key='q2'):
        st.info(
            "Does he/she say numbers out of order — long after peers have mastered this skill? "
        )

        ansq2 = st.radio("Answer:",
                         ['Yes-frequently', 'Sometimes', 'No-never'],
                         key='q2')
        anssdic['ansq2'] = ansq2
        if st.button("SUBMIT", key='q2'):
            st.write(ansq2)
            marks2 = submit(ans=ansq2, question="2")
            diss.update({'marks2': marks2})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question III', key='q3'):
        st.info(
            " Does your child not seem to understand the connection between the symbol “4” and the word “four?” Does he make mistakes when reading or following directions involving number words and symbols?"
        )

        ansq3 = st.radio("Answer:",
                         ['Yes-frequently', 'Sometimes', 'No -never'],
                         key='q3')
        anssdic['ansq3'] = ansq3
        if st.button("SUBMIT", key='q3'):
            st.write(ansq3)
            marks3 = submit(ans=ansq3, question="3")
            diss.update({'marks3': marks3})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question IV', key='q4'):
        st.info(
            "Does your child struggle to connect the concept of numbers to real-world items? When you ask him how many cookies are left, for example, does he seem confused by the question or answer incorrectly?"
        )

        ansq4 = st.radio("Answer", ['Yes-frequently', 'Sometimes', 'No-never'],
                         key='q4')
        anssdic['ansq4'] = ansq4
        if st.button("SUBMIT", key='q4'):
            st.write(ansq4)
            marks4 = submit(ans=ansq4, question="4")

            diss.update({'marks4': marks4})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question V', key='q5'):
        st.info(
            "Does your child not seem to understand the difference between adding and subtracting? Does she confuse the  +  and  –  symbols when completing math problems?"
        )

        ansq5 = st.radio("Answer:",
                         ['Yes-frequently', 'Sometimes', 'No-never'],
                         key='q5')
        anssdic['ansq5'] = ansq5
        if st.button("SUBMIT", key='q5'):
            st.write(ansq5)
            marks5 = submit(ans=ansq5, question="5")
            diss.update({'marks5': marks5})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question VI', key='q6'):
        st.info("Does your child still count on his fingers past third grade?")

        ansq6 = st.radio("Answer:",
                         ['Yes-frequently', 'Sometimes', 'No-never'],
                         key='q6')
        anssdic['ansq6'] = ansq6
        if st.button("SUBMIT", key='q6'):
            st.write(ansq6)
            marks6 = submit(ans=ansq6, question="6")
            diss.update({'marks6': marks6})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question VII', key='q7'):
        st.info(" Difficulty sustaining attention; seems "
                "hyper"
                " or "
                "daydreamer"
                " ")

        ansq7 = st.radio("Answer:", ['Yes', 'No', 'Unknown'], key='q7')
        anssdic['ansq7'] = ansq7
        if st.button("SUBMIT", key='q7'):
            st.write(ansq7)
            marks7 = submit(ans=ansq7, question="7")
            diss.update({'marks7': marks7})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question VIII', key='q8'):
        st.info(
            "Confused by letters, numbers, words, sequences, or verbal explanations."
        )

        ansq8 = st.radio("Answer:", ['Yes', 'No', 'Unknown'], key='q8')
        anssdic['ansq8'] = ansq8
        if st.button("SUBMIT", key='q8'):
            st.write(ansq8)
            marks8 = submit(ans=ansq8, question="8")
            diss.update({'marks8': marks8})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question IX', key='q9'):
        st.info(" Reads and rereads with little comprehension")

        ansq9 = st.radio("Answer:", ['Yes', 'No', 'Unknown'], key='q9')
        anssdic['ansq9'] = ansq9
        if st.button("SUBMIT", key='q9'):
            st.write(ansq9)
            marks9 = submit(ans=ansq9, question="9")
            diss.update({'marks9': marks9})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question X', key='q10'):
        st.info(
            "Difficulty putting thoughts into words; speaks in halting phrases; leaves sentences incomplete."
        )

        ansq10 = st.radio("Answer:", ['Yes', 'No', 'Unknown'], key='q10')
        anssdic['ansq10'] = ansq10
        if st.button("SUBMIT", key='q10'):
            st.write(ansq10)
            marks10 = submit(ans=ansq10, question="10")
            diss.update({'marks10': marks10})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question XI', key='q11'):
        st.info(
            "Can count, but has difficulty counting objects and dealing with money."
        )

        ansq11 = st.radio("Answer:", ['Yes', 'No', 'Unknown'], key='q11')
        anssdic['ansq11'] = ansq11
        if st.button("SUBMIT", key='q11'):
            st.write(ansq11)
            marks11 = submit(ans=ansq11, question="11")
            diss.update({'marks11': marks11})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question XII', key='q12'):
        st.info(
            "ory for sequences, facts and information that has not been experienced."
        )

        ansq12 = st.radio("Answer:", ['Yes', 'No', 'Unknown'], key='q12')
        anssdic['ansq12'] = ansq12
        if st.button("SUBMIT", key='q12'):
            st.write(ansq12)
            marks12 = submit(ans=ansq12, question="12")
            diss.update({'marks12': marks12})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question XIV', key='q13'):
        st.info(
            "Complains of dizziness, headaches or stomach aches while reading."
        )

        ansq13 = st.radio("Answer:", ['Yes', 'No', 'Unknown'], key='q13')
        anssdic['ansq13'] = ansq13
        if st.button("SUBMIT", key='q13'):
            st.write(ansq13)
            marks13 = submit(ans=ansq13, question="13")
            diss.update({'marks13': marks13})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question XIV', key='q14'):
        st.info("Is reading extremely difficult? (Below grade or age level.")

        ansq14 = st.radio("Answer:", ['Yes', 'No', 'Unknown'], key='q14')
        anssdic['ansq14'] = ansq14
        if st.button("SUBMIT", key='q14'):
            st.write(ansq14)
            marks14 = submit(ans=ansq14, question="14")
            diss.update({'marks14': marks14})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question XV', key='q15'):
        st.info("Is spelling ability poor? Letters missed, reversed etc?")

        ansq15 = st.radio("Answer:", ['Yes', 'No', 'Unknown'], key='q15')
        anssdic['ansq15'] = ansq15
        if st.button("SUBMIT", key='q15'):
            st.write(ansq15)
            marks15 = submit(ans=ansq15, question="15")
            diss.update({'marks15': marks15})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question XVI', key='q16'):
        st.info(
            "Is it difficult to rhyme words?(Not sure? Play a rhyming game with your child or student)."
        )

        ansq16 = st.radio("Answer:", ['Yes', 'No', 'Unknown'], key='q16')
        anssdic['ansq16'] = ansq16
        if st.button("SUBMIT", key='q16'):
            st.write(ansq16)
            marks16 = submit(ans=ansq16, question="16")
            diss.update({'marks16': marks16})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question XVII', key='q17'):
        st.info(
            "Is there difficulty telling time on a clock with hands and/or tying shoes with laces?"
        )

        ansq17 = st.radio("Answer:", ['Yes', 'No', 'Unknown'], key='q17')
        anssdic['ansq17'] = ansq17
        if st.button("SUBMIT", key='q17'):
            st.write(ansq17)
            marks17 = submit(ans=ansq17, question="17")
            diss.update({'marks17': marks17})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question XVIII', key='q18'):
        st.info(
            "Is there difficulty finding the right words while speaking? Lots of ums, ahs, 'those things', and 'that stuff'."
        )

        ansq18 = st.radio("Answer:", ['Yes', 'No', 'Unknown'], key='q18')
        anssdic['ansq18'] = ansq18
        if st.button("SUBMIT", key='q18'):
            st.write(ansq18)
            marks18 = submit(ans=ansq18, question="18")
            diss.update({'marks18': marks18})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question XIX', key='q19'):
        st.info(
            " Pauses, repeats or makes frequent mistakes when reading aloud.")

        ansq19 = st.radio("Answer:", ['Yes', 'No', 'Unknown'], key='q19')
        anssdic['ansq19'] = ansq19
        if st.button("SUBMIT", key='q19'):
            st.write(ansq19)
            marks19 = submit(ans=ansq19, question="19")
            diss.update({'marks19': marks19})
            #st.write(anssdic)
            #st.write(diss)

    if st.checkbox('Question XX', key='q20'):
        st.info("Unusually high or low tolerance for pain.")

        ansq20 = st.radio("Answer:", ['Yes', 'No', 'Unknown'], key='q20')
        anssdic['ansq20'] = ansq20
        if st.button("SUBMIT", key='q20'):
            st.write(ansq20)
            marks20 = submit(ans=ansq20, question="20")
            diss.update({'marks20': marks20})
            st.write("Your answers", anssdic)
            #st.write(diss)

    if st.button("Submit the quiz", key='submit'):
        try:
            if bool(anssdic['ansq1']) and bool(anssdic['ansq2']) and bool(
                    anssdic['ansq3']
            ) and bool(anssdic['ansq4']) and bool(anssdic['ansq5']) and bool(
                    anssdic['ansq6']
            ) and bool(anssdic['ansq7']) and bool(anssdic['ansq8']) and bool(
                    anssdic['ansq9']
            ) and bool(anssdic['ansq10']) and bool(anssdic['ansq11']) and bool(
                    anssdic['ansq12']) and bool(anssdic['ansq13']) and bool(
                        anssdic['ansq14']) and bool(
                            anssdic['ansq15']) and bool(
                                anssdic['ansq16']) and bool(
                                    anssdic['ansq17']) and bool(
                                        anssdic['ansq18']) and bool(
                                            anssdic['ansq19']) and bool(
                                                anssdic['ansq20']):
                conn = MySQLdb.connect("localhost", "ryan", "mark50",
                                       "dyslexia")
                c = conn.cursor()
                query = 'INSERT INTO survey(pk,name,mone,mtwo,mthree,mfour,mfive,msix,mseven,meight,mnine,mten,meleven,mtwelve,mthirteen, mfourteen,mfifteen,msixteen, mseventeen, meighteen,mnineteen,mtwenty) VALUES ("%s", "%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s");' % (
                    pk, na, diss['marks1'], diss['marks2'], diss['marks3'],
                    diss['marks4'], diss['marks5'], diss['marks6'],
                    diss['marks7'], diss['marks8'], diss['marks9'],
                    diss['marks10'], diss['marks11'], diss['marks12'],
                    diss['marks13'], diss['marks14'], diss['marks15'],
                    diss['marks16'], diss['marks17'], diss['marks18'],
                    diss['marks19'], diss['marks20'])

                #query = 'INSERT INTO quiz(pk,name,mone,mtwo,mthree,mfour,mfive,msix,mseven,meight,mnine,mten) VALUES ("%s", "%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s");' % (pk,na,dis['marks1'],dis['marks2'],dis['marks3'],dis['marks4'],dis['marks5'],dis['marks6'],dis['marks7'],dis['marks8'],dis['marks9'],dis['marks10'])
                c.execute(query)
                conn.commit()
        except KeyError:
            st.warning("Please Login")
        except MySQLdb.IntegrityError:
            st.warning("Already Submitted")

        except UnboundLocalError:
            st.warning("Please answer all questions")

#CREATE TABLE survey (pk int PRIMARY KEY,name varchar(100),mone int,mtwo int,mthree int,mfour int,mfive int ,msix int,mseven int,meight int,mnine int,mten int,meleven int,mtwelve int,mthirteen int , mfourteen int,mfifteen int,msixteen int, mseventeen int, meighteen int,mnineteen int,mtwenty int);
#query = 'INSERT INTO quiz(pk,name,mone,mtwo,mthree,mfour,mfive,msix,mseven,meight,mnine,mten,meleven,mtwelve,mthirteen, mfourteen,mfifteen,msixteen, mseventeen, meighteen,mnineteen,mtwenty) VALUES ("%s", "%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s","%s");' % (pk,na,diss['marks1'],diss['marks2'],diss['marks3'],diss['marks4'],diss['marks5'],diss['marks6'],diss['marks7'],diss['marks8'],diss['marks9'],diss['marks10'],diss['marks11'],diss['marks12'],diss['marks13'],diss['marks14'],diss['marks15'],diss['marks16'],diss['marks17'],diss['marks18'],diss['marks19'],diss['marks20'])

    if st.button("Check your result", key='result'):

        try:
            with st.spinner(text="DyslexiaML is Analyzing"):
                pred = prediction(user_prime_key=pk)
                if int(pred) == 2:
                    st.balloons()
                    st.success("Your Child Does Not Has Dyslexia!! ")
                elif int(pred) == 1:
                    st.success(
                        "Your Child Has Mild Dyslexia Symptoms. Consult Doctor "
                    )
                else:
                    st.success(
                        "Your Child Has Dyslexia Symptoms. Consult Doctor ")
        except UnboundLocalError:
            st.warning("Please Login")
コード例 #24
0

# In[11]:

normX = featurescaling(X[:, 1:])
X[:, 1:] = normX[0]

# In[12]:

theta = np.zeros(shape=(X.shape[1], 1))

# In[13]:

result = gradientdescent(X, y, theta, 0.05, 5000, 0)
finaltheta = result[0]

# In[29]:

from model import prediction

# In[30]:

predict = prediction(finaltheta, normX[1], normX[2])

# In[37]:

import pickle
modelname = modelname + ".pkl"
with open(modelname, 'wb') as output:
    pickle.dump(predict, output)
コード例 #25
0
def predict():
    predicted_talks = prediction()
    return jsonify(predicted_talks), 200
コード例 #26
0
ファイル: train.py プロジェクト: rsepassi/tf-play
import tfutils.opt
import tfutils.modeling

import data
import model as mnist
import trainer

# Data
batcher = data.batcher()
x, y = batcher.placeholders()

# Model
logits = mnist.logits(x)  # logits

# Accuracy
y_hat = mnist.prediction(logits)  # predicted label
accuracy = tfutils.modeling.accuracy(y, y_hat)

# Optimization
step = tfutils.opt.global_step()
cost = mnist.cost(y, logits, regularize=True)
train_step = tf.train.MomentumOptimizer(0.0005, 0.9).minimize(
        cost, global_step=step)

model_vars = {
    'x': x,
    'y': y,
    'logits': logits,
    'y_hat': y_hat,
    'accuracy': accuracy,
    'cost': cost,
コード例 #27
0
def train():
    global FLAGS
    training = tf.placeholder(tf.bool)
    global_step = tf.get_variable(
        'global_step',
        trainable=False,
        shape=(),
        dtype=tf.int32,
        initializer=tf.zeros_initializer())
    x = tf.placeholder(
        tf.float32, shape=[FLAGS.batch_size, FLAGS.sequence_len])
    seq_length = tf.placeholder(tf.int32, shape=[FLAGS.batch_size])
    y_indexs = tf.placeholder(tf.int64)
    y_values = tf.placeholder(tf.int32)
    y_shape = tf.placeholder(tf.int64)
    y = tf.SparseTensor(y_indexs, y_values, y_shape)
    num_channels = [FLAGS.hiddens] * (FLAGS.layers - 1) + [5]
    kernel_size = FLAGS.ksize
    dropout = FLAGS.dout
    logits, ratio = model.inference(x, num_channels, FLAGS.sequence_len, kernel_size, training, dropout)
    ctc_loss = model.loss(logits, seq_length, y)
    tf.summary.scalar('ctc_loss', ctc_loss)
    opt = model.train_opt(
        FLAGS.step_rate,
        FLAGS.max_steps,
        global_step=global_step,
        opt_name="Adam")

    gradients = opt.compute_gradients(ctc_loss)
    """ for g in gradients:
        tf.summary.histogram("%s-grad" % g[1].name, g[0]) """
    step = opt.apply_gradients(gradients, global_step=global_step)
    learningrate=opt._lr
    error,predictdense = model.prediction(logits, seq_length, y)

    init = tf.global_variables_initializer()
    saver = tf.train.Saver(max_to_keep=2)
    summary = tf.summary.merge_all()
    print("Training settings:")
    for pro in dir(FLAGS):
        if not pro.startswith('_'):
            print("%s:%s" % (pro, getattr(FLAGS, pro)))
    sess = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
    if FLAGS.retrain == False:
        sess.run(init)
        print("Model inited, begin loading data. \n")
    else:
        saver.restore(
            sess, tf.train.latest_checkpoint(FLAGS.model_dir + FLAGS.model_name))
        print("Model loaded, begin loading data. \n")
    summary_writer = tf.summary.FileWriter(
        FLAGS.model_dir + FLAGS.model_name + os.path.sep + 'summary' +
        os.path.sep, sess.graph)
    train_data, vali_data = generate_train_valid_datasets()
    start = time.time()

    for i in range(FLAGS.max_steps):
        # print('train_data shape : ', len(train_data.event))
        batch_x, seq_len, batch_y = train_data.next_batch(FLAGS.batch_size)
        indxs, values, shape = batch_y
        feed_dict = {
            x: batch_x,
            seq_length: seq_len / ratio,
            y_indexs: indxs,
            y_values: values,
            y_shape: shape,
            training: True
        }
        loss_val, _ = sess.run([ctc_loss, step], feed_dict=feed_dict)
        print('learning rate: ',sess.run(learningrate))
        print('iteration:%d,loss_val:%5.3f' % (i,loss_val))
        
        # validation every 10 iterations
        if i % 10 == 0:
            global_step_val = tf.train.global_step(sess, global_step)
            valid_x, valid_len, valid_y = vali_data.next_batch(FLAGS.batch_size)
            indxs, values, shape = valid_y
            feed_dict = {
                x: valid_x,
                seq_length: valid_len / ratio,
                y_indexs: indxs,
                y_values: values,
                y_shape: shape,
                training: True
            }
            error_val = sess.run(error, feed_dict=feed_dict)
            # print(sess.run(predictdense,feed_dict=feed_dict))
            end = time.time()
            print("Step %d/%d Epoch %d, batch number %d, train_loss: %5.3f validate_edit_distance: %5.3f Elapsed Time/step: %5.3f" % (i, FLAGS.max_steps, train_data.epochs_completed,train_data.index_in_epoch, loss_val, error_val,(end - start) / (i + 1)))
            saver.save(
                sess,
                FLAGS.model_dir + FLAGS.model_name + os.path.sep + 'model.ckpt',
                global_step=global_step_val)
            summary_str = sess.run(summary, feed_dict=feed_dict)
            summary_writer.add_summary(
                summary_str, global_step=global_step_val)
            summary_writer.flush()
    global_step_val = tf.train.global_step(sess, global_step)
    print("Model %s saved." % (FLAGS.model_dir + FLAGS.model_name))
    print("Reads number %d" % (train_data.reads_n))
    saver.save(
        sess,
        FLAGS.model_dir + FLAGS.model_name + os.path.sep + 'final.ckpt',
        global_step=global_step_val)
コード例 #28
0
def main():

    if os.path.isfile(macro._LOCAL_SAVE_DATA) == 0:

        # download data and compute featuers (see "download_data.py")
        # atomic_numbers use to compute composition vector
        # labels is target properties (formation energy)
        train_labels, compositions, features, atomic_numbers = dl.get_data()

        # compute bag-of-atom vector that trains GAN (see "preprocess.py")
        boa_vectors = pre.compute_bag_of_atom_vector(compositions,
                                                     atomic_numbers)
        train_data = np.concatenate([boa_vectors, features], axis=1)

        save_data = pd.DataFrame(
            np.concatenate([train_labels, train_data], axis=1))
        save_data.to_csv(macro._LOCAL_SAVE_DATA, index=False, header=False)

    else:
        data = pd.read_csv(macro._LOCAL_SAVE_DATA,
                           delimiter=',',
                           engine="python",
                           header=None)
        data = np.array(data)
        train_labels, train_data = np.split(data, [1], axis=1)

    # normalization of training data such that min is 0 and max is 1 (see "preprocess.py")
    normalized_train_data, data_max, data_min = pre.normalize_for_train(
        train_data)
    normalized_train_labels, max_train_prop, min_train_prop = pre.normalize_for_train(
        train_labels)

    # Save normalization parameter to .csv to use generation
    save_data = pd.DataFrame(
        np.concatenate([max_train_prop, min_train_prop, data_max, data_min],
                       axis=0))
    save_data.to_csv(macro._SAVE_NORMALIZATION_PARAM,
                     index=False,
                     header=False)

    ### start initialization of training GAN ###

    # set hyperparameters
    batch_size = macro._BATCH_SIZE  # batch size
    noise_dim = macro._NOISE_DIM  # dimension of noise to input generator
    property_dim = macro._PROP_DIM  # the number of properties
    lamb = macro._LAMB  # hyperparameter for W-GAN-GP
    max_epoch = macro._MAX_EPOCH  # maximum iteration of outer loop
    max_train_only_dis = macro._MAX_EPOCH_TRAIN_DISCRIMINATOR  # maximum iteration of inner loop defined by W-GAN-GP paper (https://arxiv.org/pdf/1704.00028.pdf)
    max_loop = int(train_data.shape[0] / batch_size)

    # set model (see "model.py")
    # in this code, we apply AC-GAN based network architecture (https://arxiv.org/abs/1610.09585)
    # difference between AC-GAN is that our model is the regression, not classification
    gen = model.generator(normalized_train_data.shape[1])
    dis = model.discriminator(normalized_train_data.shape[1])

    # rf is the output layer of discriminator that discriminates real or fake
    rf = model.real_fake()

    # pred is the output layer of discriminator that predicts target property
    pred = model.prediction()

    # set optimization method
    dis_opt = Adam(lr=1.0e-4, beta_1=0.0, beta_2=0.9)
    gen_opt = Adam(lr=1.0e-4, beta_1=0.0, beta_2=0.9)

    # first set discriminator's parameters for training
    gen.trainable = False  # generator's parameter does not update
    dis.trainable = True
    rf.trainable = True
    pred.trainable = True

    # set variables when inputting real data
    real_inputs = Input(shape=normalized_train_data.shape[1:])
    dis_real_outputs = dis(real_inputs)
    real_fake_from_real = rf(dis_real_outputs)
    predictions_from_real = pred(dis_real_outputs)

    # set variables when inputting fake data
    fake_inputs = Input(shape=(noise_dim + property_dim, ))
    gen_fake_outputs = gen(fake_inputs)
    dis_fake_outputs = dis(gen_fake_outputs)
    real_fake_from_fake = rf(dis_fake_outputs)

    # set loss function for discriminator
    # in this case, we apply W-GAN-GP based loss function because of improving stability
    # W-GAN-GP (https://arxiv.org/pdf/1704.00028.pdf)
    # W-GAN-GP is unsupervised training, on the other hand, our model is supervised (conditional).
    # So, we apply wasserstein_loss to real_fake part and apply mean_squared_error to prediction part
    interpolate = model.RandomWeightedAverage()(
        [real_inputs, gen_fake_outputs])
    dis_interpolate_outputs = dis(interpolate)
    real_fake_interpolate = rf(dis_interpolate_outputs)

    # gradient penalty of W-GAN-GP
    gp_reg = partial(model.gradient_penalty,
                     interpolate=interpolate,
                     lamb=lamb)
    gp_reg.__name__ = 'gradient_penalty'

    # connect inputs and outputs of the discriminator
    # prediction part is trained by only using training dataset (i.e., predict part is not trained by generated samples)
    dis_model = Model(inputs=[real_inputs, fake_inputs],\
    outputs=[real_fake_from_real, real_fake_from_fake, real_fake_interpolate, predictions_from_real])

    # compile
    dis_model.compile(loss=[model.wasserstein_loss,model.wasserstein_loss,\
    gp_reg,'mean_squared_error'],optimizer=dis_opt)

    # second set generator's parameters for training
    gen.trainable = True  # generator's parameters only update
    dis.trainable = False
    rf.trainable = False
    pred.trainable = False

    # set variables when inputting noise and target property
    gen_inputs = Input(shape=(noise_dim + property_dim, ))
    gen_outputs = gen(gen_inputs)

    # set variables for discriminator when inputting fake data
    dis_outputs = dis(gen_outputs)
    real_fake = rf(dis_outputs)
    predictions = pred(dis_outputs)

    # connect inputs and outputs of the discriminator
    gen_model = Model(inputs=[gen_inputs], outputs=[real_fake, predictions])

    # compile
    # generator is trained by real_fake classification and prediction of target property
    gen_model.compile(loss=[model.wasserstein_loss, 'mean_squared_error'],
                      optimizer=gen_opt)

    # if you need progress bar
    progbar = Progbar(target=max_epoch)

    # set the answer to train each model
    real_label = [-1] * batch_size
    fake_label = [1] * batch_size
    dummy_label = [0] * batch_size

    #real = np.zeros((batch_size,train_data.shape[1]), dtype=np.float32)
    inputs = np.zeros((batch_size, noise_dim + property_dim), dtype=np.float32)

    # epoch
    for epoch in range(max_epoch):

        # iteration
        for loop in range(max_loop):

            # shuffle to change the trainng order and select data
            sdata, slabels, bak = pre.paired_shuffle(normalized_train_data,
                                                     normalized_train_labels)
            real = sdata[loop * batch_size:(loop + 1) * batch_size]
            properties = slabels[loop * batch_size:(loop + 1) * batch_size]

            # generator's parameters does not update
            gen.trainable = False
            dis.trainable = True
            rf.trainable = True
            pred.trainable = True

            # train discriminator
            for train_only_dis in range(max_train_only_dis):
                noise = np.random.uniform(
                    -1, 1, (batch_size, noise_dim)).astype(np.float32)
                for i in range(len(noise)):
                    inputs[i] = np.hstack((noise[i], properties[i]))
                dis_loss = dis_model.train_on_batch(
                    [real, inputs],
                    [real_label, fake_label, dummy_label, properties])

            # second train only generator
            gen.trainable = True
            dis.trainable = False
            rf.trainable = False
            pred.trainable = False
            noise = np.random.uniform(-1, 1, (batch_size, noise_dim)).astype(
                np.float32)
            for i in range(len(noise)):
                inputs[i] = np.hstack((noise[i], properties[i]))
            gen_loss = gen_model.train_on_batch([inputs],
                                                [real_label, properties])

        # if you need progress bar
        progbar.add(1,
                    values=[("dis_loss", dis_loss[0]),
                            ("gen_loss", gen_loss[0])])

    # save generated samples and models
    eval.save(normalized_train_data, gen, dis, pred, rf)

    backend.clear_session()
コード例 #29
0
def main(filename):
    print('loading data')
    # Establish database connection
    with open(
            '/data/groups/schools1/mlpolicylab_fall20_schools1/pipeline/db_info.yaml',
            'r') as f:
        db_params = yaml.safe_load(f)['db']

    engine = create_engine('postgres://:@{host}:{port}/{dbname}'.format(
        host=db_params['host'],
        port=db_params['port'],
        dbname=db_params['dbname'],
    ))
    # Load data from database to dataframe
    df = load_data(filename, engine)

    # Split dataframe into train and test data.
    splits, years_reference = train_test_split(df)

    for i, (train_df, test_df) in enumerate(splits):
        print(f'processing split {i}')

        # Explore data for each of the cohort
        explore_data(train_df)

        # Process train and test data seperately
        updated_df_train = process_data(train_df)
        updated_df_test = process_data(test_df)

        # Upload the test and train data to database for future reference and easy retrival
        updated_df_train.columns = [
            col.replace('(', '').replace(')',
                                         '').replace(' ',
                                                     '_').replace('/', '_')
            for col in updated_df_train.columns
        ]
        updated_df_test.columns = [
            col.replace('(', '').replace(')',
                                         '').replace(' ',
                                                     '_').replace('/', '_')
            for col in updated_df_test.columns
        ]

        table_name = timestamp + '_' + str(years_reference[i][1]) + '_' + str(
            years_reference[i][0])

        df_to_db(table_name, 'processed_data', updated_df_train,
                 updated_df_test, engine)

        # Retreive test and train data from database
        processed_train, processed_test = db_to_df(table_name,
                                                   'processed_data', engine)

        updated_df_train_f = processed_train.copy()
        updated_df_train_l = processed_train.copy()
        updated_df_test_f = processed_test.copy()
        updated_df_test_l = processed_test.copy()

        # Create features for test and train data
        features_train, train_student_ids = create_features(updated_df_train_f)
        features_test, test_student_ids = create_features(updated_df_test_f)

        # Create labels
        label_train = create_label(updated_df_train_l)
        label_test = create_label(updated_df_test_l)

        # Concatenating features and labels to save in the database
        train_concat = pd.concat([features_train, label_train],
                                 axis=1,
                                 sort=False)
        test_concat = pd.concat([features_test, label_test],
                                axis=1,
                                sort=False)

        # Calculating baseline rate using grade 9 gpa and base rate
        baseline_precision = baseline(test_concat, years_reference[i])
        base_rate = sum(train_concat.not_graduated) / len(train_concat)

        # Saving and reading from database
        df_to_db(table_name, 'model_data', train_concat, test_concat, engine)
        model_train, model_test = db_to_df(table_name, 'model_data', engine)

        features_train = model_train.iloc[:, :-1]
        label_train = model_train.iloc[:, -1]
        features_test = model_test.iloc[:, :-1]
        label_test = model_test.iloc[:, -1]

        # Build model
        algos = ["Logistic", "SVM", "RandomForest", "DecisionTree"]
        gs_params = {
            "Logistic":
            ParameterGrid({
                'solver': ['lbfgs', 'liblinear', 'saga'],
                'C': [0.001, 0.01, 0.1, 1, 2, 5, 10]
            }),
            "SVM":
            ParameterGrid({
                'C': [0.01, 1, 2, 5, 10],
                'kernel': ['rbf', 'sigmoid']
            }),
            "RandomForest":
            ParameterGrid({
                'n_estimators': [30, 50, 100, 500, 1000, 10000],
                'max_depth': [5, 10, 20, 50],
                'min_samples_split': [5, 10, 15],
                'max_features': ['auto', 'log2', 'sqrt']
            }),
            "DecisionTree":
            ParameterGrid({
                'criterion': ['gini', 'entropy'],
                'max_depth': [5, 10, 20, 50],
                'min_samples_split': [5, 10, 15]
            })
        }

        print('performing model grid search')
        for model_name in algos:
            params = gs_params[model_name]
            for param in params:
                model = build_model(features_train, label_train, model_name,
                                    param)

                # Perform prediction
                pred_proba_train = prediction(features_train, model)
                pred_proba_test = prediction(features_test, model)

                # Convert prediction probabilities to dataframes for further processing
                pred_train_df = pd.DataFrame(pred_proba_train,
                                             columns=['probability'])
                pred_test_df = pd.DataFrame(pred_proba_test,
                                            columns=['probability'])

                # Retreive hyperparameters for processing
                hyperparameters = ' '.join(
                    ["{}: {}".format(key, param[key]) for key in param.keys()])

                pred_train_df['model'], pred_train_df[
                    'params'] = model_name, hyperparameters
                pred_test_df['model'], pred_test_df[
                    'params'] = model_name, hyperparameters

                # Get the prediction scores for test and train data
                predictions_train = pd.concat(
                    [train_student_ids, pred_train_df], axis=1, sort=False)
                predictions_test = pd.concat([test_student_ids, pred_test_df],
                                             axis=1,
                                             sort=False)

                # Calculate the bias metrics
                TPR_gender, FDR_gender = bias_metrics(predictions_test,
                                                      processed_test, 'gender')
                TPR_disadvantagement, FDR_disadvantagement = bias_metrics(
                    predictions_test, processed_test, 'disadvantagement')

                # Load the prediction results to database for creating visualizations
                df_to_db(table_name, 'predictions', predictions_train,
                         predictions_test, engine)

                # Evaluate model
                metric = evaluate_model(features_test,
                                        label_test,
                                        model,
                                        model_name,
                                        baseline_precision,
                                        hyperparameters,
                                        columns=model_train.columns[:-1])

                # saving results
                df_summary = pd.DataFrame({
                    'test_year':
                    years_reference[i][1],
                    'train_since':
                    years_reference[i][0],
                    'algorithm':
                    model_name,
                    'hyperparameters':
                    hyperparameters,
                    'baserate':
                    base_rate,
                    'baseline': [baseline_precision],
                    'precision':
                    metric,
                    'TPR_gender':
                    TPR_gender,
                    'FDR_gender':
                    FDR_gender,
                    'TPR_disadvantagement':
                    TPR_disadvantagement,
                    'FDR_disadvantagement':
                    FDR_disadvantagement
                })
                df_summary.to_sql(name=timestamp,
                                  schema='performance_metrics',
                                  con=engine,
                                  if_exists='append',
                                  index=False)
コード例 #30
0
batch_size = 16
print("Batch Size", batch_size)
# converting the data into sequences of batch_size
# Each sequence/window is normalised
train_x, train_y, norm_y_train = seq_and_norm(train, batch_size)
test_x, test_y, norm_y_test = seq_and_norm(test, batch_size)

train_loss, val_loss, model = LSTM_train(train_x, train_y, batch_size)
plt.plot(train_loss, label="Training Loss")
plt.plot(val_loss, label='Validation Loss')
plt.legend()
plt.show()

# predictions on train and test + inverse transformation
train_pred = prediction(train_x, model, norm_y_train)
test_pred = prediction(test_x, model, norm_y_test)

test_rmspe = eval_metrics(norm_y_test[:, 0], test_pred)
train_rmspe = eval_metrics(norm_y_train[:, 0], train_pred)

print("Test Set RMSPE: {rmspe}%".format(rmspe=test_rmspe))
print("Train Set RMSPE: {rmspe}%".format(rmspe=train_rmspe))

# plot trading signal prediction and actual
generate_plot(index_train, [train_pred, norm_y_train[:, 0]],
              ["Predicted Train", "Actual Train"],
              "Trading Signal RMSPE {}%".format(train_rmspe), "Trading Signal",
              "Time Period")
generate_plot(index_train, [test_pred, norm_y_test[:, 0]],
              ["Predicted Test", "Actual Test"],