Windows Error using XGBoost with python - python-2.7

So I'm tackling this machine-learning problem (from a previous Kaggle competition for practice: https://www.kaggle.com/c/nyc-taxi-trip-duration) and I'm trying to use XGBoost but getting an error which I have no clue how to tackle. I searched on google and stack overflow but couldn't find anyone with a similar problem.
I'm using python 2.7 with the Spyder IDE through Anaconda and I'm on Windows 10. I did have some trouble installing the xgboost package so I won't completely erase the idea that it could be an installation error. However I'm also doing a Udemy course on ML and I was able to use xgboost just fine with a small dataset and I'm using the same functions.
Code
The code is pretty simple:
... import libraries
# import dataset
dataset = pd.read_csv('data/merged.csv')
y = dataset['trip_duration'].values
del dataset['trip_duration'], dataset["id"], dataset['distance']
X = dataset.values
# Split dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.25)
# fit XGBoost to training set
classifier = XGBClassifier()
classifier.fit(X_train, y_train)
Output
However it spits out the following error:
In [1]: classifier.fit(X_train, y_train)
Traceback (most recent call last):
File "<ipython-input-44-f44724590846>", line 1, in <module>
classifier.fit(X_train, y_train)
File "C:\Users\MortZ\Anaconda3\lib\site-packages\xgboost\sklearn.py", line 464, in fit
verbose_eval=verbose)
File "C:\Users\MortZ\Anaconda3\lib\site-packages\xgboost\training.py", line 204, in train
xgb_model=xgb_model, callbacks=callbacks)
File "C:\Users\MortZ\Anaconda3\lib\site-packages\xgboost\training.py", line 74, in _train_internal
bst.update(dtrain, i, obj)
File "C:\Users\MortZ\Anaconda3\lib\site-packages\xgboost\core.py", line 819, in update
_check_call(_LIB.XGBoosterUpdateOneIter(self.handle, iteration, dtrain.handle))
WindowsError: [Error -529697949] Windows Error 0xE06D7363
I don't really know how to interpret this so any help would be very appreciated.
Thanks in advance
MortZ

Well after struggling for a few days I managed to find a solution.
A friend of mine told xgboost is known to have problems with python 2.7 so I upgraded it to 3.6 This didn't entirely solve my problem but gave me a knew error:
OSError: [WinError 541541187] Windows Error 0x20474343
After some digging I found a solution to this. The fit function I was trying to use was the source of the problem (although it did work on a different dataset so I'm not entirely sure why..).
Solution
change
classifier = XGBClassifier()
classifier.fit(X_train, y_train)
to
dtrain = xgb.DMatrix(X_train, label=y_train)
dtest = xgb.DMatrix(X_test, label=y_test)
watchlist = [(dtrain, 'train'), (dtest, 'test')]
xgb_pars = {'min_child_weight': 1, 'eta': 0.5, 'colsample_bytree': 0.9,
'max_depth': 6, 'subsample': 0.9, 'lambda': 1., 'nthread': -1, 'booster' : 'gbtree', 'silent': 1, 'eval_metric': 'rmse', 'objective': 'reg:linear'}
model = xgb.train(xgb_pars, dtrain, 10, watchlist, early_stopping_rounds=2, maximize=False, verbose_eval=1)
print('Modeling RMSLE %.5f' % model.best_score)

I guess the error is because you are using XGBClassfier instead of XGBRegressor for a regression problem.

Related

What can cause an Assertion Error while running in Ursina?

I'm pretty new to programming with Python and am starting to learn how to use Ursina Game Engine...
Currently I'm running Python 3.9.4 through IDLE. I looked up a YouTube playlist that goes through different activities using Ursina. However I hit a snag with one of the videos. I keep receiving an Assertion Error even though I copied the code exactly as it is shown in the video.
Here is the complete Error I'm receiving:
= RESTART: C:/Users/Charbel/Desktop/Ursina Tutorials/smooth follow - flying dragon.py
package_folder: C:\Users\Charbel\AppData\Roaming\Python\Python39\site-packages\ursina
asset_folder: C:\Users\Charbel\Desktop\Ursina Tutorials
blender_paths:
{'2.7': WindowsPath('C:/Program Files/Blender Foundation/Blender/blender.exe'),
'2.8': WindowsPath('C:/Program Files/Blender Foundation/Blender 2.83/blender.exe'),
'default': WindowsPath('C:/Program Files/Blender Foundation/Blender 2.83/blender.exe')}
screen resolution: (1366, 768)
size; LVector2f(1092, 614)
render mode: default
development mode: True
application successfully started
Traceback (most recent call last):
File "C:\Users\Charbel\AppData\Roaming\Python\Python39\site-packages\direct\showbase\ShowBase.py", line 2153, in __igLoop
self.graphicsEngine.renderFrame()
AssertionError: !mat.is_nan() at line 322 of c:\buildslave\sdk-windows-amd64\build\panda\src\pgraph\transformState.cxx
Traceback (most recent call last):
File "C:/Users/Charbel/Desktop/Ursina Tutorials/smooth follow - flying dragon.py", line 22, in <module>
app.run()
File "C:\Users\Charbel\AppData\Roaming\Python\Python39\site-packages\ursina\main.py", line 238, in run
super().run()
File "C:\Users\Charbel\AppData\Roaming\Python\Python39\site-packages\direct\showbase\ShowBase.py", line 3325, in run
self.taskMgr.run()
File "C:\Users\Charbel\AppData\Roaming\Python\Python39\site-packages\direct\task\Task.py", line 546, in run
self.step()
File "C:\Users\Charbel\AppData\Roaming\Python\Python39\site-packages\direct\task\Task.py", line 500, in step
self.mgr.poll()
File "C:\Users\Charbel\AppData\Roaming\Python\Python39\site-packages\direct\showbase\ShowBase.py", line 2153, in __igLoop
self.graphicsEngine.renderFrame()
AssertionError: !mat.is_nan() at line 322 of c:\buildslave\sdk-windows-amd64\build\panda\src\pgraph\transformState.cxx
Here's a copy of my entire code:
from ursina import *
def update():
global dy
player.y += dy
if abs(player.y)>= 3:
dy = -dy
app = Ursina()
player = Entity(model='quad', scale=1, x = -4.5, texture = 'dragon_head.png')
e = [None]*50
e[0] = Entity(model='circle', scale = .2, color = color.green)
e[0].add_script(SmoothFollow(target = player, offset=(.3, 0, 0)))
dy =.08
for i in range(1,50):
e[i] = Entity(model='circle', scale = .2, color = color.green)
e[i].add_script(SmoothFollow(target = e[i-1], offset=(.2, 0, 0)))
app.run()
It appears that the error has something to do with the e[i-1] from my investigation. Has anyone else run into a similar issue or know if there's a bug in the Ursina language that causes this?
Thanks in advance
Charbel
My app just run to this error either.I think it is a rendering error caused by insufficient GPU computing power of the graphics.I reduce it to 44 and it works.
I'd assume either your computer or engine cannot handle it. I tried making a cube's rotation_z multiplied by an absurdly large number (100000000000000000000000000000000000000 to be exact) and it ran fine, but when I added an extra zero to the number, the engine broke.

pyomo mindtpy example program when run becomes unfeasible for binary variable

So I installed pyomo, glpk, and ipopt with anaconda,
When I run the example code here: https://pyomo.readthedocs.io/en/stable/contributed_packages/mindtpy.html
from pyomo.environ import *
model = ConcreteModel()
model.x = Var(bounds=(1.0,10.0),initialize=5.0)
model.y = Var(within=Binary)
model.c1 = Constraint(expr=(model.x-3.0)**2 <= 50.0*(1-model.y))
model.c2 = Constraint(expr=model.x*log(model.x)+5.0 <= 50.0*(model.y))
model.objective = Objective(expr=model.x, sense=minimize)
SolverFactory('mindtpy').solve(model, mip_solver='glpk', nlp_solver='ipopt',tee=True)
model.objective.display()
model.display()
model.pprint()
I get the output that the binary variable has apparently become infeasible:
python minlpex.py
INFO: ---Starting MindtPy---
INFO: Original model has 2 constraints (2 nonlinear) and 0 disjunctions, with
2 variables, of which 1 are binary, 0 are integer, and 1 are continuous.
INFO: NLP 1: Solve relaxed integrality
INFO: NLP 1: OBJ: 1.0 LB: 1.0 UB: inf
INFO: ---MindtPy Master Iteration 0---
INFO: MIP 1: Solve master problem.
WARNING: Empty constraint block written in LP format - solver may error
Traceback (most recent call last):
File "minlpex.py", line 13, in <module>
op.SolverFactory('mindtpy').solve(model, mip_solver='glpk', nlp_solver='ipopt',tee=True)
File "/anaconda3/envs/py36/lib/python3.6/site-packages/pyomo/contrib/mindtpy/MindtPy.py", line 370, in solve
MindtPy_iteration_loop(solve_data, config)
File "/anaconda3/envs/py36/lib/python3.6/site-packages/pyomo/contrib/mindtpy/iterate.py", line 30, in MindtPy_iteration_loop
handle_master_mip_optimal(master_mip, solve_data, config)
File "/anaconda3/envs/py36/lib/python3.6/site-packages/pyomo/contrib/mindtpy/mip_solve.py", line 62, in handle_master_mip_optimal
config)
File "/anaconda3/envs/py36/lib/python3.6/site-packages/pyomo/contrib/gdpopt/util.py", line 199, in copy_var_list_values
v_to.set_value(value(v_from, exception=False))
File "/anaconda3/envs/py36/lib/python3.6/site-packages/pyomo/core/base/var.py", line 173, in set_value
if valid or self._valid_value(val):
File "/anaconda3/envs/py36/lib/python3.6/site-packages/pyomo/core/base/var.py", line 185, in _valid_value
"domain %s" % (val, type(val), self.domain))
ValueError: Numeric value `0.22709088987977885` (<class 'float'>) is not in domain Binary
So I was a little confused, since this was a code provided, I would not expect it to error like this. So I feel like I'm messing something up or I am missing some required library?
Thanks a lot.
Looks like something must be wrong with the conda pyomo install or ipopt install.
When I reinstalled using pip for ipopt and compiling pyomo from github source everything works fine.

Unsolved Reference IP() and TCP() When Using Scapy

I copied the codes from the example to learn scapy. But realized the IDE showed the error with unsolved reference for IP() & TCP(). Anyone know how to fix this?
Here are the codes:
#! /usr/bin/env python
from scapy.all import *
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)
dst_ip = "10.0.0.1"
src_port = RandShort()
dst_port=80
tcp_connect_scan_resp = sr1(IP(dst=dst_ip)/TCP(sport=src_port,dport=dst_port,flags="S"), timeout=10)
if(str(type(tcp_connect_scan_resp))==""):
print("Closed")
elif(tcp_connect_scan_resp.haslayer(TCP)):
if(tcp_connect_scan_resp.getlayer(TCP).flags == 0x12):
send_rst =sr(IP(dst=dst_ip)/TCP(sport=src_port,dport=dst_port,flags="AR"),timeout=10)
print("Open")
elif (tcp_connect_scan_resp.getlayer(TCP).flags ==0x14):
print("Closed")
I'm using Pycharm IDE. Python2.7 and scapy 2.4.0. I searched on stackoverflow and found someone asked the same question before but no answer.....
Here is the error after I tried to run the codes:
/Users/chenneyhuang/PycharmProjects/Scanner/venv/bin/python /Users/chenneyhuang/PycharmProjects/Scanner/TCP.py
Traceback (most recent call last):
File "/Users/chenneyhuang/PycharmProjects/Scanner/TCP.py", line 12, in <module>
tcp_connect_scan_resp = sr1(IP(dst=dst_ip)/TCP(sport=src_port,dport=dst_port,flags="S"), timeout=10)
File "/Users/chenneyhuang/PycharmProjects/Scanner/venv/lib/python2.7/site-packages/scapy/sendrecv.py", line 393, in sr1
s=conf.L3socket(promisc=promisc, filter=filter, nofilter=nofilter, iface=iface)
File "/Users/chenneyhuang/PycharmProjects/Scanner/venv/lib/python2.7/site-packages/scapy/arch/bpf/supersocket.py", line 58, in __init__
(self.ins, self.dev_bpf) = get_dev_bpf()
File "/Users/chenneyhuang/PycharmProjects/Scanner/venv/lib/python2.7/site-packages/scapy/arch/bpf/core.py", line 98, in get_dev_bpf
raise Scapy_Exception("No /dev/bpf handle is available !")
scapy.error.Scapy_Exception: No /dev/bpf handle is available !
Process finished with exit code 1
I answered the same Unsolved Reference issue last week here:
vscode import error: from scapy.all import IP
In short, don't worry about that error, it's a limitation of Pylint (or similar). I propose a workaround in the other question, if you'd like to remove the error/warning.
For the No /dev/bpf handle is available error, have you tried running the script as root? I see that suggested as a solution over on this GitHub issue: https://github.com/secdev/scapy/issues/1343

Keras:Vgg16 -- Error in `decode_predictions'

I am trying to perform an image classification task using a pre-trained VGG16 model in Keras. The code I wrote, following the instructions in the Keras application page, is:
from keras.applications.vgg16 import VGG16
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions
import numpy as np
model = VGG16(weights='imagenet', include_top=True)
img_path = './train/cat.1.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
(inID, label) = decode_predictions(features)[0]
which is quite similar to the code shown in this question already asked in the forum. But in spite of having the include_top parameter as True, I am getting the following error:
Traceback (most recent call last):
File "vgg16-keras-classifier.py", line 14, in <module>
(inID, label) = decode_predictions(features)[0]
ValueError: too many values to unpack
Any help will be deeply appreciated! Thanks!
It's because (according to a function definition which might be found here) a function decode_predictions returns a triple (class_name, class_description, score). This why it claims that there are too many values to unpack.

Error in TensorFlow program

I am learning TensorFlow and I stumble upon this example code for creating simple multi-layer sigmoid network. The program in the link is for MNIST database and hand written digit classification.
I want to train a network for regression task. I have 30 inputs(float) which is used to predict one output(float). So I tweaked the code to change the task from classification to regression.
My problem is that I'm getting an error in tf.Session.run(). The code and the error log is given below.
import test2
import tensorflow as tf
feed_input = test2.read_data_sets()
learning_rate = 0.001
training_epochs = 100
batch_size = 1716
display_step = 1
n_hidden_1 = 256
n_hidden_2 = 256
n_hidden_3 = 256
n_input = 30
x = tf.placeholder("float", [None, n_input])
y = tf.placeholder("float", [None])
def multilayer_perceptron(_X, _weights, _biases):
#Hidden layer with RELU activation
layer_1 = tf.nn.relu(tf.add(tf.matmul(_X, _weights['h1']), _biases['b1']))
#Hidden layer with RELU activationn_hidden_3
layer_2 = tf.nn.relu(tf.add(tf.matmul(layer_1, _weights['h2']), _biases['b2']))
layer_3 = tf.nn.relu(tf.add(tf.matmul(layer_2, _weights['h3']), _biases['b3']))
return tf.matmul(layer_3, weights['out']) + biases['out']
weights = {
'h1': tf.Variable(tf.random_normal([n_input, n_hidden_1])),
'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
'h3': tf.Variable(tf.random_normal([n_hidden_2, n_hidden_3])),
'out': tf.Variable(tf.random_normal([n_hidden_3, 1]))
}
biases = {
'b1': tf.Variable(tf.random_normal([n_hidden_1])),
'b2': tf.Variable(tf.random_normal([n_hidden_2])),
'b3': tf.Variable(tf.random_normal([n_hidden_3])),
'out': tf.Variable(tf.random_normal([1]))
}
pred = multilayer_perceptron(x, weights, biases)
n_pred = tf.mul(pred, tf.convert_to_tensor(10000.00))
cost = tf.nn.sigmoid_cross_entropy_with_logits(n_pred, y)
optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cost)
init = tf.initialize_all_variables()
with tf.Session() as sess:
sess.run(init)
# Training cycle
for epoch in range(training_epochs):
avg_cost = 0
total_batch = int(feed_input.train._num_examples / batch_size)
# Loop over all batches
for i in range(total_batch):
batch_xs, batch_ys = feed_input.train.next_batch(batch_size)
# Fit training using batch data
sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})
# Compute average loss
avg_cost += sess.run(cost, feed_dict={x: batch_xs, y: batch_ys}) / total_batch
# Display logs per epoch step
if epoch % display_step == 0:
print "Epoch:", '%04d' % (epoch+1), "cost=", "{:.9f}".format(avg_cost)
print "Optimization Finished!"
runfile('/mnt/sdb6/Projects/StockML/demo1.py',
wdir='/mnt/sdb6/Projects/StockML')
Reloaded modules: tensorflow.python.ops.nn_grad,
tensorflow.python.training.momentum,
. . . .
tensorflow.python.util.protobuf,
google.protobuf.internal.enum_type_wrapper,
tensorflow.python.ops.nn_ops, tensorflow.python,
tensorflow.python.platform.test,
google.protobuf.internal.api_implementation, tensorflow,
google.protobuf.internal.encoder
Traceback (most recent call last):
File "", line 1, in
runfile('/mnt/sdb6/Projects/StockML/demo1.py', wdir='/mnt/sdb6/Projects/StockML')
File
"/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py",
line 685, in runfile
execfile(filename, namespace)
File
"/usr/lib/python2.7/dist-packages/spyderlib/widgets/externalshell/sitecustomize.py",
line 78, in execfile
builtins.execfile(filename, *where)
File "/mnt/sdb6/Projects/StockML/demo1.py", line 69, in
sess.run(optimizer, feed_dict={x: batch_xs, y: batch_ys})
File
"/home/rammak/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py",
line 345, in run
results = self._do_run(target_list, unique_fetch_targets, feed_dict_string)
File
"/home/rammak/.local/lib/python2.7/site-packages/tensorflow/python/client/session.py",
line 406, in _do_run
except tf_session.StatusNotOK as e:
AttributeError: 'module' object has no attribute 'StatusNotOK'
Protobuf error is usually an installation issue , run it in a virtual env
# On Mac:
$ sudo easy_install pip # If pip is not already installed
$ sudo pip install --upgrade virtualenv
Next, set up a new virtualenv environment. To set it up in the directory ~/tensorflow, run:
$ virtualenv --system-site-packages ~/tensorflow
$ cd ~/tensorflow
Then activate the virtualenv:
$ source bin/activate # If using bash
$ source bin/activate.csh # If using csh
(tensorflow)$ # Your prompt should change
Inside the virtualenv, install TensorFlow:
(tensorflow)$ pip install --upgrade https://storage.googleapis.com/tensorflow/mac/tensorflow-0.5.0-py2-none-any.whl
You can then run your TensorFlow program like:
(tensorflow)$ python tensorflow/models/image/mnist/convolutional.py
# When you are done using TensorFlow:
(tensorflow)$ deactivate # Deactivate the virtualenv
$ # Your prompt should change back
If you just begin to learn TensorFlow, I would suggest you trying out examples in TensorFlow/skflow first and then once you are more familiar with TensorFlow it would be fairly easy for you to insert TensorFlow code to build a custom model you want (there are also examples for this).
Hope those examples for images and text understanding could get you started and let us know if you encounter any issues! (post issues or tag skflow in SO).
Change your logging level from WARN to INFO, so that can get a better visualization of the error you're getting.
For knowledge purpose, you should know there are 5 logging levels:
DEBUG
INFO
WARN
ERROR
FATAL