File size: 1,238 Bytes
d4d48c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import pandas as pd
from sklearn.model_selection import train_test_split

def load_data(file_path):
    """
    Load dataset from a CSV file.
    
    Args:
        file_path (str): Path to the CSV file.
        
    Returns:
        pd.DataFrame: Loaded dataset.
    """
    return pd.read_csv(file_path)

def preprocess_data(df):
    """
    Preprocess the dataset by handling missing values and encoding categorical variables.
    
    Args:
        df (pd.DataFrame): Raw dataset.
        
    Returns:
        pd.DataFrame: Preprocessed dataset.
    """
    # Handle missing values
    df = df.dropna()
    
    # Encode categorical variables
    df = pd.get_dummies(df)
    
    return df

def split_data(df, target_column, test_size=0.2):
    """
    Split the dataset into training and testing sets.
    
    Args:
        df (pd.DataFrame): Preprocessed dataset.
        target_column (str): Name of the target column.
        test_size (float): Proportion of the dataset to include in the test split.
        
    Returns:
        X_train, X_test, y_train, y_test: Split datasets.
    """
    X = df.drop(columns=[target_column])
    y = df[target_column]
    return train_test_split(X, y, test_size=test_size, random_state=42)