-
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.
Merge pull request #78 from AlonDaks/classification
Finished plots and figures for reproduction
- Loading branch information
Showing
20 changed files
with
302 additions
and
367 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
43 changes: 26 additions & 17 deletions
43
code/stat159lambda/classification/random_forest/rf_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 |
---|---|---|
@@ -1,29 +1,38 @@ | ||
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.random_forest import rf | ||
from stat159lambda.classification import partition_volumes as pv | ||
from stat159lambda.config import REPO_HOME_PATH | ||
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 | ||
|
||
design_matrix = dm.DesignMatrix( | ||
'{0}/data/processed/sub1_rcds_2d.npy'.format(REPO_HOME_PATH), | ||
pv.get_train_indices(), range(200)) | ||
|
||
X_train = design_matrix.get_design_matrix() | ||
y_train = np.array(design_matrix.get_labels()) | ||
voxels_sorted_by_t_statistic = lm.VoxelExtractor(1, 'int-ext').t_stat() | ||
num_features_values = [200, 400, 600, 1000, 1500, 2000, 3000, 5000, 10000] | ||
design_matrix = dm.DesignMatrix(dp.get_smoothed_2d_path(1, 4)) | ||
train_volume_indices = pv.get_train_indices() | ||
cv_values = [] | ||
for num_features in num_features_values: | ||
voxel_feature_indices = voxels_sorted_by_t_statistic[:num_features] | ||
|
||
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 = rf.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)) | ||
X_train = design_matrix.get_design_matrix(train_volume_indices, voxel_feature_indices) | ||
y_train = np.array(design_matrix.get_labels(train_volume_indices)) | ||
|
||
print np.mean(cv_accuracies) | ||
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 = rf.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)) | ||
|
||
cv_values.append(np.mean(cv_accuracies)) | ||
|
||
np.save('cv_values', cv_values) | ||
|
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) | ||
|
||
print('classifying') | ||
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)) | ||
|
Binary file not shown.
Binary file not shown.
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
Oops, something went wrong.