Skip to content

Commit

Permalink
setup
Browse files Browse the repository at this point in the history
  • Loading branch information
agrimagsrl committed Jul 9, 2020
0 parents commit e313975
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.idea/
venv
publish
dist
sefr/__pycache__
17 changes: 17 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
MIT License
Copyright (c) 2018 YOUR NAME
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# file GENERATED by distutils, do NOT edit
setup.py
sefr/__init__.py
sefr/sefr.py
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SEFR: A Fast Linear-Time Classifier for Ultra-Low Power Devices

A Python package for the paper [SEFR: A Fast Linear-Time Classifier for Ultra-Low Power Devices](https://arxiv.org/abs/2006.04620)
by Hamidreza Keshavarz, Mohammad Saniee Abadeh, Reza Rawassizadeh

Copied from [original implementation](https://github.com/sefr-classifier/sefr)
5 changes: 5 additions & 0 deletions how_to_use.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from sefr import SEFR


if __name__ == '__main__':
print(SEFR())
1 change: 1 addition & 0 deletions sefr/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from sefr.sefr import SEFR
78 changes: 78 additions & 0 deletions sefr/sefr.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import numpy as np


class SEFR:
def __init__(self):
self.weights = []
self.bias = 0

def fit(self, train_predictors, train_target):
"""
This is used for training the classifier on data.
Parameters
----------
train_predictors : float, either list or numpy array
are the main data in DataFrame
train_target : integer, numpy array
labels, should consist of 0s and 1s
"""
X = train_predictors
if isinstance(train_predictors, list):
X = np.array(train_predictors, dtype="float32")

y = train_target
if isinstance(train_target, list):
y = np.array(train_target, dtype="int32")

# pos_labels are those records where the label is positive
# neg_labels are those records where the label is negative
pos_labels = np.sign(y) == 1
neg_labels = np.invert(pos_labels)

# pos_indices are the data where the labels are positive
# neg_indices are the data where the labels are negative
pos_indices = X[pos_labels, :]
neg_indices = X[neg_labels, :]

# avg_pos is the average value of each feature where the label is positive
# avg_neg is the average value of each feature where the label is negative
avg_pos = np.mean(pos_indices, axis=0) # Eq. 3
avg_neg = np.mean(neg_indices, axis=0) # Eq. 4

# weights are calculated based on Eq. 3 and Eq. 4

self.weights = (avg_pos - avg_neg) / (avg_pos + avg_neg) # Eq. 5


# For each record, a score is calculated. If the record is positive/negative, the score will be added to posscore/negscore
sum_scores = np.dot(X, self.weights) # Eq. 6

pos_label_count = np.count_nonzero(y)
neg_label_count = y.shape[0] - pos_label_count

# pos_score_avg and neg_score_avg are average values of records scores for positive and negative classes
pos_score_avg = np.mean(sum_scores[y == 1]) # Eq. 7
neg_score_avg = np.mean(sum_scores[y == 0]) # Eq. 8

# bias is calculated using a weighted average

self.bias = (neg_label_count * pos_score_avg + pos_label_count * neg_score_avg) / (neg_label_count + pos_label_count) # Eq. 9

def predict(self, test_predictors):
"""
This is for prediction. When the model is trained, it can be applied on the test data.
Parameters
----------
test_predictors: either list or ndarray, two dimensional
the data without labels in
Returns
----------
predictions in numpy array
"""
X = test_predictors
if isinstance(test_predictors, list):
X = np.array(test_predictors, dtype="float32")

temp = np.dot(X, self.weights)
preds = np.where(temp <= self.bias, 0 , 1)
return preds
32 changes: 32 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from distutils.core import setup

setup(
name = 'sefr',
packages = ['sefr'],
version = '1.0.1',
license='MIT',
description = 'A Fast Linear-Time Classifier for Ultra-Low Power Devices',
author = 'Simone Salerno',
author_email = 'eloquentarduino@gmail.com',
url = 'https://github.com/eloquentarduino/sefr',
download_url = 'https://github.com/eloquentarduino/sefr/archive/v_101.tar.gz',
keywords = [
'ML',
'microcontrollers',
'machine learning'
],
install_requires=[
'numpy',
],
package_data= {},
classifiers=[
'Development Status :: 5 - Production/Stable',
'Intended Audience :: Developers',
'Topic :: Software Development',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],
)

0 comments on commit e313975

Please sign in to comment.