#菜鸟机学的的逆袭之路#day6

パス= 'ex2data2.txt'
data_init = pd.read_csv(path、header = None、names = ['Test 1'、 'Test2'、 'Accepted'])
data_init.head()

positive2 = data_init [data_init ['Accepted']。isin([1])]
negative2 = data_init [data_init ['Accepted']。isin([0])]
ax.scatter(positive2 ['Test 1']、positive2 [ 'Test 2']、s = 50、c = 'b'、marker = 'o'、lable = 'Accepted')
ax.scatter(negative2 ['Test 1']、negative2 ['Test 2']、s = 50、c = 'r'、marker = 'x'、label = 'Rejected')
ax.legend()
ax.set_xlabel( 'Test 1 Score')
ax.set_ylabel( 'Test 2 Score')
plt.show()

度= 6
data2 = data_init
x1 = data2 ['Test 1']
x2 = data2 ['Test 2']
data2.insert(3、 'ones'、1)
for i for range(1、degree + 1):
for j範囲(0、i + 1):
data2 ['F' + str(i-j)+ str(j)] = np.power(x1、ij)* np.power(x2、j)
data2.drop( 'テスト1'、axis = 1、
inplace = True)data2.drop( 'Test 2'、axis = 1、
inplace = True)data2.head()

#编辑代価函数:
def costreg(theta、X、y、learningRate):
theta = np.matrix(theta)
X = np.matrix(X)
y = np.matrix(y)
first = np.multiply(-y、 np.log(sigmoid(X * theta.T)))
2番目= np.multiply((1-y)、np.log(1-sigmoid(X * theta.T)))
reg =(learnignRate /(2 * len(X)))* np.sum(np.power(theta [:、1:theta.shape [1]]、2))
return np.sum(first-second)/ len(X)+ reg

#编辑梯度函数(正则化后)
def gradientReg(theta、X、y、learningRate):
theta = np.matrix(theta)
X = np.matrix(X)
y = np.matrix(y)
parameters = int(theta .ravel()。shpae [1])
grad = np.zeros(parameters)
error = sigmoid(X * theta.T)-y
for i in range(parameters):
term = np.multiply(error、X [:, i])
if(i == 0):
grad [i] = np.sum(term)/ len(X)
else:
grad [i] = np.sum(term)/ len(X)+((learningRate) / len(X))* theta [:、i])
卒業生を返す


#Initialize X、y、theta cols = data2.shape [1]
X2 = data2.iloc [:、1:cols]
y2 = data2.iloc [:、0:1]
theta = np.zeros(cols -1)
X2 = np.array(X2.values)
y2 = np.array(y.values)
learningRate = 1
costReg(theta2、X2、y2、learningRate)#初期コストを計算


以下は、ツールライブラリを使用してパラメーターを解決します。result2= opt.fmin_tnc(func = costReg、x0 = theta2、fprime = gradientReg、args =(X2、y2、learningRate))
result2


前のセクションの予測関数を使用して、トレーニングセットtheta_min = np.matrix(result2 [0])でのスキームの精度をチェックします
予測=予測(theta_min、X2)
正しい= [1 if((a == 1 and b == 1)または(a == 0 and b == 0))それ以外の場合は、zip内の(a、b)に対して0(予測、y2)]
精度=(合計(マップ(int、正しい))%len(正しい))
print( 'accuracy = {0}%'。format(accuracy))


決定曲線を描くdef hfunc(theta、x1、x2):
temp = theta [0] [0]
place = 0
for i in range(1、degree = 1):
for j in range(0、i + 1) :
temp + = np.power(x1、ij)* np.power(x2、j)* theta [0] [place + 1]
place + = 1
return temp
#note:ここで、hfuncは実際には決定関数であり、分割関数です
# y = theta0 + theta1 * x1 + theta2 * x2 + theta3 * x1 ^ 2 + theta4 * x1x2 + theta5 * x2 ^ 2 +…

#ここでの決定境界は、x1とx2の値が決定されたときに、式によって追加された値が、図で黄色のフォントポイントで表示されるコスト関数の臨界値よりも小さくなければならないことを意味します。
def find_decision_boundary(theta):
t1 = np.linspace(-1、1.5、1000)
t2 = np.linspace(-1、1.5、1000)

cordinates = [(x, y) for x in t1 for y in t2]
x_cord, y_cord = zip(*cordinates)
h_val = pd.DataFrame({'x1':x_cord, 'x2':y_cord})
h_val['hval'] = hfunc2(theta, h_val['x1'], h_val['x2'])

decision = h_val[np.abs(h_val['hval']) < 2 * 10**-3]
return decision.x1, decision.x2

fig、ax = plt.subplots(figsize =(12,8))
ax.scatter(positive2 ['Test 1']、positive2 ['Test 2']、s = 50、c = 'b'、marker = 'o '、label =' Accepted ')
ax.scatter(negative2 [' Test 1 ']、negative2 [' Test 2 ']、s = 50、c =' r '、marker =' x '、label =' Rejected ')
ax.set_xlabel( 'テスト1スコア')
ax.set_ylabel( 'テスト2スコア')

x、y = find_decision_boundary(result2)
plt.scatter(x、y、c = 'y'、s = 10、label = 'Prediction')
ax.legend()
plt.show()

lamuda
= 0の場合のlearningRate2 = 0の過剰適合
result3 = opt.fmin_tnc(func = costReg、x0 = theta2、fprime = gradientReg、args =(X2、y2、learningRate2))

lamuda = 100时欠拟合
learningRate2 = 100
result4 = opt.fmin_tnc(func = costReg、x0 = theta2、fprime = gradientReg、args =(X2、y2、learningRate2))

元の記事を31件公開 Likes0 訪問数697

おすすめ

転載: blog.csdn.net/ballzy/article/details/104430708