Miki Tebeka avatar Miki Tebeka committed 3885bcb

ex6

Comments (0)

Files changed (1)

+#!/usr/bin/env python
+
+from scipy.io import loadmat
+import matplotlib.pyplot as plt
+import numpy as np
+from sklearn import svm
+
+def solve(datafile, clf=None):
+    raw = loadmat(datafile)
+    data = np.append(raw['X'], raw['y'], 1)
+
+    pos = data[data[:,2]==1]
+    neg = data[data[:,2]==0]
+
+    clf = clf or svm.SVC()
+    clf.fit(raw['X'], raw['y'].ravel()>0)
+
+    xs = np.linspace(data[:,0].min(), data[:,0].max(), 100)
+    ys = np.linspace(data[:,1].min(), data[:,1].max(), 100)
+    z = np.zeros(shape=(len(xs), len(ys)))
+    for x, xv in enumerate(xs):
+        for y, yv in enumerate(ys):
+            z[x, y] = clf.predict([xv, yv])[0]
+
+    fig = plt.figure()
+    ax = fig.add_subplot(111)
+    ax.contour(xs, ys, z)
+    ax.scatter(pos[:,0], pos[:,1], marker='+', color='black')
+    ax.scatter(neg[:,0], neg[:,1], marker='o', facecolor='yellow')
+
+    fig.show()
+
+
+def main(argv=None):
+    import sys
+    from argparse import ArgumentParser
+
+    argv = argv or sys.argv
+
+    parser = ArgumentParser(description='')
+    parser.add_argument('datafile')
+    args = parser.parse_args(argv[1:])
+
+    solve(args.datafile)
+    raw_input('Hit Enter to Quit')
+
+if __name__ == '__main__':
+    main()
+
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.