<jupyter_start><jupyter_text>Scikit-Learn* [Preprocess dataset](preprocess_dataset)  * [Divide into train and test](divide_train_test)  * [PCA](PCA)* [Clustering](clustering)  * [Silhouette](silhouette)* [Classifier](classifier)  * [Predict](predict)  * [Evaluate](evaluate)<jupyter_code>import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt 

### Load Dataset
iris = datasets.load_iris()

X = iris.data
y = iris.target

print(X[:10])
print(y[:10])<jupyter_output>[[5.1 3.5 1.4 0.2]
 [4.9 3.  1.4 0.2]
 [4.7 3.2 1.3 0.2]
 [4.6 3.1 1.5 0.2]
 [5.  3.6 1.4 0.2]
 [5.4 3.9 1.7 0.4]
 [4.6 3.4 1.4 0.3]
 [5.  3.4 1.5 0.2]
 [4.4 2.9 1.4 0.2]
 [4.9 3.1 1.5 0.1]]
[0 0 0 0 0 0 0 0 0 0]<jupyter_text>Preprocess dataset  Divide into train and test<jupyter_code>from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size = 0.2)
print("From the datasets X and y")
print(np.shape(X) )
print(np.shape(y) )
print("It has been split into train and test:")
print("Size of train:")
print(np.shape(x_train) )
print(np.shape(y_train) )
print("Size of test:")
print(np.shape(x_test) )
print(np.shape(y_test) )<jupyter_output>From the datasets X and y
(150, 4)
(150,)
It has been split into train and test:
Size of train:
(120, 4)
(120,)
Size of test:
(30, 4)
(30,)<jupyter_text>PCADefine a function to compute PCA and perform a scatterplot<jupyter_code>from sklearn.decomposition import PCA
import matplotlib.pyplot as plt 
def plot_dataset(X,colors):
    pca = PCA(n_components=2)
    pca.fit(X)
    principalComponents = pca.fit_transform(X)
    plt.scatter(principalComponents[:, 0], principalComponents[:, 1],c=colors)
    plt.show()<jupyter_output><empty_output><jupyter_text>Clustering<jupyter_code>from sklearn.cluster import DBSCAN
db = DBSCAN(eps=0.3, min_samples=5).fit(x_train)

#use db.labels_ as colors
plot_dataset(x_train,db.labels_)
from sklearn.cluster import KMeans
kmeans = KMeans(n_clusters=3, random_state=0).fit(x_train)
plot_dataset(x_train,kmeans.labels_)<jupyter_output><empty_output><jupyter_text>Silhouette<jupyter_code>from sklearn import metrics

silhouette_scores=[]
for i in range(2,10):
    kmeans = KMeans(n_clusters=i, random_state=0).fit(x_train)
    labels = kmeans.labels_
    silhouette=metrics.silhouette_score(x_train, labels, metric='euclidean')
    silhouette_scores.append(silhouette)

plt.plot(silhouette_scores)
plt.show()<jupyter_output><empty_output><jupyter_text>Classifier<jupyter_code>### Train KNN
from sklearn import neighbors
n_neighbors = 15
clf = neighbors.KNeighborsClassifier(n_neighbors)
clf.fit(x_train,  y_train)<jupyter_output><empty_output><jupyter_text>Predict<jupyter_code># Make a prediction
y_predicted = clf.predict( x_test )
print(y_predicted)<jupyter_output>[1 1 2 1 1 2 2 1 0 2 2 2 1 0 2 1 2 0 1 2 1 1 0 0 2 2 1 2 1 2]<jupyter_text>Evaluate<jupyter_code>from sklearn.metrics import confusion_matrix
confusion_matrix(y_test, y_predicted)
from sklearn.metrics import precision_recall_fscore_support
precision,recall,f_score,support = precision_recall_fscore_support(y_test, y_predicted, average='macro')
print(precision)
print(recall)
print(f_score)<jupyter_output>0.9188034188034188
0.9220779220779222
0.9194847020933977
{'fraction_non_alphanumeric': 0.12688172043010754, 'fraction_numerical': 0.06359447004608294, 'mean_word_length': 6.093681917211329, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 5, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '492464', 'license': 'CC0-1.0', 'n_tokens_mistral': 1461, 'n_tokens_neox': 1312, 'n_words': 288, 'path': 'documents/scikit-learn.ipynb', 'repo_name': 'alberto-poncelas/Datascience_Tools_guides', 'size': '3255'}