-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
57 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
code/stat159lambda/classification/svm/svm_cross_validate.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
from __future__ import print_function, division | ||
import numpy as np | ||
from sklearn.cross_validation import KFold | ||
from sklearn.metrics import accuracy_score | ||
from stat159lambda.classification import design_matrix as dm | ||
from stat159lambda.classification.svm import svm | ||
from stat159lambda.classification import partition_volumes as pv | ||
from stat159lambda.config import REPO_HOME_PATH, NUM_VOXELS | ||
from stat159lambda.linear_modeling import linear_modeling as lm | ||
from stat159lambda.utils import data_path as dp | ||
|
||
|
||
voxels_sorted_by_t_statistic = lm.VoxelExtractor(1, 'int-ext').t_stat() | ||
# num_features_values = range(100, NUM_VOXELS/100, 100) | ||
# for num_features in num_features_values: | ||
voxel_feature_indices = voxels_sorted_by_t_statistic[:1000] | ||
design_matrix = dm.DesignMatrix(dp.get_smoothed_2d_path(1, 4), | ||
pv.get_train_indices(), voxel_feature_indices) | ||
|
||
X_train = design_matrix.get_design_matrix() | ||
y_train = np.array(design_matrix.get_labels()) | ||
|
||
cv_accuracies = [] | ||
for train, test in KFold(len(X_train), 5): | ||
X_cv_train = X_train[train, :] | ||
y_cv_train = y_train[train] | ||
X_cv_test = X_train[test, :] | ||
y_cv_test = y_train[test] | ||
model = svm.Classifier(X_cv_train, y_cv_train) | ||
model.train() | ||
y_predicted = model.predict(X_cv_test) | ||
cv_accuracies.append(accuracy_score(y_predicted, y_cv_test)) | ||
|
||
print(np.mean(cv_accuracies)) | ||
|