This is the Logistic Regression demo implement from Scratcth 😄 .
To install packages, install from requirements.txt
file via this command:
pip install -r requirements.txt
You can get Logistic Regression class from the LogisticRegresion.py
file, for example:
from LogisticRegression import LogisticRegression
X_train = ...
y_train = ...
X_val = ...
y_val = ...
num_iters = 1000
model = LogisticRegression(lr = 1e-2, lambda_val = 1e-3)
model.train(X_train, y_train, X_test, y_test, num_iters)
acc, precision, recall, f1_score = model.evaluate(X_test, y_test)
print(acc, precision, recall, f1_score)
All of the examples you can find in the train.py
file.
You can get the sigmoid(z)
from the file above.
Data sample can be found at the training_data.txt
file
Model can be saved at the model.json
file, which includes the weight of the model, you can use load_weight attribute from the model to load the model json file.
from LogisticRegression import LogisticRegression
model = LogisticRegression(lr = 1e-2, lambda_val = 1e-3)
model.load_weights("model.json")
model.evaluate()
For prediction, you can use predict()
attribute for predict the data (note: the data should be as same as the input format for the training).
from LogisticRegression import LogisticRegression
from sklearn.metrics import accuracy_score
model = LogisticRegression(lr = 1e-2, lambda_val = 1e-3)
model.load_weights("model.json")
y_pred = model.predict(X)
acc = accuracy_score(y_true, y_pred)
print(acc)
The config.json
file includes the hyperparameters for training, you can customize by yourself in the file
Let me know if are there any issues 😉