0% found this document useful (0 votes)
42 views13 pages

Linear SVM: 'Target'

Uploaded by

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

Linear SVM: 'Target'

Uploaded by

saran kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

LINEAR SVM

import pandas as pd
from sklearn.datasets import load_iris
iris=load_iris()
from sklearn.neighbors import KNeighborsClassifier
knn=KNeighborsClassifier(n_neighbors=10)

iris.feature_names

['sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal
width (cm)']

iris.target_names

array(['setosa', 'versicolor', 'virginica'], dtype='<U10')

df=pd.DataFrame(iris.data,columns=iris.feature_names)
df.head

<bound method NDFrame.head of sepal length (cm) sepal width (cm) petal
length (cm) petal width (cm) 0 5.1 3.5 1.4 0.2 1 4.9 3.0 1.4 0.2 2 4.7
3.2 1.3 0.2 3 4.6 3.1 1.5 0.2 4 5.0 3.6 1.4 0.2 .. ... ... ... ... 145
6.7 3.0 5.2 2.3 146 6.3 2.5 5.0 1.9 147 6.5 3.0 5.2 2.0 148 6.2 3.4 5.4
2.3 149 5.9 3.0 5.1 1.8 [150 rows x 4 columns]>

df['target']=iris.target
df.head()

sepal length (cm)sepal width (cm)petal length (cm)petal width


(cm)target05.13.51.40.2014.93.01.40.2024.73.21.30.2034.63.11.50.2045.03.61.40.20

df[df.target==1].head()

sepal length (cm)sepal width (cm)petal length (cm)petal width


(cm)target507.03.24.71.41516.43.24.51.51526.93.14.91.51535.52.34.01.31546.52.84.61.51

df[df.target==2].head()
df['flower_name'] =df.target.apply(lambda x:
iris.target_names[x])
df.head()

df[45:55]

df0=df[:50]
df1=df[50:100]
df2=df[100:]

import matplotlib.pyplot as plt


%matplotlib inline

plt.xlabel('Sepal Length')
plt.ylabel('Sepal Width')
plt.scatter(df0['sepal length (cm)'],df0['sepal width
(cm)'],color="green",marker='+')
plt.scatter(df1['sepal length (cm)'],df1['sepal width
(cm)'],color="blue",marker='.')
NONLINEAR SVM

import numpy as np
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

X = np.random.randn(100,2)
y = np.array([0]*50+[1]*50)

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.3, random_state=0)

scaler = StandardScaler()
classifier = SVC(kernel='rbf', gamma='scale')

classifier.fit(X_train, y_train)

plt.figure(figsize=(4, 3))
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, 0.1),
np.arange(y_min, y_max, 0.1))
z=classifier.predict(np.c_[xx.ravel(),yy.ravel()])
z=z.reshape(xx.shape)
plt.contourf(xx, yy,z,alpha=0.4)
plt.scatter(X[:, 0], X[:, 1], c=y, s=20, edgecolors='b')
plt.xlabel('Feature 1')
plt.ylabel('Feature 2')
plt.title('Non-linear SVM')
plt.show()
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
iris = datasets.load_iris()
X = iris.data[:, :2]
y = iris.target
C = 1.0
svc = svm.SVC(kernel ='rbf', C = 1).fit(X, y)
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
h = (x_max / x_min)/100
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
plt.subplot(1, 1, 1)
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm, datasets
iris = datasets.load_iris()
X = iris.data[:, :2]
y = iris.target
C = 1.0
svc = svm.SVC(kernel ='rbf', C = 1).fit(X, y)
x_min, x_max = X[:, 0].min() - 1, X[:, 0].max() + 1
y_min, y_max = X[:, 1].min() - 1, X[:, 1].max() + 1
h = (x_max / x_min)/100
xx, yy = np.meshgrid(np.arange(x_min, x_max, h),
np.arange(y_min, y_max, h))
plt.subplot(1, 1, 1)
Z = svc.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap = plt.cm.Paired, alpha = 0.8)
plt.scatter(X[:, 0], X[:, 1], c = y, cmap = plt.cm.Paired)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.xlim(xx.min(), xx.max())
plt.title('SVC with non-linear kernel')
plt.show()

KNN

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split
import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap
from sklearn.neighbors import KNeighborsClassifier

cmap = ListedColormap(['#FF0000','#00FF00','#0000FF'])

iris = datasets.load_iris()
X, y = iris.data, iris.target

X_train, X_test, y_train, y_test = train_test_split(X, y,


test_size=0.2, random_state=1234)
h=0.02
plt.figure()

plt.scatter(X[:,2],X[:,3], c=y, cmap=cmap, edgecolor='k',


s=50)
plt.xlabel('Sepal length')
plt.ylabel('Sepal width')
plt.show()

clf = KNeighborsClassifier(n_neighbors=5)
clf.fit(X_train, y_train)
predictions = clf.predict(X_test)

print(predictions)

acc = np.sum(predictions == y_test) / len(y_test)


print(acc)
PCA

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA

data=pd.read_csv("/content/Acoustic Features - Acoustic


Features.csv")
x=data.iloc[:,5:9].values
y=data.iloc[:,0].values
X_train, X_test, y_train, y_test = train_test_split(x, y,
test_size = 0.2, random_state = 0)

sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)
pca = PCA(n_components = 3)

principalComponents = pca.fit_transform(x)

principalDf = pd.DataFrame(data = principalComponents, columns


= ['PC 1', 'PC 2','PC 3'])

finalDf = pd.concat([principalDf, data[['Class']]], axis = 1)

fig = plt.figure(figsize = (8,8))


ax = fig.add_subplot(1,1,1)
ax.set_xlabel('PC 1')
ax.set_ylabel('PC 2')
targets = ['relax', 'happy', 'sad', 'angry']
colors = ['g', 'y', 'b','r']
for target, color in zip(targets,colors):
ind = finalDf['Class'] == target
ax.scatter(finalDf.loc[ind, 'PC 1'], finalDf.loc[ind, 'PC
2'],c = color, s = 10)
ax.legend(targets)
ax.grid
K MEANS

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

dataset= pd.read_csv("/content/Acoustic Features.csv")


X=dataset.iloc[:, [1,4]].values
from sklearn.cluster import KMeans
wcss=[]

for i in range(1,11):
kmeans = KMeans(n_clusters=i, init ='k-means++',
max_iter=300, n_init=10,random_state=0 )
kmeans.fit(X)
wcss.append(kmeans.inertia_)

plt.plot(range(0,10),wcss)
plt.title('The Elbow Method Graph')
plt.xlabel('Number of clusters')
plt.ylabel('WCSS')
plt.show()
kmeans = KMeans(n_clusters=5, init ='k-means++', max_iter=300,
n_init=5,random_state=0 )
y_kmeans = kmeans.fit_predict(X)

plt.scatter(X[y_kmeans==0, 0], X[y_kmeans==0, 1], s=10,


c='red', label ='Cluster 1')
plt.scatter(X[y_kmeans==1, 0], X[y_kmeans==1, 1], s=10,
c='blue', label ='Cluster 2')
plt.scatter(X[y_kmeans==2, 0], X[y_kmeans==2, 1], s=10,
c='green', label ='Cluster 3')
plt.scatter(X[y_kmeans==3, 0], X[y_kmeans==3, 1], s=10,
c='cyan', label ='Cluster 4')
plt.scatter(X[y_kmeans==4, 0], X[y_kmeans==4, 1], s=10,
c='magenta', label ='Cluster 5')

plt.scatter(kmeans.cluster_centers_[:, 0],
kmeans.cluster_centers_[:, 1], s=30, c='yellow', label =
'Centroids')
plt.xlabel('Energy mean')
plt.ylabel('Class')
plt.show()
LINKAGE

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

ourData = pd.read_csv('/content/Mall_Cust.csv')
ourData.head()

newData = ourData.iloc[:, [2, 3]].values

import scipy.cluster.hierarchy as sch


dendrogram = sch.dendrogram(sch.linkage(newData, method =
'single'))
plt.title('Dendrogram')
plt.xlabel('Customers')
plt.ylabel('Euclidean distances')
plt.show()

import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage

x = [4, 5, 10, 4, 3, 11, 14 , 6, 10, 12]


y = [21, 19, 24, 17, 16, 25, 24, 22, 21, 21]

data = list(zip(x, y))

linkage_data = linkage(data, method='complete',


metric='euclidean')
dendrogram(linkage_data)

plt.show()

You might also like