3 H^ @s2ddljZddlZddad ddZd d d ZdS) NcCsLtj|jd|jddf|jd}d|dddf<||ddddf<|S)zAdd intercept to matrix x. Args: x: 2D NumPy array. Returns: New matrix same as x with 1's in the 0th column. r)dtypeN)npzerosshaper)xZnew_xrJ/home/alex/Documents/10k-hrs-ml/cs229/ps1/stanford/src/linearclass/util.py add_intercepts $r yFc sdd}d}|kr$tdj|t|d}|jjjdWdQRXfd d ttD}fd d ttD}tj |dd |d }tj |dd |d } |j d krtj |d}|r||}|| fS)aWLoad dataset from a CSV file. Args: csv_path: Path to CSV file containing dataset. label_col: Name of column to use as labels (should be 'y' or 't'). add_intercept: Add an intercept entry to x-values. Returns: xs: Numpy array of x-values (inputs). ys: Numpy array of y-values (labels). cSst|S)N)r )rrrr add_intercept_fn"sz&load_dataset..add_intercept_fnr tz#Invalid label_col: {} (expected {})r,Ncsg|]}|jdr|qS)r) startswith).0i)headersrr 1sz load_dataset..csg|]}|kr|qSrr)rr)r label_colrr r2sr) delimiterskiprowsusecols)r r ) ValueErrorformatopenreadlinestripsplitrangelenrloadtxtndim expand_dims) Zcsv_pathrr r Zallowed_label_colsZcsv_fhZx_colsZl_colsinputslabelsr)rrr load_datasets     r'?cCsftjtj||dkdf||dkdfdddtj||dkdf||dkdfdddtjt|dddft|dddfd}|d|d|d|d|tjd|||d }tj||d dd tj|dddfjd |dddfjd tj |dddfjd |dddfjd tj d tj d tj |dS)aHPlot dataset and fitted logistic regression parameters. Args: x: Matrix of training examples, one per row. y: Vector of labels in {0, 1}. theta: Vector of parameters for logistic regression model. save_path: Path to save the plot. correction: Correction factor to apply, if any. rbx) linewidthrZgoNg{Gz?red)cr+g?x1x2rr0rr0r0r0r0rr) pltfigureplotrarangeminmaxlogZxlimZylimZxlabelZylabelZsavefig)rr theta save_pathZ correctionr.r/rrr r3?s **.@44  r3)r F)r()Zmatplotlib.pyplotZpyplotr1numpyrr r'r3rrrr s  *