Skip to content
Snippets Groups Projects
Commit 9d503f5c authored by Bridget Tripp's avatar Bridget Tripp
Browse files

Upload New File

parent 100d01d8
Branches
No related tags found
No related merge requests found
%% Cell type:code id: tags:
``` python
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import GridSearchCV
from sklearn.pipeline import Pipeline
from sklearn.svm import LinearSVC
from sklearn import preprocessing
from sklearn.decomposition import PCA, NMF
from sklearn.metrics import average_precision_score
```
%% Cell type:code id: tags:
``` python
soma_data = pd.read_csv('preop_soma_all.csv', header=None).T
np_data = np.array(soma_data)
soma_data.columns = np_data[2]
#y = soma_data.iloc[13:len(soma_data),1]
y = [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1]
X = soma_data.iloc[13:soma_data.shape[0],2:soma_data.shape[1]]
X = preprocessing.normalize(X, norm='l2', axis=0)
```
%% Cell type:code id: tags:
``` python
#partition data into test and train
from sklearn.cross_validation import train_test_split
X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.25, random_state=0)
```
%% Output
/home/otu/btripp2/.conda/envs/py36/lib/python3.6/site-packages/sklearn/cross_validation.py:41: DeprecationWarning: This module was deprecated in version 0.18 in favor of the model_selection module into which all the refactored classes and functions are moved. Also note that the interface of the new CV iterators are different from that of this module. This module will be removed in 0.20.
"This module will be removed in 0.20.", DeprecationWarning)
%% Cell type:code id: tags:
``` python
pipe = Pipeline([
('reduce_dim', PCA()),
('classify', LinearSVC())
])
N_FEATURES_OPTIONS = [26] #[25, 50, 75, 100]
C_OPTIONS = [(n) for n in range(1,6)]
LOSS_OPTIONS = ['squared_hinge','hinge']
param_grid = [
{
'reduce_dim': [PCA(iterated_power="auto")],
'reduce_dim__n_components': N_FEATURES_OPTIONS,
'classify__C': C_OPTIONS,
'classify__loss': LOSS_OPTIONS
},
]
reducer_labels = ['PCA']
grid = GridSearchCV(pipe, cv=5, n_jobs=1, param_grid=param_grid, refit='AUC')
grid.fit(X_train, y_train)
print(grid.predict(X_test))
print(y_test)
print('Score: {0:0.2f}'.format(grid.score(X_test,y_test)))
y_score = grid.decision_function(X_test)
average_precision = average_precision_score(y_test, y_score)
print('Average precision-recall score: {0:0.2f}'.format(
average_precision))
```
%% Output
[1 0 0 1 1 1 0 0 0]
[1, 1, 0, 1, 1, 0, 0, 0, 0]
Score: 0.78
Average precision-recall score: 0.59
%% Cell type:code id: tags:
``` python
#grid.get_params().keys()
print(grid.best_estimator_)
#cv_dict = grid.cv_results_
```
%% Output
Pipeline(memory=None,
steps=[('reduce_dim', PCA(copy=True, iterated_power='auto', n_components=26, random_state=None,
svd_solver='auto', tol=0.0, whiten=False)), ('classify', LinearSVC(C=1, class_weight=None, dual=True, fit_intercept=True,
intercept_scaling=1, loss='squared_hinge', max_iter=1000,
multi_class='ovr', penalty='l2', random_state=None, tol=0.0001,
verbose=0))])
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment