<jupyter_start><jupyter_text>Klasifikasi Note* Label pada klasifikasi bertipe nominal* Label pada regresi bertipe numerik  Knn * Berbasis point* data numerik yg di nominalkan, isunya adalah bagaimana menentukan intervalnya* data nominal ke numerik, yg mana akan diurutkan sbg nomor 1, dst  Decision Tree * Sensitif terhadap prioritas atributnya* isunya :    * bagaimana mendapatkan tree dengan kedalaman terkecil    * untuk data numerik, perlu di batasi* ide:    * menggabungkan tree dari berbagai versi (pemilihan root dan node) --> Boosting, contoh: random forest, XG Boost  Naive Bayes    * Berbasis probability ISU    * 1. tidak bisa menghitung jika tidak ada di training data --> probabilitas salah satu nol, keseluruhan jadi nol        * Solusi: dg metode smoothin --> nilai probabilitas dimodifikasi supaya tidak nol    * 2. Sensitif terhadap kelas imbalance: cenderung ke kelas yg lebih besar    * 3. Lebih bagus untuk tipe data nominal. Untuk tipe numerik bisa dibawa ke tipe nominal (interval)  Evaluasi* untuk data imbalance: undersampling, oversampling     * SMOTE, dll  Clustering * tidak batasi labelnya seperti apa.  Handson  Clustering K-Means<jupyter_code>from sklearn.decomposition import PCA
from sklearn.cluster import KMeans
from sklearn.datasets import load_iris
import pylab as pl
iris = load_iris()
iris.keys()
iris.data
iris.target
iris.target_names
iris.feature_names
pca = PCA(n_components=2).fit(iris.data)
pca_2d = pca.transform(iris.data)
print(pca_2d)
pl.figure('Reference Plot')
pl.scatter(pca_2d[:,0], pca_2d[:,1], c=iris.target)
kmeans = KMeans(n_clusters=3, random_state=111) # inisialisasi data
kmeans.fit(iris.data) # run learning/ modelling
kmeans.labels_
# Plot grafik
pl.figure('K-means with 3 clusters')
pl.scatter(pca_2d[:,0], pca_2d[:,1], c=kmeans.labels_) # (x, y, color)
pl.show()
new_observation = [[0.8, 0.8, 0.8, 0.8]]
# Predict observation's cluster
kmeans.predict(new_observation)
# View Centers Of Each Cluster
# View cluster centers
kmeans.cluster_centers_<jupyter_output><empty_output><jupyter_text>Cluster Performance evaluation<jupyter_code># Check this: http://scikit-learn.org/stable/auto_examples/cluster/plot_kmeans_digits.html#sphx-glr-auto-examples-cluster-plot-kmeans-digits-py

from sklearn import metrics
#labels_true = [0, 0, 0, 1, 1, 1]
#labels_pred = [0, 0, 1, 1, 2, 2]
#metrics.adjusted_rand_score(labels_true, labels_pred)<jupyter_output><empty_output><jupyter_text>Hierarchical Clustering<jupyter_code>from matplotlib import pyplot as plt
from scipy.cluster.hierarchy import dendrogram, linkage
import numpy as np
np.random.seed(4711)
a = np.random.multivariate_normal([10, 0], [[3, 1], [1, 4]],
size=[100,]) # 100 sample
b = np.random.multivariate_normal([0, 20], [[3, 1], [1, 4]],
size=[50,]) # 50 sample
X = np.concatenate((a, b),axis = 0)
a = np.random.multivariate_normal([10, 0], [[3, 1], [1, 4]],
size=[100,])
print(X.shape) # 150 samples with 2 dimensions
plt.scatter(X[:,0], X[:,1])
plt.show()
Z = linkage(X, 'ward')
plt.figure(figsize=(25, 10))
plt.title('Hierarchical Clustering Dendrogram')
plt.xlabel('sample index')
plt.ylabel('distance')
dendrogram(
Z,
leaf_rotation=90., # rotates the x axis labels
leaf_font_size=8., # font size for the x axis labels
)
plt.show()
plt.title('Hierarchical Clustering Dendrogram (truncated)')
plt.xlabel('sample index or (cluster size)')
plt.ylabel('distance')
dendrogram(
    Z,
    truncate_mode='lastp', # show only the last p merged clusters
    p=12, # show only the last p merged clusters
    leaf_rotation=90.,
    leaf_font_size=12.,
    show_contracted=True, # to get a distribution impression in truncated branches
)
plt.show()<jupyter_output><empty_output><jupyter_text>DBScan<jupyter_code>import numpy as np
from sklearn.cluster import DBSCAN
from sklearn import metrics
from sklearn.datasets.samples_generator import make_blobs
from sklearn.preprocessing import StandardScaler
# ######Generate sample Data
centers = [[5, 5], [-5, -1], [0, 0]]
X, labels_true = make_blobs(n_samples=750, centers=centers,
cluster_std=0.4, random_state=0)
X = StandardScaler().fit_transform(X)
# Compute DBSCAN
db = DBSCAN(eps=0.3, min_samples=10).fit(X)
core_samples_mask = np.zeros_like(db.labels_, dtype=bool)
core_samples_mask[db.core_sample_indices_] = True
labels = db.labels_
# Number of clusters in labels, ignoring noise if present.
n_clusters_ = len(set(labels)) - (1 if -1 in labels else 0)
print('Estimated number of clusters: %d' % n_clusters_)
print("Homogeneity: %0.3f" % metrics.homogeneity_score(labels_true,
labels))
print("Silhouette Coeficient: %0.3f"% metrics.silhouette_score(X,labels))
import matplotlib.pyplot as plt
unique_labels = set(labels)
colors = [plt.cm.Spectral(each)for each in np.linspace(0, 1, len(unique_labels))]
for k, col in zip(unique_labels, colors):
    if k == -1:
        # Black used for noise.
        col = [0, 0, 0, 1]
    class_member_mask = (labels == k)
    xy = X[class_member_mask & core_samples_mask]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
    markeredgecolor='k', markersize=14)
    xy = X[class_member_mask & ~core_samples_mask]
    plt.plot(xy[:, 0], xy[:, 1], 'o', markerfacecolor=tuple(col),
    markeredgecolor='k', markersize=6)
#plt.title(gEstimated number of clusters: %dg % n_clusters_)
plt.show()<jupyter_output><empty_output><jupyter_text>Klasifikasi<jupyter_code># Step 0: Import package Scikit-Learn
# from sklearn.datasets import load_svmlight_file
# from sklearn import preprocessing
# from sklearn.metrics import classification_report, confusion_matrix
# from sklearn.cross_validation import train_test_split
import numpy as np
# Step 1: Preprocess the Data
## a. Standardization (zero mean and unit variance)
X = np.array([[1., -1., 2.],
             [2.,0.,0.],
             [0.,1.,-1.]])
X_scaled = preprocessing.scale(X)
X_scaled
## b. Scaling to [0,1]
min_max_scaler = preprocessing.MinMaxScaler()
X_train_minmax = min_max_scaler.fit_transform(X)
X_train_minmax
# Step 2: Split the data
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression

X,y = X_train_minmax,range(3)
seed = 7
test_size = 0.2
X_train, X_test, Y_train, Y_test = train_test_split(X,y, test_size=test_size, random_state=seed)
X_train
X_test
Y_train
Y_test
# Step 3: Train and Evaluate (Consist of learning, inference, with crossval)
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_score
import sklearn.metrics as metrics
from sklearn.model_selection import cross_val_predict

seed = 7
num_folds = 5
k_fold = KFold(n_splits=num_folds, random_state=seed)
model = LogisticRegression()
predicted = cross_val_predict(model, X_train, Y_train, cv=k_fold)
print ("Accuracy : %.3f"%(metrics.accuracy_score(Y_train, predicted)*100))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11646645180193378, 'fraction_numerical': 0.023439789041898623, 'mean_word_length': 5.634596695821186, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '242187', 'license': 'MIT', 'n_tokens_mistral': 2610, 'n_tokens_neox': 2460, 'n_words': 646, 'path': 'day3.ipynb', 'repo_name': 'indrawaspada/dmbd2018_ui', 'size': '6826'}