Skip to content

Instantly share code, notes, and snippets.

Last active May 3, 2022 08:14
Show Gist options
  • Save rohithteja/1b017bbd9e0bb17eb528ee3177fb0e49 to your computer and use it in GitHub Desktop.
Save rohithteja/1b017bbd9e0bb17eb528ee3177fb0e49 to your computer and use it in GitHub Desktop.
Argparse wrapper
from argparse import ArgumentDefaultsHelpFormatter, ArgumentParser
def parse_args():
parser = ArgumentParser(formatter_class=ArgumentDefaultsHelpFormatter,
parser.add_argument('--dim_red_type', default='pca', choices=[
'pca','lle'], help='The dim red. types')
parser.add_argument('--n_comp', default=10,type=int, choices=[
5,10], help='output dimensions')
parser.add_argument('--classifier', default='lr', choices=[
'lr','svc','rf'], help='Classifiers')
args = parser.parse_args()
return args
def main(args):
X, y = make_classification(n_samples=1000, n_features=30,
n_informative=15, n_redundant=15,
X_train,X_test,y_train,y_test = train_test_split(X, y,stratify=y,
# dimensionality reduction
def dim_reduction(X_train,X_test,dim_red_type,n_comp):
if dim_red_type == 'pca':
dim_red = PCA(n_components=n_comp)
elif dim_red_type == 'lle':
dim_red = LocallyLinearEmbedding(n_components=n_comp)
X_train_dim = dim_red.transform(X_train)
X_test_dim = dim_red.transform(X_test)
return X_train_dim, X_test_dim
# model training and eval
def train(classifier,X_train,y_train,X_test,y_test):
if classifier == 'lr':
clf = LogisticRegression()
elif classifier == 'svc':
clf = SVC()
elif classifier == 'rf':
clf = RandomForestClassifier(),y_train)
y_pred = clf.predict(X_test)
acc_score = accuracy_score(y_test,y_pred).round(3)
return acc_score * 100
X_train, X_test = dim_reduction(X_train,X_test,'lle',2)
acc_score = train('lr',X_train,y_train,X_test,y_test)
def more_main():
args = parse_args()
if __name__ == "__main__":
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment