Python
Python
Prerequisites
1. Install Python: Make sure you have Python installed. You can download it
from Python's official website (https://www.python.org/downloads/).
2. Install Required Libraries: You will need the following libraries: 'pandas',
'numpy', and 'matplotlib'. You can install them using pip.
3. Set Up Your IDE: You can use any Python IDE or text editor (like Jupyter
Notebook, VS Code, or PyCharm).
Step 1: Gather Data
For demonstration, let’s create a sample dataset in CSV format. Save the
following data in a file named 'business_data.csv'.
CustomerID,Name,Email,JoinDate,AmountSpent
1,John Doe,[email protected],2024-01-15,150.00
2,Jane Smith,[email protected],2024-02-20,200.00
3,Bob Johnson,,2024-03-05,150.00
4,Mary Johnson,[email protected],2024-02-30,300.00
5,Tom Brown,[email protected],2024-03-15,400.00
6,Emily Davis,[email protected],2024-01-25,
1,John Doe,[email protected],2024-01-15,150.00
data = load_iris()
df = pd.DataFrame(data.data,
columns=data.feature_names)
print(df.head())
Step 3: Dimensionality Reduction Techniques
essential patterns.
pca = PCA(n_components=2)
df_pca = pca.fit_transform(df)
print(df_pca[:5])
Sample Output:
[[-2.68412563 0.31939725]
[-2.71414169 -0.17700123]
[-2.88899057 -0.14494943]
[-2.74534286 -0.31829898]
[-2.72871654 0.32675451]]
b. t-Distributed Stochastic Neighbor Embedding (t-SNE)
Sample Code:
df_tsne = tsne.fit_transform(df)
print(df_tsne[:5])
Sample Output:
[[ 1.2379045 12.769159 ]
[ 8.755232 7.7505245]
[ 9.419792 8.941869 ]
[ 9.378086 7.217551 ]
[ 2.849782 6.5989175]]
Sample Code:
-----------
plt.figure(figsize=(10, 7))
dendrogram(linked, truncate_mode='lastp')
plt.title("Hierarchical Clustering Dendrogram")
plt.show()
Expected Output: A dendrogram plot will display showing hierarchical relationships between data
points.
Sample Code:
cluster = AgglomerativeClustering(n_clusters=3)
labels = cluster.fit_predict(df)
Sample Output:
This score evaluates clustering quality, where higher values indicate better-defined
clusters.