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
|
import numpy as np
def perceptron(X, y, eta=1, max_step=100000): """ X : feature array contain intercept y: lables 0 or 1 eta: step length max_step: maximum iterations
"""
n, d = X.shape w = np.zeros(d) step = 0 row = 0 while step < max_step: if np.sign(X[row, :].dot(w) * (y[row]-0.5)) <=0: step += 1 w += eta * y[row] * X[row, :] if row == n: row = 0 if np.sum((X.dot(w) == y)) == n: break return w
import matplotlib.pyplot as plt import pandas as pd from sklearn import tree import numpy as np
data = pd.read_csv('classfication-regression.csv') data_coloums_name = data.columns.tolist()
def Tree_test():
data_x = data[['H-In', 'coord', 'O_v', 'bi-HCOO', 'H_2COO', 'CH_2O']] data_y = data['ts2-c'] clf = tree.DecisionTreeClassifier(max_depth=1) clf = clf.fit(data_x, data_y)
import graphviz dot_data = tree.export_graphviz(clf, out_file=None, feature_names=['H-In', 'coord', 'O_v', 'bi-HCOO', 'H_2COO', 'CH_2O'], class_names=['TS3', 'TS2'], filled=True, rounded=True, special_characters=True) graph = graphviz.Source(dot_data) graph.render("ts2-c") graph.view()
|