Spaces:
Configuration error
Configuration error
Create preprocess.py
Browse files- preprocess.py +52 -0
preprocess.py
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import argparse
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from sklearn.model_selection import train_test_split
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
def parse(csv_path):
|
| 9 |
+
print(f"Location of the file: {csv_path}")
|
| 10 |
+
|
| 11 |
+
# Step 1: Load the dataset
|
| 12 |
+
# file_path = "dataset.csv" # Path to the original dataset
|
| 13 |
+
data = pd.read_csv(csv_path)
|
| 14 |
+
|
| 15 |
+
# Drop dupes
|
| 16 |
+
data = data.drop_duplicates()
|
| 17 |
+
|
| 18 |
+
# Step 2: Define the feature columns (X) and target column (y)
|
| 19 |
+
X = data[["name", "attendance percentage", "average sleep time", "average screen time"]] # Feature columns
|
| 20 |
+
y = data["grade"] # Target column
|
| 21 |
+
|
| 22 |
+
# Step 3: Split the dataset into training and testing sets
|
| 23 |
+
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
|
| 24 |
+
|
| 25 |
+
# Step 4: Combine X and y back into dataframes for train and test
|
| 26 |
+
train_data = pd.concat([X_train, y_train], axis=1) # Combine features and target for training data
|
| 27 |
+
test_data = pd.concat([X_test, y_test], axis=1) # Combine features and target for testing data
|
| 28 |
+
|
| 29 |
+
# Step 5: Create the 'data' folder if it doesn't exist
|
| 30 |
+
output_folder = "data"
|
| 31 |
+
os.makedirs(output_folder, exist_ok=True)
|
| 32 |
+
|
| 33 |
+
# Step 6: Save the train and test sets as CSV files
|
| 34 |
+
train_file_path = os.path.join(output_folder, "train.csv")
|
| 35 |
+
test_file_path = os.path.join(output_folder, "test.csv")
|
| 36 |
+
|
| 37 |
+
train_data.to_csv(train_file_path, index=False)
|
| 38 |
+
test_data.to_csv(test_file_path, index=False)
|
| 39 |
+
|
| 40 |
+
print(f"Train and test datasets saved in '{output_folder}' folder.")
|
| 41 |
+
|
| 42 |
+
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
if __name__ == '__main__':
|
| 47 |
+
parser = argparse.ArgumentParser()
|
| 48 |
+
parser.add_argument("--csv-path", type=str)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
args = parser.parse_args()
|
| 52 |
+
parse(args.csv_path)
|