Professional Documents
Culture Documents
ml
ml
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
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()
df[df.target==1].head()
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:]
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)
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
clf = KNeighborsClassifier(n_neighbors=5)
clf.fit(X_train, y_train)
predictions = clf.predict(X_test)
print(predictions)
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
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)
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
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(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()
import numpy as np
import matplotlib.pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
plt.show()