-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSAMPLE_EXAM_TEMPLATE.py
More file actions
76 lines (58 loc) · 1.96 KB
/
SAMPLE_EXAM_TEMPLATE.py
File metadata and controls
76 lines (58 loc) · 1.96 KB
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
76
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
## Please rename this file to your student ID.py. e.g. z1234567.py
## STUDENT ID: FILL IN YOUR ID
## STUDENT NAME: FILL IN YOUR NAME
## Question 6
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import make_blobs
np.random.seed(2)
n_points = 15
X, y = make_blobs(n_points, 2, centers=[(0,0), (-1,1)])
y[y==0] = -1 # use -1 for negative class instead of 0
plt.scatter(*X[y==1].T, marker="+", s=100, color="red")
plt.scatter(*X[y==-1].T, marker="o", s=100, color="blue")
#plt.savefig("boosting_data.png")
plt.show()
def plotter(classifier, X, y, title, ax=None):
# plot decision boundary for given classifier
plot_step = 0.02
x_min, x_max = X[:, 0].min() - 1, X[:,0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:,1].max() + 1
xx, yy = np.meshgrid(np.arange(x_min, x_max, plot_step),
np.arange(y_min, y_max, plot_step))
Z = classifier.predict(np.c_[xx.ravel(),yy.ravel()])
Z = Z.reshape(xx.shape)
if ax:
ax.contourf(xx, yy, Z, cmap = plt.cm.Paired)
ax.scatter(X[:, 0], X[:, 1], c = y)
ax.set_title(title)
else:
plt.contourf(xx, yy, Z, cmap = plt.cm.Paired)
plt.scatter(X[:, 0], X[:, 1], c = y)
plt.title(title)
## Question 6 part a
# your code here
## Question 6 part b
def weighted_error(w, y, yhat):
# your code here
T = 15
w = np.zeros((T, n_points))
w[0] = np.ones(n_points)/n_points
# storage for boosted model
alphas = np.zeros(T-1); component_models = []
## Question 6 part d
class boosted_model:
def __init__(self, T):
self.alphas = # your code here
# your code here
def predict(self, x):
# your code here
# your code here