| |
| import pandas as pd |
| import numpy as np |
| |
| from sklearn.model_selection import train_test_split |
| |
| from sklearn.preprocessing import LabelEncoder |
| |
| from huggingface_hub import login, HfApi |
| import os |
|
|
| |
|
|
| |
| |
| api = HfApi(token=os.getenv("HF_TOKEN")) |
|
|
| |
|
|
| DATASET_PATH = "hf://datasets/Tulsi10/Tourism/tourism.csv" |
| df = pd.read_csv(DATASET_PATH) |
| print("Dataset loaded successfully.") |
| print(f"Dataset shape: {df.shape}") |
|
|
| |
| if 'Unnamed: 0' in df.columns or df.columns[0] == '': |
| df = df.iloc[:, 1:] |
|
|
| |
| if 'CustomerID' in df.columns: |
| df.drop(columns=['CustomerID'], inplace=True) |
|
|
|
|
| |
| categorical_cols = df.select_dtypes(include=['object']).columns |
| for col in categorical_cols: |
| if df[col].isnull().sum() > 0: |
| df[col].fillna(df[col].mode()[0], inplace=True) |
|
|
| |
| if 'Gender' in df.columns: |
| df['Gender'] = df['Gender'].str.strip().replace({'Fe Male': 'Female', 'Fe male': 'Female'}) |
|
|
| |
| print("\nEncoding categorical variables...") |
| label_encoder = LabelEncoder() |
|
|
| |
| categorical_features = ['TypeofContact', 'Occupation', 'Gender', 'ProductPitched', |
| 'MaritalStatus', 'Designation'] |
|
|
| for col in categorical_features: |
| if col in df.columns: |
| df[col] = label_encoder.fit_transform(df[col].astype(str)) |
| |
| target_col = 'ProdTaken' |
|
|
| |
| X = df.drop(columns=[target_col]) |
| y = df[target_col] |
|
|
| print(f"\nFeatures shape: {X.shape}") |
| print(f"Target shape: {y.shape}") |
| print(f"Target distribution:\n{y.value_counts()}") |
|
|
| |
| Xtrain, Xtest, ytrain, ytest = train_test_split( |
| X, y, test_size=0.2, random_state=42, stratify=y |
| ) |
|
|
| print(f"\nTrain set size: {Xtrain.shape[0]}") |
| print(f"Test set size: {Xtest.shape[0]}") |
|
|
| |
| Xtrain.to_csv("Xtrain.csv", index=False) |
| Xtest.to_csv("Xtest.csv", index=False) |
| ytrain.to_csv("ytrain.csv", index=False) |
| ytest.to_csv("ytest.csv", index=False) |
|
|
| print("\nDatasets saved locally.") |
|
|
| |
| files = ["Xtrain.csv", "Xtest.csv", "ytrain.csv", "ytest.csv"] |
|
|
| for file_path in files: |
| api.upload_file( |
| path_or_fileobj=file_path, |
| path_in_repo=file_path.split("/")[-1], |
| repo_id="Tulsi10/Tourism", |
| repo_type="dataset", |
| ) |
| print(f"Uploaded {file_path} to Hugging Face") |
|
|
| print("\nData preparation completed successfully!") |
|
|