<commit_before>#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <sqlite3.h>

#define NUMBER_OF_PASSES 10

typedef struct Classifier {
  int count;
  int feature_count;
  double features[];
} Classifier;

Classifier *c;

int pass = 1;

double sigmoid(double x) {
  return 1.0 / (1.0 + exp(-x));
}

double classify_instance(int count, sqlite3_value** features) {
  double logit = 0.0,
         feature_value;

  for(int i = 0; i < count; i++) {
    feature_value = sqlite3_value_double(features[i]);
    logit += c->features[i] * feature_value;
  }

  return sigmoid(logit);
}

void trainClassifierStep(sqlite3_context* context, int argc, sqlite3_value** argv){
  int label;
  double result, feature_value;

  argc--;

  if(c == NULL) {
    c = malloc(sizeof(struct Classifier) + argc*sizeof(double));
    c->feature_count = argc;
  }

  c->count++;

  label  = sqlite3_value_int64(argv[argc]);
  result = classify_instance(argc, argv);

  for(int i = 0; i < argc; i++) {
    feature_value = sqlite3_value_double(argv[i]);
    c->features[i] = c->features[i] + (0.01 * (label - result) * feature_value);
  }
}

void trainClassifierFinalise(sqlite3_context *context){
  sqlite3 *db = sqlite3_context_db_handle(context);
  sqlite3_stmt *stmt = sqlite3_next_stmt(db, NULL);

  if(pass < NUMBER_OF_PASSES) {
    printf("Pass %d complete\n",pass);
    pass++;
    sqlite3_reset(stmt);
    sqlite3_step(stmt);
  } else {
    for(int i = 0; i < c->feature_count; i++) {
      printf("(%d) %f\n",i,c->features[i]);
    }
  }
}

void classifyData(sqlite3_context* context, int argc, sqlite3_value** argv){
  sqlite3_result_double(context, classify_instance(argc, argv));
}<commit_msg>factor in bias term for regression<commit_after>#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <sqlite3.h>

#define NUMBER_OF_PASSES 10

typedef struct Classifier {
  int count;
  int feature_count;
  double features[];
} Classifier;

Classifier *c;

int pass = 1;

double sigmoid(double x) {
  return 1.0 / (1.0 + exp(-x));
}

double classify_instance(int count, sqlite3_value** features) {
  double logit = c->features[0],
         feature_value;

  for(int i = 0; i < count; i++) {
    feature_value = sqlite3_value_double(features[i]);
    logit += c->features[i+1] * feature_value;
  }

  return sigmoid(logit);
}

void trainClassifierStep(sqlite3_context* context, int argc, sqlite3_value** argv){
  int label;
  double result, feature_value, alpha;

  alpha = 1/pass;

  argc--;

  if(c == NULL) {
    c = malloc(sizeof(struct Classifier) + (argc+1)*sizeof(double));
    c->feature_count = argc;
  }

  c->count++;

  label  = sqlite3_value_int64(argv[argc]);
  result = classify_instance(argc, argv);

  c->features[0] = c->features[0] + alpha*(label - result); // Bias term

  for(int i = 0; i < argc; i++) {
    feature_value = sqlite3_value_double(argv[i]);
    c->features[i+1] = c->features[i+1] + (alpha * (label - result) * feature_value);
  }
}

void trainClassifierFinalise(sqlite3_context *context){
  sqlite3 *db = sqlite3_context_db_handle(context);
  sqlite3_stmt *stmt = sqlite3_next_stmt(db, NULL);

  if(pass < NUMBER_OF_PASSES) {
    printf("Pass %d complete\n",pass);
    pass++;
    sqlite3_reset(stmt);
    sqlite3_step(stmt);
  } else {
    printf("(Bias) %f\n", c->features[0]);
    for(int i = 0; i < c->feature_count; i++) {
      printf("(%d) %f\n", i+1, c->features[i+1]);
    }
  }
}

void classifyData(sqlite3_context* context, int argc, sqlite3_value** argv){
  sqlite3_result_double(context, classify_instance(argc, argv));
}
{'fraction_non_alphanumeric': 0.15481522956326987, 'fraction_numerical': 0.020716685330347144, 'mean_word_length': 4.0609065155807365, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1588940', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1323, 'n_words': 285}