from sklearn.preprocessing import StandardScaler, LabelEncoder from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt from lassonet import LassoNetClassifier, plot_path X, y = fetch_openml(name="miceprotein", return_X_y=True) # Fill missing values with the mean X = SimpleImputer().fit_transform(X) # Convert labels to scalar y = LabelEncoder().fit_transform(y) # standardize X = StandardScaler().fit_transform(X) X_train, X_test, y_train, y_test = train_test_split(X, y) model = LassoNetClassifier(verbose=True) path = model.path(X_train, y_train) plot_path(model, path, X_test, y_test) plt.savefig("miceprotein.png") model = LassoNetClassifier(batch_size=64, backtrack=True, verbose=True) path = model.path(X_train, y_train) plot_path(model, path, X_test, y_test) plt.savefig("miceprotein_backtrack_64.png")
y_val = np.asarray(y_val) y_val = np.reshape(y_val, (-1, 1)) # (5, 270) for clas, epochi in enumerate(train_epochs): Xi, y = slide_epochs(epochi, clas, wind, stride) assert Xi.shape[0] == len(y) X_train.append(Xi) y_train.append(y) X_train = np.concatenate(X_train, axis=0) # (1300, 63, 500) y_train = np.asarray(y_train) y_train = np.reshape(y_train, (-1, 1)) # (5, 270) chn_num = X_train.shape[1] ################## #X_train, X_test, y_train, y_test = train_test_split(X, y) model = LassoNetClassifier(M=30, verbose=True) path = model.path(X_train, y_train) img = model.feature_importances_.reshape(28, 28) plt.title("Feature importance to discriminate 5 and 6") plt.imshow(img) plt.colorbar() plt.savefig("mnist-classification-importance.png") n_selected = [] accuracy = [] lambda_ = [] for save in path: model.load(save.state_dict)
y = LabelEncoder().fit_transform(y) # standardize X = StandardScaler().fit_transform(X) X_train, X_test, y_train, y_test = train_test_split(X, y) model = LassoNetClassifierCV() model.path(X_train, y_train) print("Best model scored", model.score(X_test, y_test)) print("Lambda =", model.best_lambda_) plot_cv(model, X_test, y_test) plt.savefig("miceprotein-cv.png") 1 / 0 model = LassoNetClassifier() path = model.path(X_train, y_train) plot_path(model, path, X_test, y_test) plt.savefig("miceprotein.png") model = LassoNetClassifier(dropout=0.5) path = model.path(X_train, y_train) plot_path(model, path, X_test, y_test) plt.savefig("miceprotein_dropout.png") model = LassoNetClassifier(hidden_dims=(100, 100)) path = model.path(X_train, y_train) plot_path(model, path, X_test, y_test) plt.savefig("miceprotein_deep.png") model = LassoNetClassifier(hidden_dims=(100, 100), gamma=0.01)
def test_classifier(): X, y = load_digits(return_X_y=True) model = LassoNetClassifier() model.fit(X, y) model.score(X, y)
import matplotlib.pyplot as plt from sklearn.metrics import accuracy_score from sklearn.model_selection import train_test_split from data import get_mnist, get_cifar from lassonet import LassoNetClassifier train_loader, val_loader, test_loader = get_cifar(train=True, batch_size=64, val_size=.1, flatten=True) print("Data Loaded.") model = LassoNetClassifier(M=30, verbose=True, hidden_dims=(100,), n_iters=(1000, 100), patience=(10, 5), lambda_start=5e2, path_multiplier=1.03) path = model.path((train_loader, val_loader), stochastic=True, verboseEpochs=True, iterationsPerEpoch=100) img = model.feature_importances_.reshape(3, 32, 32).mean(0) img = (img * 1.0 / (img.max())) plt.title("Feature importance.") plt.imshow(img) plt.colorbar() plt.savefig("cifar-classification-importance.png") n_selected = [] accuracy = [] lambda_ = [] for save in path: model.load(save.state_dict) y_pred, y_true = model.predict(test_loader, stochastic=True) n_selected.append(save.selected.sum()) accuracy.append(accuracy_score(y_true, y_pred)) lambda_.append(save.lambda_)
def load_mice(): df = pd.read_csv("Data_Cortex_Nuclear.csv") y = list(df[df.columns[78:81]].itertuples(False)) classes = {lbl: i for i, lbl in enumerate(sorted(set(y)))} y = np.array([classes[lbl] for lbl in y]) feats = df.columns[1:78] X = df[feats].fillna(df.groupby(y)[feats].transform("mean")).values X = MinMaxScaler(feature_range=(0, 1)).fit_transform(X) return X, y X, y = load_mice() X_train, X_test, y_train, y_test = train_test_split(X, y) model = LassoNetClassifier(eps=1e-3, n_lambdas=1000) path = model.path(X_train, y_train) n_selected = [] accuracy = [] for save in path: model.load(save.state_dict) n_selected.append(save.selected.sum()) y_pred = model.predict(X_test) accuracy.append(accuracy_score(y_test, y_pred)) fig = plt.figure(figsize=(9, 6)) plt.grid(True) plt.plot(n_selected, accuracy, "o-") plt.xlabel("number of selected features")
This dataset consists of protein expression levels measured in the cortex of normal and trisomic mice who had been exposed to different experimental conditions. Each feature is the expression level of one protein. """ from sklearn.datasets import fetch_openml from sklearn.impute import SimpleImputer from sklearn.preprocessing import StandardScaler, LabelEncoder from sklearn.model_selection import train_test_split import matplotlib.pyplot as plt from lassonet import LassoNetClassifier, plot_path X, y = fetch_openml(name="miceprotein", return_X_y=True) # Fill missing values with the mean X = SimpleImputer().fit_transform(X) # Convert labels to scalar y = LabelEncoder().fit_transform(y) # standardize X = StandardScaler().fit_transform(X) X_train, X_test, y_train, y_test = train_test_split(X, y) model = LassoNetClassifier(verbose=True) path = model.path(X_train, y_train) plot_path(model, path, X_test, y_test) plt.savefig("miceprotein.png")
import matplotlib.pyplot as plt from sklearn.metrics import accuracy_score from sklearn.model_selection import train_test_split from data import get_mnist from lassonet import LassoNetClassifier X, y = get_mnist(['1', '4']) X_train, X_test, y_train, y_test = train_test_split(X, y) model = LassoNetClassifier(M=30, verbose=True, hidden_dims=(100, ), lambda_start=7e+2) path = model.path((X_train, y_train), stochastic=False) img = model.feature_importances_.reshape(28, 28) plt.title("Feature importance.") plt.imshow(img) plt.colorbar() plt.savefig("mnist-classification-importance.png") n_selected = [] accuracy = [] lambda_ = [] for save in path: model.load(save.state_dict) y_pred = model.predict(X_test, stochastic=False)