Color spiral in sklearn

Description

https://we.tl/t-fgb3KzL1PG

Don't use plagiarized sources. Get Your Custom Assignment on
Color spiral in sklearn
From as Little as $13/Page

Unformatted Attachment Preview

INFO371 Lab: Neural Networks
February 19, 2024
Introduction
This lab is about the coloring a spiral, we have done it before, just not with neural networks. The task
is to create models that work well with patterns of color points, one that can with high accuracy predict
the pattern. Your task is to design neural network models that gives you good validation accuracy but
do not contain many parameters.
1
Color spiral
1. Load dataset “spiral-1000.csv”. Plot the points–x versus y, colored according to color
2. How many different colors is there in the spiral?
3. Your main task will be to predict color based on x and y. Create the corresponding design matrix
and split it into training and validation chunks.
4. Fit a neural network (multi-layer perceptron) to data, and compute the validation accuracy.
Feel free to do CV instead, but that may be a bit slow.
5. Create a decision-boundary plot, along the lines we have done multiple times. As a refresher, here
is one option you may use:
def dbPlot(m, X, y, nGrid=100):
“””
m: fitted model (perceptron or any other)
X: design matrix, used to plot dots
y: outcome vector, used to color dots
“””
## Predict: take this code as black box
range1 = np.linspace(X[:,0].min(), X[:,0].max(), nGrid)
range2 = np.linspace(X[:,1].min(), X[:,1].max(), nGrid)
(xx1, xx2) = np.meshgrid(range1, range2)
Xgrid = np.column_stack((xx1.ravel(), xx2.ravel()))
yhat = m.predict(Xgrid)
plt.figure(figsize=(10,10))
## plot the predicted values on grid as an image
plt.imshow(yhat.reshape((nGrid,nGrid)),
alpha=0.3, extent=[xx1.min(), xx1.max(), xx2.min(), xx2.max()],
origin=’lower’, aspect=’auto’)
## add the actual values
1
plt.scatter(X[:,0], X[:,1], c=y, s=20, alpha=0.5, edgecolor=”k”)
plt.xlabel(“X1”)
plt.ylabel(“X2”)
_ = plt.show()
2
Improve your model
A major problem with neural networks is that they tend to contain too many parameters. You should
try to fiddle with your model to get one that has few parameters but high validation accuracy. How good
can you get?
You can get the parameters out through attributes m.coefs_ (biases) and m.intercepts_ (weights).
Both will give you a list of arrays, you have to figure out how many values are there.
1. Extract both biases and coefficients. These are lists, how many components do these lists contain?
Why?
Note: I am asking about list components, not the number of individual parameters.
2. Find the number of parameters in your model. You may need to sum the counts layer-by-layer.
Hint: array.size gives the number of elements in a numpy array.
3. Now try to tweak your model in a way that will give you a high validation accuracy, but few
parameters.
4. Finally, sketch the model you consider the best on paper using the neural networks structure like
what we did in class. Include the photo in your answer, or upload separately on canvas!
How many input and output nodes does the network have?
2

Purchase answer to see full
attachment