Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- from sklearn.svm import SVC
- DATASET = '../rsc/q2_dataset.txt'
- # Read dataset and return 3 lists corresponding to classification, x and y
- def train_predict(constant, dataset):
- fid = open(dataset, 'r')
- x = []
- y = []
- for line in fid:
- x.append([float(line.split()[1][2:]), float(line.split()[2][2:])])
- y.append(float(line.split()[0]))
- fid.close()
- clf = SVC(C=constant, kernel='linear')
- clf.fit(np.array(x), np.array(y))
- # Predicting Random values.
- val = clf.predict(np.array([[8, 8], [2, 8], [5, 6], [5, 3], [6, 8]]))
- alphas = np.abs(clf.dual_coef_)
- print(alphas) # Alpha
- print(clf.coef_) # Weights
- print(val) # Predicted classifications.
- def main():
- train_predict(1, DATASET)
- print('-----')
- train_predict(3, DATASET)
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement