我不明白为什么我的逻辑回归图中有一条垂直线?

I don't understand why is there a vertical line in my logistic regression graph?

%matplotlib notebook
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

x_df=pd.DataFrame([0.5,0.75,1,1.25,1.5,1.75,1.75,2,2.25,2.5,2.75,3,3.25,3.5,4,4.25,4.5,4.75,5,5.5])
y_df=pd.DataFrame([0,0,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,1,1])
print()

#adding the column one since there is an extra theta value
x_df['intercept']=1

#converting to matrix
X = np.matrix(x_df.values)
print(X)
#
##converting the matrix y
y= np.matrix(y_df.values)
print(y)

#initialize theta
theta = np.matrix(np.array([0,0]))

def sigmoid(x):

    return 1/(1 + np.exp(-x))


def cost(x,y,theta):
    m = y.shape[0]
    h = sigmoid(x * theta.T)
    h1 = np.multiply(y,np.log(h))
    h2 = np.multiply(1- y,np.log(1-h))
    return -np.sum(h1+h2)/(1.0*m)

def gd(x,y,theta,alpha = 0.1,iter=10000):

    m = y.shape[0]

    for i in range(iter):
        h = sigmoid(x * theta.T)
        error = h-y
        update = np.dot(error.T,x)
        theta = theta - ( (alpha*update)/m )

    return theta,cost(x,y,theta),h

new_theta,new_cost,new_h=gd(X,y,theta)

print(np.ravel(new_h).T)

n=np.ravel(new_h).T

n=pd.DataFrame(n)
print(n)

plt.plot(x_df,y_df,'go',x_df,n,'bo')

我花了很多时间尝试对 python 3 中的逻辑回归进行硬编码,我相信代码是正确的。花了那么多时间,当我开始绘制图表时,结果是这样的!

weird logistic regression graph in blue circles

有人可以帮我写代码吗?我不擅长可视化图表(假设函数与 X)!

将您的绘图线更改为

plt.plot(x_df,y_df,'go', X[:, 0], new_h,'bo')

plt.plot(x_df,y_df,'go', x_df.ix[:, 0], n,'bo')

您的问题是 x_df 有两列,一列称为 0,另一列称为 intercept(全为 1)并且都绘制为 x 轴.