0% found this document useful (0 votes)
5 views5 pages

Complex Engineering Problems

The document describes three classification techniques using Support Vector Classification (SVC) and Linear Discriminant Analysis (LDA). It includes examples of binary classification with a non-linear SVC on XOR data, plotting decision functions with weighted datasets, and comparing normal LDA with shrinkage LDA. Each section provides code snippets and visualizations to illustrate the decision boundaries and classification accuracy.

Uploaded by

Asha Judi V
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views5 pages

Complex Engineering Problems

The document describes three classification techniques using Support Vector Classification (SVC) and Linear Discriminant Analysis (LDA). It includes examples of binary classification with a non-linear SVC on XOR data, plotting decision functions with weighted datasets, and comparing normal LDA with shrinkage LDA. Each section provides code snippets and visualizations to illustrate the decision boundaries and classification accuracy.

Uploaded by

Asha Judi V
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

1. Perform binary classification using non-linear SVC with RBF kernel.

The
target to predict is a XOR of the inputs.

The color map illustrates the decision function learned by the SVC.

print(__doc__)

import numpy as np
import [Link] as plt
from sklearn import svm
xx, yy = [Link]([Link](-3, 3, 500),
[Link](-3, 3, 500))
[Link](0)
X = [Link](300, 2)
Y = np.logical_xor(X[:, 0] > 0, X[:, 1] > 0)

# fit the model


clf = [Link](gamma='auto')
[Link](X, Y)

# plot the decision function for each datapoint on the grid


Z = clf.decision_function(np.c_[[Link](), [Link]()])
Z = [Link]([Link])

[Link](Z, interpolation='nearest',
extent=([Link](), [Link](), [Link](), [Link]()), aspect='auto',
origin='lower', cmap=[Link].PuOr_r)
contours = [Link](xx, yy, Z, levels=[0], linewidths=2,
linestyles='dashed')
[Link](X[:, 0], X[:, 1], s=30, c=Y, cmap=[Link],
edgecolors='k')
[Link](())
[Link](())
[Link]([-3, 3, -3, 3])
[Link]()

2. Plot decision function of a weighted dataset, where the size of points is


proportional to its weight.

The sample weighting rescales the C parameter, which means that the
classifier puts more emphasis on getting these points right. The effect might
often be subtle. To emphasize the effect here, we particularly weight
outliers, making the deformation of the decision boundary very visible.

import numpy as np
import [Link] as plt
from sklearn import svm

def plot_decision_function(classifier, sample_weight, axis, title):


# plot the decision function
xx, yy = [Link]([Link](-4, 5, 500), [Link](-4, 5,
500))

Z = classifier.decision_function(np.c_[[Link](), [Link]()])
Z = [Link]([Link])

# plot the line, the points, and the nearest vectors to the plane
[Link](xx, yy, Z, alpha=0.75, cmap=[Link])
[Link](X[:, 0], X[:, 1], c=y, s=100 * sample_weight, alpha=0.9,
cmap=[Link], edgecolors='black')

[Link]('off')
axis.set_title(title)

# we create 20 points
[Link](0)
X = np.r_[[Link](10, 2) + [1, 1], [Link](10, 2)]
y = [1] * 10 + [-1] * 10
sample_weight_last_ten = abs([Link](len(X)))
sample_weight_constant = [Link](len(X))
# and bigger weights to some outliers
sample_weight_last_ten[15:] *= 5
sample_weight_last_ten[9] *= 15

# for reference, first fit without sample weights

# fit the model


clf_weights = [Link](gamma=1)
clf_weights.fit(X, y, sample_weight=sample_weight_last_ten)

clf_no_weights = [Link](gamma=1)
clf_no_weights.fit(X, y)

fig, axes = [Link](1, 2, figsize=(14, 6))


plot_decision_function(clf_no_weights, sample_weight_constant, axes[0],
"Constant weights")
plot_decision_function(clf_weights, sample_weight_last_ten, axes[1],
"Modified weights")

[Link]()

3. Employ Normal and Shrinkage Linear Discriminant Analysis for Classification.


import numpy as np
import [Link] as plt

from [Link] import make_blobs


from sklearn.discriminant_analysis import LinearDiscriminantAnalysis

n_train = 20 # samples for training


n_test = 200 # samples for testing
n_averages = 50 # how often to repeat classification
n_features_max = 75 # maximum number of features
step = 4 # step size for the calculation

def generate_data(n_samples, n_features):


"""Generate random blob-ish data with noisy features.

This returns an array of input data with shape `(n_samples,


n_features)`
and an array of `n_samples` target labels.

Only one feature contains discriminative information, the other


features
contain only noise.
"""
X, y = make_blobs(n_samples=n_samples, n_features=1, centers=[[-2],
[2]])

# add non-discriminative features


if n_features > 1:
X = [Link]([X, [Link](n_samples, n_features - 1)])
return X, y
acc_clf1, acc_clf2 = [], []
n_features_range = range(1, n_features_max + 1, step)
for n_features in n_features_range:
score_clf1, score_clf2 = 0, 0
for _ in range(n_averages):
X, y = generate_data(n_train, n_features)

clf1 = LinearDiscriminantAnalysis(solver='lsqr',
shrinkage='auto').fit(X, y)
clf2 = LinearDiscriminantAnalysis(solver='lsqr',
shrinkage=None).fit(X, y)

X, y = generate_data(n_test, n_features)
score_clf1 += [Link](X, y)
score_clf2 += [Link](X, y)

acc_clf1.append(score_clf1 / n_averages)
acc_clf2.append(score_clf2 / n_averages)

features_samples_ratio = [Link](n_features_range) / n_train

[Link](features_samples_ratio, acc_clf1, linewidth=2,


label="Linear Discriminant Analysis with shrinkage",
color='navy')
[Link](features_samples_ratio, acc_clf2, linewidth=2,
label="Linear Discriminant Analysis", color='gold')

[Link]('n_features / n_samples')
[Link]('Classification accuracy')

[Link](loc=1, prop={'size': 12})


[Link]('Linear Discriminant Analysis vs. \
shrinkage Linear Discriminant Analysis (1 discriminative feature)')
[Link]()

You might also like