# ml-class / ex2_1.py

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75``` ```# coding=utf8 import matplotlib.pylab as plt import numpy as np from sklearn import linear_model def parse_line(line): return map(float, line.split(',')) def load(): return np.loadtxt('ex2/ex2data1.txt', delimiter=',') def load_features(): data = load() xys = data[:,0:2] ones = np.ones((len(xys), 1)) return np.append(ones, xys, 1), data[:,2] def sigmoid(z): return 1. / (1 + np.e**(-z)) usig = np.vectorize(sigmoid) def diff(theta, xi, yi): ht = xi * theta.T return -np.log(ht) if yi else np.log(1-ht) def J(theta, xs, ys): d = lambda xi, yi: diff(theta, xi, yi) m = float(len(xs)) return sum(d(xi, yi) for (xi, yi) in zip(xs, ys)) / m def learn_p(): xs, ys = load_features() clf = linear_model.LogisticRegression() clf.fit(xs, ys) count = 0 for x, y in zip(xs, ys): v = clf.predict(x) if v >= 0.5 and y: count += 1 elif v < 0.5 and not y: count += 1 return float(count) / len(xs) * 100 def plot(data, xlabel='Exam 1 score', ylabel='Exam 2 score', legend=True): passed = data[data[:,2]==1] failed = data[data[:,2]==0] fig = plt.figure() ax = fig.add_subplot(111) ax.scatter(passed[:,0], passed[:,1], color='black', marker='+', label='Admitted') ax.scatter(failed[:,0], failed[:,1], color='red', marker='o', label='Not admitted') ax.set_xlabel(xlabel) ax.set_ylabel(ylabel) if legend: plt.legend() plt.show() if __name__ == '__main__': print(learn_p()) ```