ml-finance-python

python scripts for finance machine learning

git clone https://9o.is/git/ml-finance-python.git

logistic_regression.py

(1062B)


      1 from __future__ import print_function
      2 from sklearn import datasets
      3 import numpy as np
      4 import matplotlib.pyplot as plt
      5 
      6 # Import helper functions
      7 from mlfromscratch.utils import make_diagonal, normalize, train_test_split, accuracy_score
      8 from mlfromscratch.deep_learning.activation_functions import Sigmoid
      9 from mlfromscratch.utils import Plot
     10 from mlfromscratch.supervised_learning import LogisticRegression
     11 
     12 def main():
     13     # Load dataset
     14     data = datasets.load_iris()
     15     X = normalize(data.data[data.target != 0])
     16     y = data.target[data.target != 0]
     17     y[y == 1] = 0
     18     y[y == 2] = 1
     19 
     20     X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33, seed=1)
     21 
     22     clf = LogisticRegression(gradient_descent=True)
     23     clf.fit(X_train, y_train)
     24     y_pred = clf.predict(X_test)
     25 
     26     accuracy = accuracy_score(y_test, y_pred)
     27     print ("Accuracy:", accuracy)
     28 
     29     # Reduce dimension to two using PCA and plot the results
     30     Plot().plot_in_2d(X_test, y_pred, title="Logistic Regression", accuracy=accuracy)
     31 
     32 if __name__ == "__main__":
     33     main()