def output(partId): # Random Test Cases X1 = np.column_stack( (np.ones(20), np.exp(1) + np.exp(2) * np.linspace(0.1, 2, 20))) Y1 = X1[:, 1] + np.sin(X1[:, 0]) + np.cos(X1[:, 1]) X2 = np.column_stack((X1, X1[:, 1]**0.5, X1[:, 1]**0.25)) Y2 = np.power(Y1, 0.5) + Y1 if partId == '1': out = formatter('%0.5f ', warmUpExercise()) elif partId == '2': out = formatter('%0.5f ', computeCost(X1, Y1, np.array([0.5, -0.5]))) elif partId == '3': out = formatter( '%0.5f ', gradientDescent(X1, Y1, np.array([0.5, -0.5]), 0.01, 10)) elif partId == '4': out = formatter('%0.5f ', featureNormalize(X2[:, 1:4])) elif partId == '5': out = formatter( '%0.5f ', computeCostMulti(X2, Y2, np.array([0.1, 0.2, 0.3, 0.4]))) elif partId == '6': out = formatter( '%0.5f ', gradientDescentMulti(X2, Y2, np.array([-0.1, -0.2, -0.3, -0.4]), 0.01, 10)) elif partId == '7': out = formatter('%0.5f ', normalEqn(X2, Y2)) return out
def output(partId): # Random Test Cases X1 = column_stack((ones(20), exp(1) + dot(exp(2), arange(0.1, 2.1, 0.1)))) Y1 = X1[:,1] + sin(X1[:,0]) + cos(X1[:,1]) X2 = column_stack((X1, X1[:,1]**0.5, X1[:,1]**0.25)) Y2 = Y1**0.5 + Y1 if partId == '1': return sprintf('%0.5f ', warmUpExercise()) elif partId == '2': return sprintf('%0.5f ', computeCost(X1, Y1, array([0.5, -0.5]))) elif partId == '3': return sprintf('%0.5f ', gradientDescent(X1, Y1, array([0.5, -0.5]), 0.01, 10)) elif partId == '4': return sprintf('%0.5f ', featureNormalize(X2[:,1:3])); elif partId == '5': return sprintf('%0.5f ', computeCostMulti(X2, Y2, array([0.1, 0.2, 0.3, 0.4]))) elif partId == '6': return sprintf('%0.5f ', gradientDescentMulti(X2, Y2, array([-0.1, -0.2, -0.3, -0.4]), 0.01, 10)) elif partId == '7': return sprintf('%0.5f ', normalEqn(X2, Y2))
# # For this exercise, you will not need to change any code in this file, # or any other files other than those mentioned above. # # x refers to the population size in 10,000s # y refers to the profit in $10,000s # ## Initialization ##clear # close all# clc ## ==================== Part 1: Basic Function ==================== # Complete warmUpExercise.m print('Running warmUpExercise ... \n')# print('5x5 Identity Matrix: \n')# print(warmUpExercise()) print('Program paused. Press enter to continue.\n')# raw_input(">>>") # # # ## ======================= Part 2: Plotting ======================= print('Plotting Data ...\n') data = np.loadtxt('ex1data1.txt',delimiter=',')# 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
import pylab as pl #import 3d axes plots from matpotlib from mpl_toolkits.mplot3d import Axes3D #importing plotData file with the function for ploting the curves and we access it use initials plt import plotData as plt #importing gradientDescent as gD import gradientDescent as gD #importing warmUpExercise file with identity matrix function import warmUpExercise as wmUpEx #import computeCost file with computeCost function and to access it we use initials as cC import computeCost as cC print("Running warmUpExercise ... \n") print("5x5 Identity Matrix: \n") #warmUpExercise to print the identity matrix similar to eye(5) in octaveor matlab A=wmUpEx.warmUpExercise() print(A)#printing the identity matrix print("Program paused press enter to continue ") """%% ======================= Part 2: Plotting ======================= """ #load data located in ex1data1 in a variable called data data=np.loadtxt('ex1data1.txt',delimiter=",") #load data in the first column of variable data in variable X #unlike in matlab in python indexing starts at 0 X = data[:, 0] #load data in second column of variable data in variable y y = data[:, 1] #get the length of y m=len(y)
def testWarmUp(): assert_array_equal(warmUpExercise(), eye(5))
import warmUpExercise as wue import plotData as pd import computeCost as cc import featureNormalize as fn import gradientDescent as gd import normalEq as ne import numpy as np import matplotlib.pyplot as plt print "Running warmUpExercise" print "5*5 identity matrix" wue.warmUpExercise() raw_input("Press Enter to continue.....") print "Plotting Data" pd.plotData() raw_input("Press Enter to continue.....") a=pd.content[0] X=np.ones((20,2)) X[:,1]=a b=pd.content[1] y=np.ones((20,1)) y[:,0]=b theta=np.zeros((2,1)) iterations=100 alpha= 0.01 J=cc.computeCost(X,y,theta) print "Computed Cost: ",J
import numpy as np from plotData import plotData from computeCost import computeCost from gradientDescent import gradientDescent from warmUpExercise import warmUpExercise import matplotlib.pyplot as plt def pause(): input("Press the <ENTER> key to continue...") """## Part 1: Basic Function """ print("Running warmUpExercise ... \n") print("5x5 Identity Matrix: \n") print(warmUpExercise()) print("Program paused. Press enter to continue.\n") pause() """## ======================= Part 2: Plotting """ print("Plotting Data ...\n") data = np.loadtxt('ex1data1.txt', delimiter =",") X = data[:, 0] #x refers to the population size in 10,000s y = data[:, 1] #y refers to the profit in $10,000s m = y.size #umber of training examples y = y.reshape((m,1))
# import pandas as pd import numpy as np # clear close all clc ## ==================== Part 1: Basic Function ==================== # Complete warmUpExercise.py # Readings: https://docs.python.org/3/tutorial/modules.html from warmUpExercise import warmUpExercise print('Running warmUpExercise ... \n') print('5x5 Identity Matrix: \n') warmUpExercise() input('Program paused. Press enter to continue.\n') ## ======================= Part 2: Plotting ======================= print('Plotting Data ...\n') # Readings: https://docs.scipy.org/doc/numpy/reference/generated/numpy.loadtxt.html # https://docs.scipy.org/doc/numpy/reference/generated/numpy.ndarray.copy.html DATA_PATH = "ex1data1.txt" data = np.loadtxt(DATA_PATH, delimiter=',') # Used np.copy here because the original data should be unchanged in case its needed later X = np.copy(data[:, 0])
from matplotlib import cm import pandas as pd import numpy as np from numpy import genfromtxt import warmUpExercise import computeCost import gradientDescent #import plotData as plotData; np.set_printoptions(precision=2) ## ==================== Part 1: Basic Function ==================== # Complete warmUpExercise.m print('Running warmUpExercise ... \n') print('5x5 Identity Matrix: \n') A = warmUpExercise.warmUpExercise() print(A) input("Press the <ENTER> key to continue...") ##======================= Part 2: Plotting ======================= print('Plotting Data ...\n') my_data = genfromtxt( "E:\Private\ML\machine-learning-ex1-python\ex1\ex1data1.txt", delimiter=',') data = pd.read_csv( 'E:\Private\ML\machine-learning-ex1-python\ex1\ex1data1.txt') X = my_data[:, 0] y = my_data[:, 1] m = y.shape # Plot Data
# # INITIALIZE import os import matplotlib.pyplot as plt import pandas as pd import numpy as np os.system('cls' if os.name == 'nt' else 'clear') plt.close("all") ## =================== Part 1: Basic Function =================== from warmUpExercise import warmUpExercise print('A basic function.') print(' - A 5x5 Identity Matrix: \n') # FILE: warmUpExercise.py print(warmUpExercise(), '\n') input('Paused. Press enter to continue.\n') ## =================== Part 2: Plotting ==================== from plotData import plotData # x= population size in 10,000s # y= profit in $10,000s print('Plotting data') data = pd.read_csv('ex1data1.txt', names=['Population', 'Profit']) X = data.iloc[:, 0] y = data.iloc[:, 1] m = data.shape[0] #tuple (row, col) // looking for row #FILE: plotData.py plotData(X, y) input('Paused. Press enter to continue.\n')
# -*- coding: utf-8 -*- """ Spyder Editor This is a temporary script file. """ import warmUpExercise as warmEx import numpy as np import matplotlib.pyplot as plt import gradientDescent as gd from matplotlib import cm print "Running warmUpExercise ... " print "5x5 Identity Matrix:" warmEx.warmUpExercise() try: input('Program paused. Press enter to continue...') except SyntaxError: pass print 'Plotting Data ...' """ ======================= Part 2: Plotting ======================= """ data = np.genfromtxt('ex1data1.txt',delimiter=',') #print data X = data[:,0] y = data[:,1]
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from warmUpExercise import warmUpExercise from plotData import plotData, plotData2 from gradientDescent import gradientDescent from computeCost import computeCost # ==================== Part 1: Basic Function ==================== print('Running warmup exercise ...') print('5x5 Identity Matrix:') print(warmUpExercise(), '\n') # ======================= Part 2: Plotting ======================= print('Plotting Data ...\n') data = np.loadtxt('ex1data1.txt', delimiter=',') X, y = data[:, 0], data[:, 1] plotData(X, y) # =================== Part 3: Gradient descent =================== print('Running Gradient Descent ...') m, n = data.shape X = np.hstack((np.ones((m, 1)), data[:, [0]])) y = data[:, [1]] theta = np.zeros((2, 1)) iterations = 1500
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm import warmUpExercise as wue import plotData as pd import computeCost as cc import gradientDescent as gd ## ==================== Part 1: Basic Function ==================== # Complete warmUpExercise.py print('Running warmUpExercise...') print('5x5 Identity Matrix: ') print(wue.warmUpExercise()) raw_input('Program paused. Press enter to continue.\n') ## ======================= Part 2: Plotting ======================= print('Plotting Data...') data = np.loadtxt('ex1data1.txt', delimiter=",") X = data[:,0] y = data[:,1] m = len(y) # number of training examples # Plot Data # Note: You have to complete the code in plotData.py pd.plotData(X, y)
from __future__ import division import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D #from matplotlib import cm import numpy as np import warmUpExercise, computeCost, gradientDescent print "Running warmUpExercise" print "5x5 Indentity Matrix:" print warmUpExercise.warmUpExercise() print "Program paused. Press enter to continue." raw_input("Press ENTER to continue") print ("Plotting data") #infile = open('ex1data1.txt', 'r') #for line in infile: # data.append(line) #infile.close() data = np.loadtxt('ex1data1.txt', delimiter = ',') X = data[:,0] y = np.matrix(data[:,1]).T m = len(y) plt.figure() plt.plot(X, y, 'o') #plt.savefig('temp.png') plt.show()
def Matrix(self): matrix = warmUpExercise.warmUpExercise() return matrix
# gradientDescentMulti.py # computeCostMulti.py # featureNormalize.py # normalEqn.py # # For this exercise, you will not need to change any code in this file, # or any other files other than those mentioned above. # # x refers to the population size in 10,000s # y refers to the profit in $10,000s # ==================== Part 1: Basic Function ==================== # Complete warmUpExercise.py print 'Running warmUpExercise ...' print '5x5 Identity Matrix:' warmup = warmUpExercise() print warmup #raw_input("Program paused. Press Enter to continue...") # ======================= Part 2: Plotting ======================= data = np.loadtxt('C:\Users\HTDA\Coursera-Stanford-ML-Python\ex1\ex1data1.txt', delimiter=',') m = data.shape[0] X = np.vstack(zip(np.ones(m),data[:,0])) y = data[:, 1] # Plot Data # Note: You have to complete the code in plotData.py print 'Plotting Data ...' plotData(data) #show()
# featureNormalize.py # normalEqn.py # # For this exercise, you will not need to change any code in this file, # or any other files other than those mentioned above. # # x refers to the population size in 10,000s # y refers to the profit in $10,000s # # # Initialization # # ==================== Part 1: Basic Function ==================== print('Running warmUpExercise ... \n') print('5x5 Identity Matrix: \n') print(WUE.warmUpExercise()) input("Press Enter to continue...") data = pd.read_csv('ex1data2.txt', header=None) X, y = data.iloc[:, :2], data.iloc[:, 2] #Data Separated into two pandas series m = np.size(y) #number of training example #Print out some data points print('First 10 examples from the dataset: \n') print('Printing X\n', X.head(10)) print('Printing y\n', y.head(10)) # Scale features and set them to zero mean print('Normalizing Features ...\n') [X, mu, sigma] = FN.featureNormalize(X) # Add intercept term to X
import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm import warmUpExercise as wue import plotData as pd import computeCost as cc import gradientDescent as gd ## ==================== Part 1: Basic Function ==================== # Complete warmUpExercise.py print('Running warmUpExercise...') print('5x5 Identity Matrix: ') print(wue.warmUpExercise()) raw_input('Program paused. Press enter to continue.\n') ## ======================= Part 2: Plotting ======================= print('Plotting Data...') data = np.loadtxt('ex1data1.txt', delimiter=",") X = data[:, 0] y = data[:, 1] m = len(y) # number of training examples # Plot Data # Note: You have to complete the code in plotData.py pd.plotData(X, y)
# featureNormalize.py # normalEqn.py # # For this exercise, you will not need to change any code in this file, # or any other files other than those mentioned above. # # x refers to the population size in 10,000s # y refers to the profit in $10,000s # ## ==================== Part 1: Basic Function ==================== from warmUpExercise import warmUpExercise print pcolor.WARN+"Runing warmUpExercise.py..."+pcolor.ENDC print pcolor.NOTE+"5x5 Indentity Matrix:"+pcolor.ENDC warmUpExercise() raw_input(pcolor.WARN+"Program paused.Press enter key to continue..."+pcolor.ENDC) ## ======================= Part 2: Plotting ======================= print "Plotting Data..." f = open('ex1data1.txt') X = np.array(np.empty(1)) y = np.array(np.empty(1)) for tmp in f.readlines(): tmp = tmp.split(',') X = np.append(X,float(tmp[0].split()[0])) y = np.append(y,float(tmp[1].split()[0])) from plotData import plotDisData as plotd plotd(X,y) raw_input("Program paused.Press enter key to continue...")
# gradientDescentMulti.py # computeCostMulti.py # featureNormalize.py # normalEqn.py # # For this exercise, you will not need to change any code in this file, # or any other files other than those mentioned above. # # x refers to the population size in 10,000s # y refers to the profit in $10,000s # ==================== Part 1: Basic Function ==================== # Complete warmUpExercise.py print('Running warmUpExercise ...') print('5x5 Identity Matrix:') warmup = warmUpExercise() print(warmup) input("Program paused. Press Enter to continue...") # ======================= Part 2: Plotting ======================= data = np.loadtxt('ex1data1.txt', delimiter=',') m = data.shape[0] X = np.vstack(zip(np.ones(m),data[:,0])) y = data[:, 1] # Plot Data # Note: You have to complete the code in plotData.py print('Plotting Data ...') plotData(data) plt.show()
from numpy import * from matplotlib.pyplot import * from mpl_toolkits.mplot3d import axes3d, Axes3D from warmUpExercise import warmUpExercise from plotData import plotData from computeCost import computeCost from gradientDescent import gradientDescent # 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', delimiter=',') X = data[:, 0] y = data[:, 1] m = len(y) # number of training examples # Plot Data # Note: You have to complete the code in plotData.py firstPlot = plotData(X, y) firstPlot.show()