Ejemplo n.º 1
0
########################################################################
##================ Part 0: Reading data and plotting ==================#
########################################################################

import pandas as pd
import numpy as np

data = pd.read_csv('ex2data2.txt')
X = np.vstack([data.x1,data.x2]).T
y = data.y

import matplotlib.pyplot as plt
import plot_utils

print 'Plotting data with green circle indicating (y=1) examples and red circle indicating (y=0) examples ...'
plot_utils.plot_twoclass_data(X,y,'Chip Test 1', 'Chip Test 2',['y=0','y=1'])
plt.savefig('fig3.pdf')


########################################################################
##================ Part 1: Compute cost and gradient ==================#
########################################################################

# map the features in ex2data2.txt into a pth order polynomial

import sklearn
from sklearn.preprocessing import PolynomialFeatures

# Map X onto polynomial features and normalize

p = 6
Ejemplo n.º 2
0
########################################################################
#  Unregularized logistic regression
########################################################################
##================ Part 0: Reading data and plotting ==================#
########################################################################

data = pd.read_csv('ex1data1.txt')
X = np.vstack([data.x1,data.x2]).T
y = data.y

import matplotlib.pyplot as plt
import plot_utils

print 'Plotting data with green circle indicating (y=1) examples and red circle indicating (y=0) examples ...'
plot_utils.plot_twoclass_data(X,y,'Exam 1 score', 'Exam 2 score',['Not Admitted','Admitted'])
plt.savefig('fig1.pdf')

########################################################################
##================ Part 1: Compute cost and gradient ==================#
########################################################################

# set up the X matrix with the column of ones as intercept

XX = np.vstack([np.ones((X.shape[0],)),X.T]).T

# set up a logistic regression model

log_reg1 = LogisticRegressor()

# test the loss and gradient function