-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateData.py
48 lines (39 loc) · 1.49 KB
/
generateData.py
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
import os
import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
# If data folder does not exist, create it
if not os.path.exists('data'):
os.makedirs('data')
def preprocess(img):
thresh = img.mean()
binary = img > thresh
return binary.astype(int)
# Function to save data to a txt file
def save_to_txt(filename, X, y):
with open(filename, 'w') as f:
# Write the number of instances and number of features
f.write(f"{X.shape[0]}\n")
f.write(f"{X.shape[1]}\n")
# Write the data
for i in range(X.shape[0]):
features = ' '.join(map(str, X[i]))
f.write(f"{features} {y[i]}\n")
# Load the breast cancer dataset
data = load_digits(n_class = 2)
X, y = data.data, data.target
# Preprocess the data
X = np.array([preprocess(x.reshape(8, 8)).flatten() for x in X])
# Split the dataset into training and testing sets (stratified)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.2, random_state = 42, stratify = y)
# Save the train and test sets to txt files
save_to_txt('data/train.txt', X_train, y_train)
save_to_txt('data/test.txt', X_test, y_test)
print("Data saved successfully!")
# Train a logistic regression model as a baseline
model = LogisticRegression()
model.fit(X_train, y_train)
# Evaluate the model
score = model.score(X_test, y_test)
print(f"Baseline Logistic Regression Accuracy: {score}")