Rixf123's picture
download
raw
2.57 kB
import requests
import numpy as np
IRIS_URL = "https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data"
def load_iris_from_uci():
r = requests.get(IRIS_URL, timeout=10)
r.raise_for_status()
txt = r.text.strip()
data = []
labels = []
for line in txt.splitlines():
if not line:
continue
parts = line.split(',')
if len(parts) != 5:
continue
features = list(map(float, parts[:4]))
label = parts[4]
data.append(features)
labels.append(label)
X = np.array(data, dtype=float)
uniq = sorted(set(labels))
label_to_idx = {label: idx for idx, label in enumerate(uniq)}
y = np.array([label_to_idx[label] for label in labels], dtype=int)
return X, y
def one_hot(y, num_classes=None):
if num_classes is None:
num_classes = int(np.max(y)) + 1
out = np.zeros((y.shape[0], num_classes), dtype=float)
out[np.arange(y.shape[0]), y] = 1.0
return out
def train_test_split(X, y, test_size=0.2, seed=42):
rng = np.random.RandomState(seed)
n = X.shape[0]
perm = rng.permutation(n)
split = int(n * (1 - test_size))
return X[perm[:split]], X[perm[split:]], y[perm[:split]], y[perm[split:]]
def prepare_iris(test_size=0.2, val_size=0.1, seed=42):
X, y = load_iris_from_uci()
X_mean = X.mean(axis=0, keepdims=True)
X_std = X.std(axis=0, keepdims=True) + 1e-9
X = (X - X_mean) / X_std
if not 0.0 <= val_size < 1.0:
raise ValueError("val_size must be between 0 and 1")
if not 0.0 <= test_size < 1.0:
raise ValueError("test_size must be between 0 and 1")
if val_size + test_size >= 1.0:
raise ValueError("test_size + val_size must be less than 1")
n = X.shape[0]
rng = np.random.RandomState(seed)
perm = rng.permutation(n)
split_val = int(n * (1 - test_size - val_size))
split_test = int(n * (1 - test_size))
X_train = X[perm[:split_val]]
X_val = X[perm[split_val:split_test]] if val_size > 0 else np.empty((0, X.shape[1]), dtype=float)
X_test = X[perm[split_test:]]
y_train = y[perm[:split_val]]
y_val = y[perm[split_val:split_test]] if val_size > 0 else np.empty((0,), dtype=int)
y_test = y[perm[split_test:]]
Y_train = one_hot(y_train)
Y_val = one_hot(y_val, num_classes=Y_train.shape[1]) if val_size > 0 else np.empty((0, Y_train.shape[1]), dtype=float)
Y_test = one_hot(y_test, num_classes=Y_train.shape[1])
return X_train, X_val, X_test, Y_train, Y_val, Y_test, y_train, y_val, y_test

Xet Storage Details

Size:
2.57 kB
·
Xet hash:
f25114cffcfc5ba268cdf0454ad01b943d0e3c36de6fe509a76308f79b60ff78

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.