예제 #1
0
def __main__():
    '''Perform Linear Regression using gradient descent or normal equations on data provided by user'''
    ###############
    # Obtain Data
    ###############
    print 'Inside main() '
    dataFileName = raw_input('Please enter the filename of data file: ')
    dataDelimiter = raw_input('Please enter the delimiter : ')

    try:
        data = genfromtxt(open(dataFileName, 'r'),
                          delimiter=dataDelimiter,
                          comments='""')  # Directly imports to 'data'
        X = data[:, 0:
                 -1]  # Assuming that the last row of data is the target value
        # and all others are those of X
        y = data[:, [
            -1
        ]]  # Note the single parenthesis to assert that we are choosing
        # one single column
        #        if shape(X)[1] <= 2:
        #            plotdata(X,y)                           # Display data to user if possible

        ################################
        # Perform Normalization
        ################################

        (Xnormalized, meanTable, stdTable) = normalizeData(X)

        Xnormalized = hstack((ones((shape(Xnormalized)[0], 1)), Xnormalized))
        X = hstack((ones((shape(X)[0], 1)), X))  # Add X0 = 1

    except:
        print 'Unable to process data'
        exit(1)  # Exit the program with error code 1

    ###############################
    # Assign Theta, alpha, num_iter
    ###############################

    initialTheta = zeros(
        (shape(X)[1],
         1))  # The number of columns of X is the no. of parameters
    alpha = float(
        raw_input('Enter value of alpha: '))  # Allow user to choose alpha
    numIter = int(
        raw_input('Iterations: '))  # User specified number of iterations

    ###########################
    # Perform Machine Learning
    ###########################

    thetaGradDes = gradientDescent(Xnormalized, y, initialTheta, alpha,
                                   numIter)
    thetaNormalEq = normalEquation(X, y, initialTheta)

    print 'Theta observed using Gradient Descent:', thetaGradDes
    print 'Theta observed using Normal Equations:', thetaNormalEq

    exit(0)
예제 #2
0
def __main__():
    '''Perform Linear Regression using gradient descent or normal equations on data provided by user'''
    ###############
    # Obtain Data
    ###############
    print 'Inside main() '
    dataFileName = raw_input('Please enter the filename of data file: ')
    dataDelimiter    = raw_input('Please enter the delimiter : ')
    
    try:
        data = genfromtxt(open(dataFileName, 'r') ,delimiter = dataDelimiter, comments= '""') # Directly imports to 'data' 
        X = data[:,0:-1]                            # Assuming that the last row of data is the target value
                                                    # and all others are those of X
        y = data[:,[-1]]                            # Note the single parenthesis to assert that we are choosing
                                                    # one single column
#        if shape(X)[1] <= 2:
#            plotdata(X,y)                           # Display data to user if possible

        ################################
        # Perform Normalization
        ################################

        (Xnormalized, meanTable,stdTable) = normalizeData(X)
        
        Xnormalized = hstack((ones((shape(Xnormalized)[0], 1)), Xnormalized)) 
        X = hstack((ones((shape(X)[0], 1)), X))                  # Add X0 = 1 

    except:
        print 'Unable to process data'
        exit(1)                                     # Exit the program with error code 1

    ###############################
    # Assign Theta, alpha, num_iter
    ###############################  
    
    initialTheta = zeros((shape(X)[1],1))              # The number of columns of X is the no. of parameters
    alpha = float(raw_input('Enter value of alpha: ')) # Allow user to choose alpha
    numIter = int(raw_input('Iterations: '))          # User specified number of iterations
    
    ###########################
    # Perform Machine Learning
    ###########################
    
    thetaGradDes  = gradientDescent(Xnormalized,y,initialTheta, alpha, numIter) 
    thetaNormalEq = normalEquation(X,y,initialTheta)
    
    print 'Theta observed using Gradient Descent:', thetaGradDes
    print 'Theta observed using Normal Equations:', thetaNormalEq

    exit(0)
예제 #3
0
theta = [0, 0]
print '\nTesting the cost function...'
J = computeCost(x, y, theta)
print 'With theta = [0 , 0]\nCost computed = ' + str(J)
print 'Expected cost value (approx) 32.07'

theta = [-1, 2]
J = computeCost(x, y, theta)
print '\nWith theta = [-1 , 2]\nCost computed = ' + str(J)
print 'Expected cost value (approx): 54.24'

print '\nProgram paused. Press enter to continue.'
wait = raw_input()

print '\nRunning gradient descent...'
theta = gradientDescent(x, y, theta, alpha, iters)
print 'Theta found by gradient descent: ' + str(theta)
print 'Expected theta values (approx): [-3.6303, 1.1664]'

print '\nProgram paused. Exit plot to continue.'

# calculating values of y according to the theta found from using gradient descent so that we can plot our linear
# regression model
y1 = []
for i in range(len(x)):
    y1.append(hyp(x[i], theta))
# plotting the linear regression model
plt.plot(x, y, 'r+', x, y1, '-')
plt.xlabel('Population of City in 10,000s')
plt.ylabel('Profit in $10,000s')
plt.show()
예제 #4
0
X1 = np.transpose(
    np.array([np.ones(20), np.exp(1) + np.exp(2) * np.arange(0.1, 2.1, 0.1)]))
Y1 = np.transpose(np.array([X1[:, 1]+np.sin(X1[:, 0])+np.cos(X1[:, 1])]))
X2 = np.transpose(np.array([X1[:, 1]**0.5, X1[:, 1]**0.25]))
X2 = np.concatenate((X1, X2), axis=1)
Y2 = np.array(Y1**0.5 + Y1)

# WarmUpExercise
print('Print WarmUpExercise:\n{}'.format(warmUpExercise(5)))

# computeCost with one variable
print('Print computeCost with one variable:\n{}'.format(computeCost(X1, Y1.transpose(),
                                                                    np.array([0.5, -0.5]).transpose())))

# gradientDescent with one variable
(theta, J_history) = gradientDescent(
    X1, Y1[:, 0], np.array([0.5, -0.5]).transpose(), 0.01, 10)
print('theta_single = {}, J_history_single = {}'.format(theta, J_history))

# Feature Normalization
[X_norm, mu, sigma] = featureNormalize(X2[:, 1:3])
print('X_norm:\n{}\nmu:{}\nsigma{}'.format(X_norm, mu, sigma))

# computeCost with multiple variables
costMulti = computeCost(X2, Y2.transpose(), np.array(
    [0.1, 0.2, 0.3, 0.4]).transpose())
print('Print computeCost with multiple variables\n{}'.format(costMulti))

# gradientDescent with multiple variables
(theta_multi, J_history_multi) = gradientDescent(
    X2, Y2[:, 0], np.array([-0.1, -0.2, -0.3, -0.4]).transpose(), 0.01, 10)
print('theta_multi = {}, J_history_multi = {}'.format(
예제 #5
0
print('Testing the cost function ...')
# compute and display initial cost refer to computeCost
J = computeCost(X, y, theta)
print('With theta = [0  0]\nCost computed = {0:.2f}'.format(J))
print('Expected cost value (approx) 32.07')
pause()
# further testing of the cost function
J = computeCost(X, y, np.array([[-1], [2]]))
print('With theta = [-1  2]\nCost computed = {0:.2f}'.format(J))
print('Expected cost value (approx) 54.24')

print('Program paused. Press enter to continue.')
print('Running Gradient Descent ...')

# run gradient descent refer to gradientDescent
theta, J = gradientDescent(X, y, theta, alpha, iterations)

# print theta to screen
print('Theta found by gradient descent:')
print('{0:.4f} \n {1:.4f}'.format(theta[0][0], theta[1][0]))
print('Expected theta values (approx)')
print(' -3.6303\n  1.1664')

# Plot the linear fit
plt.plot(X[:, 1], X.dot(theta), 'r-')
plt.legend(['Training data', 'Linear regression'])
plt.show()
pause()

# Predict values for population sizes of 35,000 and 70,000
predict1 = np.array([1, 3.5]).dot(theta)
steps of about 3 times the previous value (i.e., 0.3, 0.1, 0.03, 0.01 and so on).
'''

# Initiate list where to store the cost function for different alpha
num_iters = 50
J_history_alpha = []

alpha = [0.3, 0.1, 0.03, 0.01]
colors = ['k', 'r', 'g', 'b']

for i in alpha:

    # Init Theta and Run Gradient Descent
    theta = np.zeros((X_norm.shape[1], 1))
    y = np.array(data[2])
    (theta, J_history) = gradientDescent(X_norm, y, theta, i, num_iters)
    J_history_alpha.append(J_history)

# Plot the convergence graph
j = len(colors)
for j, colors in enumerate(colors):
    plt.plot(J_history_alpha[j],
             color=colors,
             label=r'$\alpha = %.2f$' % alpha[j])

plt.xlabel('Number of iterations')
plt.ylabel('Cost J')
plt.title('Convergence of gradient descent with an appropriate learning rate',
          y=1.05)
plt.legend()
plt.xticks(range(0, 51, 5))
예제 #7
0
alpha = 0.01

print('\nTesting the cost function ...\n')

# compute and display initial cost
J = computeCost(X, y, theta)
print("With theta = {}\nCost computed = {}\n".format(theta.transpose(), J))

# further testing of the cost function
J = computeCost(X, y, [[-1], [2]])
print('\nWith theta = [-1 ; 2]\nCost computed = {}\n'.format(J))
print('Program paused. Press enter to continue. \n')

# Calculate gradientDescent
print('\nRunning Gradient Descent ...\n')
(theta, J_history) = gradientDescent(X, y, theta, alpha, iterations)
print('Theta found by gradient descent:\n')
print('{}\n'.format(theta))

# check the cost function trend over iterations (should never increase)
iters = np.arange(1, iterations + 1).reshape(iterations, 1)
plt.plot(J_history, iters)
plt.xlabel('N. Iterations')
plt.ylabel('J(θ) - Cost function')
plt.title('Cost function vs Iteration')
plt.show()

# Plot the linear fit
plt.scatter
plt.plot(X[:, 1], y, 'rx', markersize=10, label='Training data')
plt.xlabel('Population of City in 10,000s')
예제 #8
0
ax.set_xlabel('Exam 1 Score')
ax.set_ylabel('Exam 2 Score')
plt.show()

"""第2部分 逻辑回归"""
data.insert(0, 'Ones', 1)
cols = data.shape[1]
X = data.iloc[:, 0:cols-1]
y = data.iloc[:, cols-1:cols]
X = np.array(X.values)
y = np.array(y.values)
theta = np.zeros(3)
print('initial cost : ' + str(costFunction(theta, X, y)) + ' (This value should be about 0.693)')

"""第3部分 梯度下降"""
print('gradient descent : ' + str(gradientDescent(theta, X, y)))
result = opt.fmin_tnc(func=costFunction, x0=theta, fprime=gradientDescent, args=(X, y))
print('result : ' + str(result))
print('cost : ' + str(costFunction(result[0], X, y)) + ' (This value should be about 0.203)')

"""第4部分 用训练集预测和验证"""
params = np.zeros((X.shape[1], 1)).ravel()
args = (X, y)


def f(params, *args):
    X_train, y_train = args
    m, n = X_train.shape
    J = 0
    theta = params.reshape((n, 1))
    h = sigmoid(np.dot(X_train, theta))
예제 #9
0
df = pd.read_csv('data.csv', header=None)
X, y = df.drop(columns=2).values, df[2].values.reshape(-1, 1)
X, mu, sigma = featureNormalize(X)
y = 2 * y - 1

# Learning Parameters
theta = np.zeros((X.shape[1], 1))
bias = 0
num_iters = 100
C = 1

theta, bias, J_history = gradientDescent(X,
                                         y,
                                         theta,
                                         bias,
                                         num_iters,
                                         alpha=0.001,
                                         C=C)

# Plot input data
plt.figure(figsize=(12, 12))
plt.scatter(X[:, 0], X[:, 1], c=y.tolist(), marker='o')

# Plot decision boundary
plot_x = np.linspace(np.min(X[:, 0]), np.max(X[:, 0]), 100).reshape(-1, 1)
plot_y_1 = (1 / theta[1]) * (1 - (theta[0] * plot_x) - bias)
plot_y_m1 = (1 / theta[1]) * (-1 - (theta[0] * plot_x) - bias)
plot_y_0 = (1 / theta[1]) * (0 - (theta[0] * plot_x) - bias)

plt.plot(plot_x, plot_y_0, color='red', label="Hyperplane WX + B = 0")
예제 #10
0
파일: ex1.py 프로젝트: KWMalik/py-coursera
## =================== Part 3: Gradient descent ===================
print 'Running Gradient Descent ...'

X = hstack((ones((m, 1)), vstack(data[:,0]))) # Add a column of ones to x
theta = zeros((2, 1)) # initialize fitting parameters

# Some gradient descent settings
iterations = 1500
alpha = 0.01

# compute and display initial cost
computeCost(X, y, theta)

# run gradient descent
(theta, J_history) = gradientDescent(X, y, theta, alpha, iterations)

# print theta to screen
print 'Theta found by gradient descent: '
print '%f %f \n' % (theta[0].var(), theta[1].var())

# Plot the linear fit
#hold on; # keep previous plot visible
plot(vstack(X[:,1]), X.dot(theta), '-')
legend(('Training data', 'Linear regression'))
# not sure how to avoid overlaying any more plots on this figure - call figure()?

# Predict values for population sizes of 35,000 and 70,000
# note this it outputting too many times TODO fix this....
predict1 = array([1, 3.5]) *theta
print 'For population = 35,000, we predict a profit of %f\n' % (predict1.var()*10000)
예제 #11
0
#(97, 2) (97, 1) (1, 2)

#计算初始代价函数的值
computeCost(X, y, theta)
#print(computeCost(X,y,theta))
#32.072733877455676 值为当(theta0,theta1) = [0,0]时的代价函数的值

#======================= Part 3: Gradient descent==========

#初始化:学习速率α和要执行的迭代次数epoch
alpha = 0.01
epoch = 1000

#现在让我们运行梯度下降算法来将我们的参数θ适合于训练集
#final_theta是迭代了1000次后得到的最优解【*,*】
final_theta, cost = gradientDescent(X, y, theta, alpha, epoch)

#最后,我们可以使用我们拟合的参数计算训练模型的代价函数(误差)
#32.072733877455676 值为当(theta0,theta1) = [0,0]时的代价函数的值
computeCost(X, y, final_theta)
#print(final_theta)   [[-3.24140214  1.1272942 ]]
#print(computeCost(X,y,final_theta))   4.515955503078912 相较于最开始,的确降低了很多,是不是最优?

#=================== Part 4:在原始数据上 绘制线性模型以及数据===========
x = np.linspace(data.Population.min(), data.Population.max(), 100)
#print(x)
f = final_theta[0, 0] + final_theta[0, 1] * x
#print(f)

fig, ax = plt.subplots(figsize=(20, 15))
ax.plot(x, f, 'r', label='Prediction')
예제 #12
0
num_features = X.shape[1]
m = X.shape[0]

# Normalize the training data
X, mu, sigma = featureNormalize(X)

# Add intercept term to X
X = np.concatenate([np.ones((X.shape[0], 1)), X], axis=1)

# Initialize parameters
theta = np.random.rand(num_features + 1, 1)
alpha = 0.01
num_iters = 2000

# Train the linear regression model
theta, J_history = gradientDescent(X, y, theta, num_iters, alpha)
print(f'Theta values using gradientDescent: {theta}')

# Making Prediction
X_test = np.array([1, (3137 - mu[0]) / sigma[0], (3 - mu[1]) / sigma[1]])
print(f'Actual price for 3137 sq feet area and 3 rooms: 579900')
print(f'Predicted: {np.dot(X_test, theta)}')

# Plot input data
fig = plt.figure(figsize=(10, 7))
ax = plt.axes(projection='3d')
ax.scatter3D(X[:, 1], X[:, 2], y, marker='o')
ax.zaxis.set_major_formatter(
    ticker.FuncFormatter(lambda x, pos: '{:,.0f}'.format(x / 1000) + 'K'))
ax.set_xlabel('Area per Square Feet')
ax.set_ylabel('Rooms')