#菜鸟机学习的逆袭之路#Day4

isin()はリストを受け入れ、列の要素がリストにあるかどうかを判断します。

fig、ax = plt.subplots(figsize =(12,8))
#figは描画ウィンドウ(図)を表し、axはこの描画ウィンドウの座標系(軸)を表し、通常は引き続きaxを操作します。
凡例()の主な機能は、グラフ上の凡例をマークすることです。これは、各曲線のテキスト表示を示すために使用されます。凡例を左、右、下などに制御することもできます。
実際の使用では、legend()には、凡例の位置を制御するlocパラメータがあります。たとえば、plot.legend(loc = 2)の場合、この位置は4つのアイテムの2番目のアイテム、つまり左上隅です。locは4つの数値1、2、3、4です。
元のリンク:
http : //30daydo.com/article/215
転載、出典を明記してください

fmin_tnc関数:
パラメータ:
func:最適化された目的関数
x0:初期値
fprime:最適化関数funcの勾配関数を提供します。それ以外の場合、最適化関数funcは関数値と勾配を返す必要があります。または、approx_grad = Trueを
設定するapprox_grad:Trueに設定すると、近似勾配勾配
args:タプル、これは最適化関数に渡されるパラメーターです。
戻り値:
x:配列、返された最適化問題のターゲット値
nfeval:整数、関数評価の数。
最適化実行されると、ターゲット最適化関数が呼び出されるたびに、機能評価も。反復中に複数の関数評価があります。このパラメーターは反復数と同じではありませんが、多くの場合、反復数より大きくなります。
rc:int、戻りコード、以下を参照

#np linspace関数:最初のパラメーターの開始値、2番目のパラメーターの終了値(含まれていません)、3番目のパラメーターのステップ(各要素間の間隔)

データの
インポートnumpyをnp
として
インポートpandas をpdとしてインポートmatplotlib.pyplotをpltとしてインポート

path = 'd:/ex2data1.txt'
data = pd.read_csv(path、header = None、names = ['Exam 1'、 'Exam 2'、 'Admitted'])
data.head()

#datadata分组
positive = data [data ['Admitted']。isin([1])]
negative = data [data ['Admitted']。isin([0])]
fig、ax = plt.subplots(figsize =( 12,8))
ax.scatter(positive ['Exam 1']、positive ['Exam 2']、s = 50、c = 'b'、marler = 'o'、label = 'Admitted')
ax.scatter (negative ['Exam 1']、negative ['Exam 2']、s = 50、c = 'r'、marker = 'x'、label = 'Not Admitted')
ax.legend()
ax.set_xlabel( '試験1スコア ')
ax.set_ylabel('試験2スコア ')
plt.show()

シグモイド関数を編集
def sigmoid(z)
return 1 /(1 + np.exp(-z))

#编辑代価函数J(theta)
def cost(theta、X、y)
theta = np.matrix(theta)
X = np.matrix(X)
y = np.matrix(y)
first = np.multiply(-y、 np.log(sigmoid(X * theta.T)))
second = np.multiply((1-y)、log(1-sigmoid(X * theta.T)))
return np.sum(first-second)/ (len(X))

#初始化X、y、theta
data.insert(0、 'Ones'、1)
cols = data.shape [1]
X = data.iloc [:、:-1]
y = data.iloc [:、cols-1 :cols]
theta = np.zeros(3)

X = np.array(X.values)
y = np.array(y.values)


行列の次元をチェックX.shape、y.shape、th​​eta.shape
cost(theta、X、y)

勾配計算関数を実現し、theta
def gradient(theta、X、y)の更新はありません
theta = np.matrix(theta)
X = np.matrix(X.values)
y = np。Matrix(y.values)
parameters = int(theta.ravel().shape [1])
grad = np.zeros(parameters)
error = sigmoid(X * theta.T)-y
for i in range(parameters):
term = np.multiply(error、 X [:、i])
grad [i] = np.sum(term)/ len(X)
return grad

#pythonライブラリを呼び出した後、反復回数とステップサイズの関数を自動的に見つけることができ、関数は自動的に最適解を解決します。

import scipy.optimize as opt
result = opt.fmin_tnc(func = cost、x0 = theta、fprime = gradient、args =(X、y))
result
#查看结果
cost(result [0]、X、y)

plotting_x1 = np.linspace(30,100,100)
plotting_h1 =(-result [0,0]-result [0] [1] * plotting_x1)/ result [0] [2]
fig、ax = plt.subplots(figsize =(12、 8))
ax.plot(plotting_x1、plotting_h1、 'y'、label = 'Prediction')
ax.scatter(positive ['Exam 1']、positive ['Exam 2']、s = 50、c = 'b' 、marker = 'o'、label = 'Admitted')
ax.scatter(negative ['Exam 1']、negative ['Exam 2']、s = 50、c = 'r'、marker = 'x'、label = '認められない')
ax.legend()
ax.set_xlabel( '試験1スコア')
ax.set_ylabel( '試験2スコア')
plt.show()

def hfunc1(theta、X):
return sigmoid(np.dot(theta.T、X))
hfunc1(result [0]、[1,45,85])

#Define 予測関数
def
predict (シータ、X):確率=シグモイド(X *シータT)
戻り値[x> = 0.5の場合は1、そうでなければxの確率は0]


#統計予測の正解率:theta_min = np.matrix(result [0])
予測=予測(theta_min、X)
正解= [1 if((a1とb1)または(a == 0)および(b == 0))それ以外の場合はzip(predictions、y)で(a、b)の場合0]
精度=(sum(map(int、correct))%len(correct) ))
preint( 'accuracy = {0}%'。format(accuracy))

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

おすすめ

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