sklearn machine learning to classify the rows of a excel sheet containing text, date and float columns, unsupervised

i am just starting with machine learning. I am trying to deal with simple examples.
i have a *.csv file with columns containing text, numbers and dates. My goal, for learning purposes, is to classify the rows of this file in unsupervised mode using for example KMeans.

I don’t know how to combine all transformers to use it with KMeans.
Maybe someone can help me with different suggestions.

`

import numpy as np
import pandas as pd
from sklearn.cluster import KMeans
from sklearn.preprocessing import StandardScaler
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.pipeline import make_pipeline
from sklearn.base import BaseEstimator, TransformerMixin

class DateTransformer(BaseEstimator, TransformerMixin):
    def fit(self, X, y=None):
        return self

    def transform(self, X):
       X_date_encoded = np.zeros_like(X)
       valid_dates = ~np.isnat(X).all(axis=1)
       if np.any(valid_dates):
          X_date_encoded[valid_dates] = (X[valid_dates] - X[valid_dates].min()) / (
                    X[valid_dates].max() - X[valid_dates].min())
       return X_date_encoded


def classify_excel_rows(file_path, n_clusters=3):
    """
    Classify the rows of an Excel sheet using unsupervised learning (KMeans).

    Args:
    - file_path: The path to the Excel file.
    - n_clusters: The number of clusters to form.

    Returns:
    - A DataFrame with an additional column 'Cluster' indicating the cluster each row belongs to.
   """

   # Read the Excel file
   data = pd.read_csv(file_path, sep=';', skiprows=4)

   data['Value'] = data['Value'].str.replace(',', '.').astype(float)
   data['Value'] = pd.to_numeric(data['Value'], errors='coerce')

   date_format = "%d.%m.%Y"
   data['Date_1'] = pd.to_datetime(data['Date_1'], format=date_format, errors='coerce')
   data['Date_2'] = pd.to_datetime(data['Date_2'], format=date_format, errors='coerce')

   # Identify different data types
   text_columns = data.select_dtypes(include=['object']).columns.tolist()
   numeric_columns = data.select_dtypes(include=['float64']).columns
   date_columns = data.select_dtypes(include=['datetime']).columns

   # Preprocess the data
   numeric_transformer = make_pipeline(StandardScaler())
   text_transformer = make_pipeline(TfidfVectorizer())
   date_transformer = make_pipeline(DateTransformer())

   **X = ???? # how to combine all transformers to use it with KMeans?**

   [...]

   # Perform K-Means clustering
   kmeans = KMeans(n_clusters=n_clusters, random_state=42)
   labels = kmeans.fit_predict(X)

`

I have tried with the example code above. I don’t know how to use KMeans with this type of data for unsupervised learning/classification. I expect to cluster the rows of my *.csv file according to the column values.

New contributor

Pete is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật