Andrew Ng’s Machine Learning Course in Python (Linear Regression)

(https://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html)plt.plot(J_history)plt.xlabel("Iteration")plt.ylabel("$J(Theta)$")plt.title("Cost function using Gradient Descent")Plotting the cost function against the number of iterations gave a nice descending trend, indicating that the gradient descent implementation works in reducing the cost functionNow with that optimized Θ values, I will plot the graph together with the predicted values (the line of best fit)plt.scatter(data[0],data[1])x_value=[x for x in range(25)]y_value=[y*theta[1]+theta[0] for y in x_value]plt.plot(x_value,y_value,color="r")plt.xticks(np.arange(5,30,step=5))plt.yticks(np.arange(-5,30,step=5))plt.xlabel("Population of City (10,000s)")plt.ylabel("Profit ($10,000")plt.title("Profit vs Population")Again, might not be the best way to generate a line based on Θ, let me know if there is a better way of doing soThe last part of the assignment involved making predictions based on your modeldef predict(x,theta): """ Takes in numpy array of x and theta and return the predicted value of y based on theta """ predictions= np.dot(theta.transpose(),x) return predictions[0]predict1=predict(np.array([1,3.5]),theta)*10000print("For population = 35,000, we predict a profit of $"+str(round(predict1,0)))The print statement print: For population = 35,000, we predict a profit of $4520.0predict2=predict(np.array([1,7]),theta)*10000print("For population = 70,000, we predict a profit of $"+str(round(predict2,0)))The print statement print: For population = 70,000, we predict a profit of $45342.0Machine Learning – Andrew NgNow on to multivariate linear regression using the dataset ex1data2.txtAs with all datasets, I started off by loading the data and looking into the datadata2=pd.read_csv("Multi_linear.txt", header=None)data2.head()data2.describe()As you can see, now there are 2 features for X, making it a multivariate problem# Create 2 subplot, 1 for each variablefig, axes = plt.subplots(figsize=(12,4),nrows=1,ncols=2)axes[0].scatter(data2[0],data2[2],color="b")axes[0].set_xlabel("Size (Square Feet)")axes[0].set_ylabel("Prices")axes[0].set_title("House prices against size of house")axes[1].scatter(data2[1],data2[2],color="r")axes[1].set_xlabel("Number of bedroom")axes[1].set_ylabel("Prices")axes[1].set_xticks(np.arange(1,6,step=1))axes[1].set_title("House prices against number of bedroom")# Enhance layoutplt.tight_layout()Plotting the price against each feature shows the relationship between them..Just by looking at the plot, we should expect some degree of positive correlation between the dependent and the independent variables.For multivariable problem optimizing using gradient descent, feature normalization is required to speed up the optimizing process.def featureNormalization(X): """ Take in numpy array of X values and return normalize X values, the mean and standard deviation of each feature """ mean=np.mean(X,axis=0) std=np.std(X,axis=0) X_norm = (X – mean)/std return X_norm , mean , stddata_n2=data2.valuesm2=len(data_n2[:,-1])X2=data_n2[:,0:2].reshape(m2,2)X2, mean_X2, std_X2 = featureNormalization(X2)X2 = np.append(np.ones((m2,1)),X2,axis=1)y2=data_n2[:,-1].reshape(m2,1)theta2=np.zeros((3,1))Next is to test if our previous functions, computeCost(X, y, theta) and gradientDescent(X, y, theta, alpha, num_iters) work with multiple features inputUsing computeCost(X2,y2,theta2) gives 65591548106.45744 which is the cost of using Θ (0,0,0) as parameterstheta2, J_history2 = gradientDescent(X2,y2,theta2,0.01,400)print("h(x) ="+str(round(theta2[0,0],2))+" + "+str(round(theta2[1,0],2))+"x1 + "+str(round(theta2[2,0],2))+"x2")The print statement print: h(x) =334302.06 + 99411.45×1 + 3267.01×2 ,which is the optimized Θ values round to 2 decimals placesplt.plot(J_history2)plt.xlabel("Iteration")plt.ylabel("$J(Theta)$")plt.title("Cost function using Gradient Descent")Plotting the J(Θ) against the number of iterations gives a descending trend, proving that our gradientDescent function works for multivariate cases tooLastly, making predictions using the optimized Θ values for a 1650 square feet house with 3 bedrooms.#feature normalisation of x valuesx_sample = featureNormalization(np.array([1650,3]))[0]x_sample=np.append(np.ones(1),x_sample)predict3=predict(x_sample,theta2)print("For size of house = 1650, Number of bedroom = 3, we predict a house value of $"+str(round(predict3,0)))This print statement print: For size of house = 1650, Number of bedroom = 3, we predict a house value of $430447.0That's all for the first exercise..Hope you enjoy reading it as much as I do writing it..Feel free to leave me some comment on how I can improve..If you want to access the jupyter notebook for this assignment, I had uploaded the code in Github (https://github.com/Benlau93/Machine-Learning-by-Andrew-Ng-in-Python).. More details

Leave a Reply