- KNN:
- import numpy as np
- import matplotlib.pyplot as pit
- import pandas as pd
- # In[13]:
- url = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
- names = ['sepal-length', 'sepal-width', 'petal-length', 'petal-width', 'Class']
- dataset = pd.read_csv(url, names=names)
- # In[14]:
- dataset.head()
- # In[15]:
- X = dataset.iloc[:,:-1].values
- y = dataset.iloc[:, 4].values
- # In[16]:
- from sklearn.model_selection import train_test_split
- X_train, X_test, y_train, y_test = train_test_split(X,y, test_size=0.20)
- # In[17]:
- from sklearn.preprocessing import StandardScaler
- scaler = StandardScaler()
- scaler.fit(X_train)
- X_train = scaler.transform(X_train)
- X_test = scaler.transform(X_test)
- # In[18]:
- from sklearn.neighbors import KNeighborsClassifier
- classifier = KNeighborsClassifier(n_neighbors=5)
- classifier.fit(X_train, y_train)
- # In[19]:
- y_pred = classifier.predict(X_test)
- # In[21]:
- from sklearn.metrics import classification_report, confusion_matrix
- print(classification_report(y_test, y_pred))
- print(confusion_matrix(y_test, y_pred))
- Decision Tree:
- import numpy as np
- import pandas as pd
- from sklearn.model_selection import train_test_split
- from sklearn.tree import DecisionTreeClassifier
- from sklearn.metrics import accuracy_score
- from sklearn import tree
- # In[95]:
- balance_data = pd.read_csv("C:/Users/test.LAB/Desktop/balance.csv")
- # In[96]:
- balance_data.head()
- # In[97]:
- X = balance_data.values[:, 1:5]
- Y = balance_data.values[:, 0]
- # In[98]:
- X_train, X_test, y_train, y_test = train_test_split( X, Y, test_size = 0.3, random_state = 100)
- # In[99]:
- clf_entropy = DecisionTreeClassifier(criterion = "entropy", random_state=100, max_depth=3, min_samples_leaf=5)
- clf_entropy.fit(X_train, y_train)
- # In[100]:
- y_pred_en = clf_entropy.predict(X_test)
- y_pred_en
- # In[101]:
- print(("Accuracy is"), accuracy_score(y_test, y_pred_en)*100)
- K Means Clustering:
- import numpy as np
- import matplotlib.pyplot as plt
- import pandas as pd
- #Importing the mall dataset with pandas
- dataset = pd.read_csv('Mall_Customers.csv')
- X = dataset.iloc[:,[3,4]].values
- # Using the elbow method to find the optimal number of clusters
- 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_)
- # Plot the graph to visualize the Elbow Method to find the optimal number of cluster
- plt.plot(range(1,11),wcss)
- plt.title('The Elbow Method')
- plt.xlabel('Number of clusters')
- plt.ylabel('WCSS')
- plt.show()
- # Applying KMeans to the dataset with the optimal number of cluster
- kmeans=KMeans(n_clusters= 5, init = 'k-means++', max_iter = 300, n_init = 10, random_state = 0)
- y_kmeans = kmeans.fit_predict(X)
- # Visualising the clusters
- plt.scatter(X[Y_Kmeans == 0, 0], X[Y_Kmeans == 0,1],s = 100, c='red', label = 'Cluster 1')
- plt.scatter(X[Y_Kmeans == 1, 0], X[Y_Kmeans == 1,1],s = 100, c='blue', label = 'Cluster 2')
- plt.scatter(X[Y_Kmeans == 2, 0], X[Y_Kmeans == 2,1],s = 100, c='green', label = 'Cluster 3')
- plt.scatter(X[Y_Kmeans == 3, 0], X[Y_Kmeans == 3,1],s = 100, c='cyan', label = 'Cluster 4')
- plt.scatter(X[Y_Kmeans == 4, 0], X[Y_Kmeans == 4,1],s = 100, c='magenta', label = 'Cluster 5')
- plt.scatter(kmeans.cluster_centers_[:,0], kmeans.cluster_centers_[:,1], s = 300, c = 'yellow', label = 'Centroids')
- plt.title('Clusters of clients')
- plt.xlabel('Annual Income (k$)')
- plt.ylabel('Spending score (1-100)')
- plt.legend()
- plt.show()
Recent Pastes