ml-finance-python

python scripts for finance machine learning

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

support_vector_machine.py

(969B)


      1 from __future__ import division, print_function
      2 import numpy as np
      3 from sklearn import datasets
      4 
      5 # Import helper functions
      6 from mlfromscratch.utils import train_test_split, normalize, accuracy_score, Plot
      7 from mlfromscratch.utils.kernels import *
      8 from mlfromscratch.supervised_learning import SupportVectorMachine
      9 
     10 def main():
     11     data = datasets.load_iris()
     12     X = normalize(data.data[data.target != 0])
     13     y = data.target[data.target != 0]
     14     y[y == 1] = -1
     15     y[y == 2] = 1
     16     X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.33)
     17 
     18     clf = SupportVectorMachine(kernel=polynomial_kernel, power=4, coef=1)
     19     clf.fit(X_train, y_train)
     20     y_pred = clf.predict(X_test)
     21 
     22     accuracy = accuracy_score(y_test, y_pred)
     23 
     24     print ("Accuracy:", accuracy)
     25 
     26     # Reduce dimension to two using PCA and plot the results
     27     Plot().plot_in_2d(X_test, y_pred, title="Support Vector Machine", accuracy=accuracy)
     28 
     29 
     30 if __name__ == "__main__":
     31     main()