Why is the Sigmoid function not displaying the line on the scatter plot?
I'm currently using the 'Sigmoid' function, which the 'cost' function uses in order to determine, where the line should be within the scatter plot diagram. However, upon output, the x and y arrays are outputted onto the scatter plot with the correct keys, but the line is absent from the diagram.
The code that determines the theta is as follows:
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]
The findings are then plotted into a scatter graph here:
# 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
The code below is used by the opt.fmin_cg function:
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)
Values:
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 contains the IV's SibSp and Parch
y contains the DV survived
This is the unexpected output:
This is the expected output:
EDIT:
The line appeared! However, it's inaccurate.
python numpy
add a comment |
I'm currently using the 'Sigmoid' function, which the 'cost' function uses in order to determine, where the line should be within the scatter plot diagram. However, upon output, the x and y arrays are outputted onto the scatter plot with the correct keys, but the line is absent from the diagram.
The code that determines the theta is as follows:
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]
The findings are then plotted into a scatter graph here:
# 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
The code below is used by the opt.fmin_cg function:
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)
Values:
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 contains the IV's SibSp and Parch
y contains the DV survived
This is the unexpected output:
This is the expected output:
EDIT:
The line appeared! However, it's inaccurate.
python numpy
2
Plz provide actual x and y values in your code rather than posting snapshots from which we can't copy the values
– Bazingaa
Jan 1 at 19:23
2
try to moveplt.show()
afterax.plot(x_axis, y_axis, linewidth=2)
– Bazingaa
Jan 1 at 19:24
@Bazingaa Thank you, the line has appeared! However it seems to be a bit distorted.
– Danny_P
Jan 1 at 19:31
add a comment |
I'm currently using the 'Sigmoid' function, which the 'cost' function uses in order to determine, where the line should be within the scatter plot diagram. However, upon output, the x and y arrays are outputted onto the scatter plot with the correct keys, but the line is absent from the diagram.
The code that determines the theta is as follows:
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]
The findings are then plotted into a scatter graph here:
# 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
The code below is used by the opt.fmin_cg function:
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)
Values:
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 contains the IV's SibSp and Parch
y contains the DV survived
This is the unexpected output:
This is the expected output:
EDIT:
The line appeared! However, it's inaccurate.
python numpy
I'm currently using the 'Sigmoid' function, which the 'cost' function uses in order to determine, where the line should be within the scatter plot diagram. However, upon output, the x and y arrays are outputted onto the scatter plot with the correct keys, but the line is absent from the diagram.
The code that determines the theta is as follows:
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]
The findings are then plotted into a scatter graph here:
# 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
The code below is used by the opt.fmin_cg function:
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)
Values:
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 contains the IV's SibSp and Parch
y contains the DV survived
This is the unexpected output:
This is the expected output:
EDIT:
The line appeared! However, it's inaccurate.
python numpy
python numpy
edited Jan 2 at 0:18
Danny_P
asked Jan 1 at 19:20
Danny_PDanny_P
715
715
2
Plz provide actual x and y values in your code rather than posting snapshots from which we can't copy the values
– Bazingaa
Jan 1 at 19:23
2
try to moveplt.show()
afterax.plot(x_axis, y_axis, linewidth=2)
– Bazingaa
Jan 1 at 19:24
@Bazingaa Thank you, the line has appeared! However it seems to be a bit distorted.
– Danny_P
Jan 1 at 19:31
add a comment |
2
Plz provide actual x and y values in your code rather than posting snapshots from which we can't copy the values
– Bazingaa
Jan 1 at 19:23
2
try to moveplt.show()
afterax.plot(x_axis, y_axis, linewidth=2)
– Bazingaa
Jan 1 at 19:24
@Bazingaa Thank you, the line has appeared! However it seems to be a bit distorted.
– Danny_P
Jan 1 at 19:31
2
2
Plz provide actual x and y values in your code rather than posting snapshots from which we can't copy the values
– Bazingaa
Jan 1 at 19:23
Plz provide actual x and y values in your code rather than posting snapshots from which we can't copy the values
– Bazingaa
Jan 1 at 19:23
2
2
try to move
plt.show()
after ax.plot(x_axis, y_axis, linewidth=2)
– Bazingaa
Jan 1 at 19:24
try to move
plt.show()
after ax.plot(x_axis, y_axis, linewidth=2)
– Bazingaa
Jan 1 at 19:24
@Bazingaa Thank you, the line has appeared! However it seems to be a bit distorted.
– Danny_P
Jan 1 at 19:31
@Bazingaa Thank you, the line has appeared! However it seems to be a bit distorted.
– Danny_P
Jan 1 at 19:31
add a comment |
1 Answer
1
active
oldest
votes
The issue is not with the plotting, but with the regression concept.
y_axis = (-1 / 1) * (sibSpTheta * x_axis + parchTheta)
This derives from the calculation that looks like this:
weights * features = weight0 + weight1 * feature1 + weight2 * feature2 + ...
You need to create a weight which corresponds to no feature value so this line becomes something that looks like this:
freeWeight = theta[0]
sibSpTheta = theta[1]
parchTheta = theta[2]
y_axis = (-1 / freeWeight) * (sibSpTheta * x_axis + parchTheta)
This can be done by creating an extra column which corresponds to no feature but has a dummy value when you splice your data frame. This process is called scaling.
Moving onto the x
and +
markers. You need to loop the x
data frame. Not the full train
data frame.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998263%2fwhy-is-the-sigmoid-function-not-displaying-the-line-on-the-scatter-plot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The issue is not with the plotting, but with the regression concept.
y_axis = (-1 / 1) * (sibSpTheta * x_axis + parchTheta)
This derives from the calculation that looks like this:
weights * features = weight0 + weight1 * feature1 + weight2 * feature2 + ...
You need to create a weight which corresponds to no feature value so this line becomes something that looks like this:
freeWeight = theta[0]
sibSpTheta = theta[1]
parchTheta = theta[2]
y_axis = (-1 / freeWeight) * (sibSpTheta * x_axis + parchTheta)
This can be done by creating an extra column which corresponds to no feature but has a dummy value when you splice your data frame. This process is called scaling.
Moving onto the x
and +
markers. You need to loop the x
data frame. Not the full train
data frame.
add a comment |
The issue is not with the plotting, but with the regression concept.
y_axis = (-1 / 1) * (sibSpTheta * x_axis + parchTheta)
This derives from the calculation that looks like this:
weights * features = weight0 + weight1 * feature1 + weight2 * feature2 + ...
You need to create a weight which corresponds to no feature value so this line becomes something that looks like this:
freeWeight = theta[0]
sibSpTheta = theta[1]
parchTheta = theta[2]
y_axis = (-1 / freeWeight) * (sibSpTheta * x_axis + parchTheta)
This can be done by creating an extra column which corresponds to no feature but has a dummy value when you splice your data frame. This process is called scaling.
Moving onto the x
and +
markers. You need to loop the x
data frame. Not the full train
data frame.
add a comment |
The issue is not with the plotting, but with the regression concept.
y_axis = (-1 / 1) * (sibSpTheta * x_axis + parchTheta)
This derives from the calculation that looks like this:
weights * features = weight0 + weight1 * feature1 + weight2 * feature2 + ...
You need to create a weight which corresponds to no feature value so this line becomes something that looks like this:
freeWeight = theta[0]
sibSpTheta = theta[1]
parchTheta = theta[2]
y_axis = (-1 / freeWeight) * (sibSpTheta * x_axis + parchTheta)
This can be done by creating an extra column which corresponds to no feature but has a dummy value when you splice your data frame. This process is called scaling.
Moving onto the x
and +
markers. You need to loop the x
data frame. Not the full train
data frame.
The issue is not with the plotting, but with the regression concept.
y_axis = (-1 / 1) * (sibSpTheta * x_axis + parchTheta)
This derives from the calculation that looks like this:
weights * features = weight0 + weight1 * feature1 + weight2 * feature2 + ...
You need to create a weight which corresponds to no feature value so this line becomes something that looks like this:
freeWeight = theta[0]
sibSpTheta = theta[1]
parchTheta = theta[2]
y_axis = (-1 / freeWeight) * (sibSpTheta * x_axis + parchTheta)
This can be done by creating an extra column which corresponds to no feature but has a dummy value when you splice your data frame. This process is called scaling.
Moving onto the x
and +
markers. You need to loop the x
data frame. Not the full train
data frame.
edited Jan 2 at 19:06
answered Jan 2 at 18:47
JaquarhJaquarh
3,4261135
3,4261135
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998263%2fwhy-is-the-sigmoid-function-not-displaying-the-line-on-the-scatter-plot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Plz provide actual x and y values in your code rather than posting snapshots from which we can't copy the values
– Bazingaa
Jan 1 at 19:23
2
try to move
plt.show()
afterax.plot(x_axis, y_axis, linewidth=2)
– Bazingaa
Jan 1 at 19:24
@Bazingaa Thank you, the line has appeared! However it seems to be a bit distorted.
– Danny_P
Jan 1 at 19:31