# General libraries import os import numpy as np # Other functions from warmUpExercise import * from plotData import * from computeCost import * from gradientDescent import * from predic import * ## ======== Part 1: Basic Function ======== ## # Complete warmUpExercise print('Runing warmUpExercise...\n') print('5x5 Identity Matrix: \n') print(warmUpExercise()) print('\n') ## ======== Part 2: Plotting ======== ## f = open('../Data/ex1data1.txt') content = f.readlines() f.close() X = [] y = [] m = len(content) for line in content: line = line.replace('\n', '') x_, y_ = line.split(',') X.append(float(x_)) y.append(float(y_))
from warmUpExercise import * from featureNormalize import * from normalEqn import * import os os.chdir('/home/morena/MachineLearning/AndrewNg_Python/2.LinearRegression/machine-learning-ex1/ex1') # Inputs to the functions 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
from mpl_toolkits.mplot3d import Axes3D from warmUpExercise import * from plotData import * from computeCost import * from gradientDescent import * def pause(): programPause = input('Program paused. Press enter to continue.') ## ==================== Part 1: Basic Function ==================== # refer to warmUpExercise.py print('Running warmUpExercise ... ') Matrix = warmUpExercise() print('5x5 Identity Matrix: ') print(Matrix) pause() ## ======================= Part 2: Plotting ======================= print('Plotting Data ...') data = loadtxt('ex1data1.txt', delimiter=',') X = data[:, 0] y = data[:, 1] m = len(y) # number of training examples # Plot Data # Note: refer to plotData.py plotData(X, y)
# Machine Learning Online Class - Exercise 1: Linear Regression from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt import numpy as np import pandas as pd from gradientDescent import * from computeCost import * from plotData import * from warmUpExercise import * # ==================== Part 1: Basic Function ==================== # Complete warmUpExercise.py print('\nRunning warmUpExercise ... \n') print('5x5 Identity Matrix: \n') warmUpExercise(5) input('\nProgram paused. Press Enter to continue.\n') # ======================= Part 2: Plotting ======================= print('Plotting Data ...\n') data = pd.read_csv('ex1data1.txt', header=None) X = np.array(data[0]) y = np.array(data[1]) m = len(y) # number of training examples # Plot Data plotData(X, y)
def testWarmUp(self): '''trying to compare arrays but see http://stackoverflow.com/questions/1322380/gotchas-where-numpy-differs-from-straight-python and http://stackoverflow.com/questions/3302949/whats-the-best-way-to-assert-for-scipy-array-equality''' assert_array_equal(warmUpExercise(), array([])) self.assertTrue((warmUpExercise() == array([])).all())
from bokeh.plotting import figure, output_notebook, show, output_file from decimal import * # Importing own modules import warmUpExercise import plotData import computeCost # ==================== Part 1: Basic Function ==================== # Complete warmUpExercise.py print('Running warmUpExercise function ....\n') print('5x5 Identity Matrix: \n') # Calling warmUpExercise() from file warmUpExercise.py print warmUpExercise(5) # Script is paused. User should press Enter in order to continue raw_input('Program paused. Press <Enter> to continue.\n') # ======================= Part 2: Plotting ======================= print('Plotting Data ...\n') # Loading data from a textfile data = np.loadtxt('../DATA/ex1data1.txt', delimiter=",") # Assigning different columns of data to variables X and y # X --> the population of a city and the second column is # y --> the prfit of a food truck in that city X = data[:, 0]
# import pdb ## Initialization from warmUpExercise import * from plotData import * from computeCost import * from gradientDescent import * from mpl_toolkits.mplot3d import axes3d, Axes3D # is there any equivalent to "clear all; close all; clc"? ## ==================== Part 1: Basic Function ==================== # Complete warmUpExercise.py print 'Running warmUpExercise ... ' print '5x5 Identity Matrix: ' print warmUpExercise() print('Program paused. Press enter to continue.') raw_input() ## ======================= Part 2: Plotting ======================= print 'Plotting Data ...' data = loadtxt('ex1data1.txt') X = data[:, 0]; y = data[:, 1] m = len(y) # number of training examples # Plot Data # Note: You have to complete the code in plotData.m firstPlot = plotData(X, y) firstPlot.show()