JustPaste
HomeCategoriesAboutDonateContactTerms of UsePrivacy Policy
JustPaste

Free online notepad — write and share instantly

Navigate

  • Home
  • Timeline
  • Categories

Info

  • About
  • Donate
  • Contact

Legal

  • Terms of Use
  • Privacy Policy

© 2026 JustPaste.app. All rights reserved.

Made with ♥ by JustPaste

Untitled Page | JustPaste.app
about 2 months ago3 views
👨‍💻Programming
#5
import matplotlib.pyplot as plt
from sklearn import datasets
from sklearn.cluster import KMeans
import pandas as pd
import numpy as np
# import some data to play with
iris = datasets.load_iris()
X = pd.DataFrame(iris.data)
X.columns = ['Sepal_Length','Sepal_Width','Petal_Length','Petal_Width']
y = pd.DataFrame(iris.target)
y.columns = ['Targets']
# Build the K Means Model
model = KMeans(n_clusters=3)
# model.labels_ : Gives cluster no for which samples belongs to
model.fit(X) 
# Visualise the clustering results
plt.figure(figsize=(14,14))
colormap = np.array(['red', 'lime', 'black'])
# General EM for GMM
from sklearn import preprocessing
# transform your data such that its distribution will have a
# mean value 0 and standard deviation of 1.
scaler = preprocessing.StandardScaler()
scaler.fit(X)
xsa = scaler.transform(X)
xs = pd.DataFrame(xsa, columns = X.columns)

from sklearn.mixture import GaussianMixture

gmm = GaussianMixture(n_components=3)
gmm.fit(xs)
gmm_y = gmm.predict(xs)
plt.subplot(2, 2, 3)
plt.scatter(X.Petal_Length, X.Petal_Width, c=colormap[gmm_y], s=40)
plt.title('GMM Clustering')
plt.xlabel('Petal Length')
plt.ylabel('Petal Width')
plt.show()
print('Observation: The GMM using EM algorithm based clustering matched the true labels more closely than the Kmeans.')
#6
from itertools import combinations
# Transaction Dataset
transactions = [
#7
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn.datasets import load_breast_cancer
from sklearn.preprocessing import MinMaxScaler
from sklearn.preprocessing import StandardScaler
from sklearn.decomposition import PCA

cancer=load_breast_cancer()
cancer.keys()

print(cancer['feature_names'])

for i in range(len(cancer.target_names)):
    print("Label",i,"-",str(cancer.target_names[i]))
    
print(cancer['DESCR'])

df=pd.DataFrame(cancer['data'],columns=cancer['feature_names'])
df.head(5)

scaler=StandardScaler()
scaler.fit(df)
scaled_data=scaler.transform(df)
scaled_data

pca=PCA(n_components=2)
pca.fit(scaled_data)
x_pca=pca.transform(scaled_data)

scaled_data.shape

x_pca.shape

scaled_data

x_pca

plt.figure(figsize=(8,6))
plt.scatter(x_pca[:,0],x_pca[:,1],c=cancer['target'])
plt.xlabel('First principle component')
plt.ylabel('Second principle component')

    ['Milk', 'Bread', 'Butter'],
    ['Bread', 'Butter'],
    ['Milk', 'Bread'],
    ['Milk', 'Butter'],
    ['Milk', 'Bread', 'Butter']
]
# Minimum support count
min_support = 2

# Function to calculate support count
def get_support(itemset, transactions):
    count = 0
    for transaction in transactions:
        if itemset.issubset(set(transaction)):
            count += 1
    return count
    
# Step 1: Generate Frequent 1-itemsets
items = set()
for transaction in transactions:
    for item in transaction:
        items.add(frozenset([item]))

frequent_itemsets = dict()

current_L = dict()

print("Frequent 1-itemsets:")
for item in items:
    support = get_support(set(item), transactions)
    if support >= min_support:
        current_L[item] = support
        print(set(item), "Support =", support)

k = 2

# Store all frequent itemsets
frequent_itemsets.update(current_L)

# Step 2 onwards
while current_L:
    candidates = set()
    current_items = list(current_L.keys())
    # Generate candidate itemsets
    for i in range(len(current_items)):
        for j in range(i + 1, len(current_items)):
            union_set = current_items[i].union(current_items[j])
            if len(union_set) == k:
                candidates.add(union_set)
    current_C = dict()

    # Calculate support for candidates
    for candidate in candidates:
        support = get_support(set(candidate), transactions)

        if support >= min_support:
            current_C[candidate] = support

    # Print frequent k-itemsets
    if current_C:
        print(f"\nFrequent {k}-itemsets:")
        for itemset, support in current_C.items():
            print(set(itemset), "Support =", support)

    # Update frequent itemsets
    frequent_itemsets.update(current_C)

    current_L = current_C
    k += 1

# Association Rule Generation
print("\nAssociation Rules:")

for itemset in frequent_itemsets:
    if len(itemset) >= 2:
        for i in range(1, len(itemset)):
            subsets = combinations(itemset, i)

            for subset in subsets:
                subset = frozenset(subset)

                remain = itemset - subset

                support_itemset = frequent_itemsets[itemset]
                support_subset = frequent_itemsets[subset]

                confidence = support_itemset / support_subset

                print(f"{set(subset)} => {set(remain)}")
                print("Confidence =", round(confidence * 100, 2), "%\n")
← Back to timeline