using factor in ggplot for python - python-ggplot

I'm having trouble using factor in ggplot for python. Here is the R code that I want to re-create in python.
library('ggplot2')
head(iris)
ggplot(iris, aes(Sepal.Length,Sepal.Width)) + geom_point(aes(colour=factor(Species)))
In python I do the following:
from ggplot import *
from sklearn.datasets import load_iris
iris = load_iris()
ggplot(iris.data, aes(iris.data[:, x_index], iris.data[:, y_index])) \
+ geom_point(aes(colour = iris.target)) + xlab('x axis') + ylab('y axis')
I keep getting errors. I believe this has to do with the factor part. I can't set to a factor and then allow it to plot. Any help would be appreciated.

Related

Solving stochastic PDEs in Fipy

I would like to simulate coupled PDEs with a Gaussian white noise field, and was unable to find any examples or documentation that suggests how it should be done. In particular, I am interested in Cahn-Hilliard-like systems with noise:
d/dt(phi) = div(grad(psi)) + div(noise)
psi = f(phi) + div(grad(phi))
Is there a way to implement this in Fipy?
You can add a GaussianNoiseVariable as a source to your equations.
For a non-conserved field, you would do, e.g.,
noise = fp.GaussianNoiseVariable(mesh=..., mean=..., variance=...)
:
:
eq = fp.TransientTerm(...) == fp.DiffusionTerm(...) + ... + noise
for step in steps:
noise.scramble()
:
:
eq.solve(...)
For a conserved field, you would use:
eq = fp.TransientTerm(...) == fp.DiffusionTerm(...) + ... + noise.faceGrad.divergence

How to activate naoqi_navigation_samples on my Pepper robot?

One of the goal of the project I'm working with is making Pepper robot patrolling hospital wards "autonomously". So I downloaded some basic application to start with navigation (https://github.com/aldebaran/naoqi_navigation_samples). The "explore" application is critical since it is needed by the other two (places and patrol). I tried to launch "explore" on Choregraphe, but the robot does not move (so it does not explore neither creates a map, obviously) and the application ends by saying the final sentence. In particular the block "Get map" gives an error. So, the application starts correctly but it does not work properly.
I saved "explore" as a robot application and tried in both autonomous life and not autonomous life.
I can not understand where I'm wrong: could you help me please?
Make sure the charging flap is not open when you run. Also try this code, it will create a map
#! /usr/bin/env python
# -*- encoding: UTF-8 -*-
"""Example: Use explore method."""
import qi
import argparse
import sys
import numpy
from PIL import Image
def main(session):
"""
This example uses the explore method.
"""
# Get the services ALNavigation and ALMotion.
navigation_service = session.service("ALNavigation")
motion_service = session.service("ALMotion")
# Wake up robot
motion_service.wakeUp()
# Explore the environement, in a radius of 2 m.
radius = 5.0
error_code = navigation_service.explore(radius)
if error_code != 0:
print ("Exploration failed.")
return
# Saves the exploration on disk
path = navigation_service.saveExploration()
print ("Exploration saved at path: \"" + path + "\"")
# Start localization to navigate in map
navigation_service.startLocalization()
# Come back to initial position
navigation_service.navigateToInMap([0., 0., 0.])
# Stop localization
navigation_service.stopLocalization()
# Retrieve and display the map built by the robot
result_map = navigation_service.getMetricalMap()
map_width = result_map[1]
map_height = result_map[2]
img = numpy.array(result_map[4]).reshape(map_width, map_height)
img = (100 - img) * 2.55 # from 0..100 to 255..0
img = numpy.array(img, numpy.uint8)
Image.frombuffer('L', (map_width, map_height), img, 'raw', 'L', 0, 1).show()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--ip", type=str, default="127.0.0.1",
help="Robot IP address. On robot or Local Naoqi: use '127.0.0.1'.")
parser.add_argument("--port", type=int, default=9559,
help="Naoqi port number")
args = parser.parse_args()
session = qi.Session()
try:
session.connect("tcp://" + args.ip + ":" + str(args.port))
except RuntimeError:
print ("Can't connect to Naoqi at ip \"" + args.ip + "\" on port " + str(args.port) +".\n"
"Please check your script arguments. Run with -h option for help.")
sys.exit(1)
main(session)

The gof function from package btergm gives AUC value of a precision-recall greater than 1

I was trying to do out-of-sample prediction using the gof function from package btergm. When calculating the AUC value of a precision-recall curve from the testing set, I get the result of 1.012909, which seems to be theoretically impossible.
How can I interpret the result, or am I doing something wrong. Thank you! Here is my code:
network <- readRDS(url("https://www.dropbox.com/s/zxhqxa8h9awkzpd/network.rds?dl=1"))
model.3<- btergm(network[1:9]~edges+gwodegree(1, fixed=TRUE)+transitiveties + ctriple +
gwidegree(1, fixed=TRUE)+mutual+gwesp(1.5, fixed=TRUE)+ttriple+
memory(type="stability")+delrecip,R=1000)
gof.3 <-gof(model.3, nsim = 1000,target=network[[10]],formula = network[9:10]~edges+gwodegree(1,fixed=TRUE) +
transitiveties + ctriple +gwidegree(1,fixed=TRUE)+mutual+gwesp(1.5, fixed=TRUE)+ttriple+
memory(type="stability")+delrecip,coef = coef(model.3),
statistics = rocpr)
gof.3[[1]]$auc.pr

Confidence Intervals for prediction of a logistic generalized linear mixed model (GLMM)

I am running a logistic generalized linear mixed model and would like to plot my effects together with confidence intervals.
I use the lme4 package to fit my model:
glmer (cbind(positive, negative) ~ F1 * F2 * F3 + V1 + F1 * I(V1^2) + V2 + F1 * I(V2^2) + V3 + I(V3^2) + V4 + I(V4^2) + F4 + (1|OLRE) + (1|ind), family = binomial, data = try, na.action = na.omit, control=glmerControl(optimizer = "optimx", calc.derivs = FALSE, optCtrl = list(method = "nlminb", starttests = FALSE, kkt = FALSE)))
OLRE means that I use an observational level random effect in order to overcome overdispersion.
If you wonder because of convergence warnings, I went through the lme4 troubleshooting protocol, and it should be fine.
In order to get effect plots with confidence intervals, I tried to use ggpredict:
sjPlot, e.g.:
plot_model(mod, type = "pred", terms = c("F1", "F2", "F3"), ci.lvl = 0.95)
I also tried to go through this protocol:
https://rpubs.com/hughes/84484
intdata <- expand.grid(
positive = c(0,1),
negative = c(0,1),
F1 = as.factor(c(0,1)),
F2 = as.factor(c(1,2,3)),
F3= as.factor(c(1,2,3,4)),
V1= as.numeric(median(try$V1)),
F4= as.factor(c(30, 31)),
ind= as.factor(c(68)),
OLRE = as.factor(c(2450)),
V2= as.numeric(median(try$V2)),
V3= as.numeric(median(try$V3)),
V4= as.numeric(median(try$V4))
)
#conditional variances
cV <- ranef(mod, condVar = TRUE)
ranvar <- attr(cV[[1]], "postVar")
sqrt(diag(ranvar[,,1]))
mm <- model.matrix(terms( mod), data=intdata,
contrasts.arg = lapply(intdata[,c(3:5, 7:9)], contrasts, contrasts=FALSE))
predFun<-function(.) mm%*%fixef(.)
bb<-bootMer(mod,FUN=predFun,nsim=3)
with some problems and warnings regarding contrasts and
Error in mm %*% fixef(.) : non-conformable arguments
Therefore, I am wondering if anyone might help me but so far I am not able to Nothing worked so far, so I would really appreciate some help.
Here the link to the data https://drive.google.com/file/d/1qZaJBbM1ggxwPnZ9bsTCXL7_BnXlvfkW/view?usp=sharing

Tensorflow Probability Logistic Regression Example

I feel I must be missing something obvious, in struggling to get a positive control for logistic regression going in tensorflow probability.
I've modified the example for logistic regression here, and created a positive control features and labels data. I struggle to achieve accuracy over 60%, however this is an easy problem for a 'vanilla' Keras model (accuracy 100%). What am I missing? I tried different layers, activations, etc.. With this method of setting up the model, is posterior updating actually being performed? Do I need to specify an interceptor object? Many thanks..
### Added positive control
nSamples = 80
features1 = np.float32(np.hstack((np.reshape(np.ones(40), (40, 1)),
np.reshape(np.random.randn(nSamples), (40, 2)))))
features2 = np.float32(np.hstack((np.reshape(np.zeros(40), (40, 1)),
np.reshape(np.random.randn(nSamples), (40, 2)))))
features = np.vstack((features1, features2))
labels = np.concatenate((np.zeros(40), np.ones(40)))
featuresInt, labelsInt = build_input_pipeline(features, labels, 10)
###
#w_true, b_true, features, labels = toy_logistic_data(FLAGS.num_examples, 2)
#featuresInt, labelsInt = build_input_pipeline(features, labels, FLAGS.batch_size)
with tf.name_scope("logistic_regression", values=[featuresInt]):
layer = tfp.layers.DenseFlipout(
units=1,
activation=None,
kernel_posterior_fn=tfp.layers.default_mean_field_normal_fn(),
bias_posterior_fn=tfp.layers.default_mean_field_normal_fn())
logits = layer(featuresInt)
labels_distribution = tfd.Bernoulli(logits=logits)
neg_log_likelihood = -tf.reduce_mean(labels_distribution.log_prob(labelsInt))
kl = sum(layer.losses)
elbo_loss = neg_log_likelihood + kl
predictions = tf.cast(logits > 0, dtype=tf.int32)
accuracy, accuracy_update_op = tf.metrics.accuracy(
labels=labelsInt, predictions=predictions)
with tf.name_scope("train"):
optimizer = tf.train.AdamOptimizer(learning_rate=FLAGS.learning_rate)
train_op = optimizer.minimize(elbo_loss)
init_op = tf.group(tf.global_variables_initializer(),
tf.local_variables_initializer())
with tf.Session() as sess:
sess.run(init_op)
# Fit the model to data.
for step in range(FLAGS.max_steps):
_ = sess.run([train_op, accuracy_update_op])
if step % 100 == 0:
loss_value, accuracy_value = sess.run([elbo_loss, accuracy])
print("Step: {:>3d} Loss: {:.3f} Accuracy: {:.3f}".format(
step, loss_value, accuracy_value))
### Check with basic Keras
kerasModel = tf.keras.models.Sequential([
tf.keras.layers.Dense(1)])
optimizer = tf.train.AdamOptimizer(5e-2)
kerasModel.compile(optimizer = optimizer, loss = 'binary_crossentropy',
metrics = ['accuracy'])
kerasModel.fit(features, labels, epochs = 50) #100% accuracy
Compared to the github example, you forgot to divide by the number of examples when defining the KL divergence:
kl = sum(layer.losses) / FLAGS.num_examples
When I change this to your code, I quickly get to an accuracy of 99.9% on your toy data.
Additionaly, the output layer of your Keras model actually expects a sigmoid activation for this problem (binary classification):
kerasModel = tf.keras.models.Sequential([
tf.keras.layers.Dense(1, activation='sigmoid')])
It's a toy problem, but you will notice that the model gets to 100% accuracy faster with a sigmoid activation.

Resources