<repo_name>prezaie/Skill-Matching-Python-Flask<file_sep>/preproces.py
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split

def preprocess(df, X_te):
    """Standardize data

    Args:
        df (str, or DataFrame): input data
        test_size (float) : size of test set 
    Returns:
        X_tr (Dataframe): Train DataFrame
        X_te (Dataframe): Test DataFrame
        X_tr_sc (Numpy array): Standardized Train DataFrame
        X_te_sc (Numpy array): Standardized Test DataFrame

    """   
    df = df.astype(float) 
    X_te = X_te.astype(float)
    sc = StandardScaler()
    X_tr = df.drop(columns='project_employee_id')
    X_te = X_te.drop(columns='project_employee_id')
    X_tr_sc = sc.fit_transform(X_tr)
    X_te_sc = sc.transform(X_te)
      
    return X_tr, X_te, X_tr_sc, X_te_sc<file_sep>/read.py

import pandas as pd
from sqlalchemy.engine import create_engine

def data_reader():
    """Reads data from SQL database and transform data to a DataFrame

    Args:
        none
    Returns:
        df (Dataframe): Dataframe contains employee skills and employee id

    """
    engine = create_engine('postgresql://parisarezaie@localhost/kapa') 
    df = pd.read_sql('''SELECT 
                                   name,
                                   level,
                                   project_employee_id
                            FROM criteria, employee_criteria 
                            WHERE (criteria.id = employee_criteria.criteria_id) AND (criteria.type = 'SKILL')
                            ''', engine)

    df = df.pivot(index='project_employee_id', columns='name', values='level')
    df.fillna(0,inplace=True)
    df = df.reset_index() 
    return df                    <file_sep>/model.py
from sklearn.neighbors import NearestNeighbors

def knn(X_tr_sc, n_neighbors, radius):
    
    """ Unsupervised learner for implementing neighbor searches

    Args:
        X_tr_sc (DataFrame): Standardized Train DataFrame
        n_neighbors (int): Number of neighbors
        radius (float): Range of parameter space
    Returns:
        model (Class): DataModel that fitted to the training data
     
    """    

    neigh = NearestNeighbors(n_neighbors, radius,metric='euclidean')
    model = neigh.fit(X_tr_sc) 
    
    return model<file_sep>/prediction.py
from model import knn

def prediction(model,X_tr, X_te, X_tr_sc, X_te_sc):
    """ Prediction closest employees to our query position
    Args:
        model (Class): DataModel created from our training data
        X_tr (DataFrame): Train Dataframe
        X_te (DataFrame): Test Dataframe
        X_tr_sc (DataFrame): Standardized Train DataFrame
        X_te_sc (DataFrame): Standardized Test DataFrame    
    Returns:
        X_tr.loc[m1,m2] (DataFrame): Query results
    """
    
    poi = X_te_sc
    closest_points = model.kneighbors(poi.reshape(1, -1), 3, return_distance=False)
    # for i,close_id in enumerate(closest_points[0]):
    #     idx = X_tr.index[close_id]
    #     m1 = (X_tr.index == idx)
    #     m2 = (X_tr[m1] != 0).all()
    #return X_tr.loc[m1,m2]  
    
    return X_tr.loc[closest_points[0],:]
    <file_sep>/main.py
from read import data_reader
from preproces import preprocess
from model import knn
import pandas as pd
import numpy as np
from prediction import prediction
from flask import Flask, request, jsonify
from flask_cors import CORS, cross_origin
import json

"""Connects to Flask to get the search query. Using KNN to return most similar results for skill match
    Args:
        json data
    Returns:
        df (Dataframe): Dataframe contains best matches employee IDs and skills
"""

app = Flask(__name__)
CORS(app)

@app.route('/api/predict', methods = ['POST'])
def predict():
        skills= request.json['skill']
        headers = [str(i) for i in skills.split(',')]
        payload= request.json['data']
        values = [float(i) for i in payload.split(',')]
        df=data_reader()
        col_list=list(df.columns)
        df_new=pd.DataFrame(columns=col_list)
        X=pd.DataFrame([values],columns=headers, 
                                dtype=float)                    
        X_te=pd.concat([df_new,X], axis=0,sort=False)
        X_te.fillna(0,inplace=True) 
                           
        X_tr, X_te, X_tr_sc, X_te_sc = preprocess(df,X_te)
        my_model = knn(X_tr_sc, 5, 0.4)
        prediction_results=prediction(my_model,X_tr, X_te, X_tr_sc, X_te_sc)
        prediction_results=prediction_results.loc[:, (prediction_results != 0).any(axis=0)]
        return json.dumps(json.loads(prediction_results.to_json(orient='index')))
if __name__ == '__main__':
    app.run(port=6000, debug=True)
