В настоящее время я использую функцию 'Sigmoid', которую использует функция 'cost', чтобы определить, где линия должна находиться на диаграмме рассеяния.Однако после вывода массивы x и y выводятся на точечную диаграмму с правильными клавишами, но линия отсутствует на диаграмме.
Код, который определяет тета, выглядит следующим образом:
def loadTrgDf():
train, test = proc.getTrainingData()
# x derives from spouses and siblings
x = train.iloc[:, 2:4]
# y derives from the actual output
y = train.iloc[:, 1]
# Split and zeros
initial_theta = np.zeros(x.shape[1])
# Calculate the threta
theta = opt.fmin_cg(cost, initial_theta, cost_gradient, (x, y))
print(" ")
print(theta)
# Store for readability
sibSpTheta = theta[0]
parchTheta = theta[1]
Полученные данные затем выводятся на график рассеяния здесь:
# Plot findings
fig, ax = plt.subplots()
for index, row in train.iterrows():
if row['Survived'] == 1:
ax.scatter(row['SibSp'], row['Parch'], marker="+", c='green')
else:
ax.scatter(row['SibSp'], row['Parch'], marker="x", c='red', linewidth=1)
plt.title("Survival Rate", fontsize=16)
plt.xlabel("Spouses", fontsize=14)
plt.ylabel("Siblings", fontsize=14)
plt.legend(["survived", "not survived"])
plt.show()
x_axis = np.array([x.min(), x.max()])
y_axis = (-1 / 1) * (sibSpTheta * x_axis + parchTheta)
ax.plot(x_axis, y_axis, linewidth=2)
fig
Код ниже используется opt.fmin_cgфункция:
def sigmoid(z):
return 1 / (1 + np.exp(-z))
def cost(theta, x, y):
predictions = sigmoid(x @ theta)
predictions[predictions == 1] = 0.5 # log(1)=0 causes division error during optimization
error = -y * np.log(predictions) - (1 - y) * np.log(1 - predictions)
return sum(error) / len(y);
def cost_gradient(theta, x, y):
predictions = sigmoid(x @ theta);
return x.transpose() @ (predictions - y) / len(y)
Значения:
PassengerId Survived SibSp Parch
77 78 0 0 0
748 749 0 1 0
444 445 1 0 0
361 362 0 1 0
576 577 1 0 0
27 28 0 3 2
232 233 0 0 0
424 425 0 1 1
785 786 0 0 0
... ... ... ... ...
x содержит SibSp и Parch IV
y содержит выжившего DV
Это неожиданный вывод:
![enter image description here](https://i.stack.imgur.com/TUyst.png)
Это ожидаемый вывод:
![enter image description here](https://i.stack.imgur.com/Z3pun.png)
РЕДАКТИРОВАТЬ: Строка появилась!Однако это неточно.
![enter image description here](https://i.stack.imgur.com/Twg8K.png)