diff options
-rwxr-xr-x | bin/model.py | 68 | ||||
-rw-r--r-- | flake.nix | 7 |
2 files changed, 74 insertions, 1 deletions
diff --git a/bin/model.py b/bin/model.py new file mode 100755 index 0000000..c44e0a6 --- /dev/null +++ b/bin/model.py @@ -0,0 +1,68 @@ +#!/usr/bin/env python3 + +from typing import List, Tuple +from sklearn import linear_model +import tqdm +import scipy +import sqlite3 +import argparse +import numpy as np + +class matrix_builds_inputpnames: + independent_variables: scipy.sparse.coo_array + dependent_variable: np.ndarray + + def __init__(self, db: str) -> None: + row: List[int] = [] + col: List[int] = [] + data: List[int] = [] + + con = sqlite3.connect(db) + cur = con.cursor() + + builds = self.table_fetch(cur, ''' + SELECT ROWID, drv_id, duration FROM builds_cleaned + ''') + self.dependent_variable = np.array([ [ row[1] ] for row in tqdm.tqdm(builds) ]) + + for build in tqdm.tqdm(builds): + inputpnames = self.table_fetch(cur, ''' + SELECT drv_id, pname_id FROM input_pnames + WHERE input_pnames.drv_id = ? + ''', (build[1],) ) + + for inputpname in inputpnames: + row.append(build[0] - 1) + col.append(inputpname[1]) + data.append(1) + + np_row = np.array(row) + np_col = np.array(col) + np_data = np.array(data) + self.independent_variables = scipy.sparse.coo_array((np_data, (np_row, np_col))) + + con.commit() + con.close() + + @staticmethod + def table_fetch(cur: sqlite3.Cursor, query: str, args: Tuple = ()) -> List: + query_exec = cur.execute(query, args) + return query_exec.fetchall() + +def args_get(): + parser = argparse.ArgumentParser() + parser.add_argument('-d', '--db', required=True, help="path to sqlite database containing input_pnames and builds") + parser.add_argument('-t', '--test', type=int, required=True, help="row to test against") + return parser.parse_args() + +if __name__ == '__main__': + args = args_get() + matrix = matrix_builds_inputpnames(args.db) + + regr = linear_model.LinearRegression() + regr.fit(matrix.independent_variables, matrix.dependent_variable) + pred = regr.predict([matrix.independent_variables.toarray()[args.test]]) + + print(f'prediction : {pred[0][0]}') + print(f'og duration : {matrix.dependent_variable[args.test][0]}') + @@ -49,8 +49,13 @@ linuxKernel.packages.linux_6_6.perf hyperfine nix-eval-jobs - python3 + pyright + (python3.withPackages (p: [ + p.scipy + p.tqdm + p.scikit-learn + ])) ]; shellHook = '' |