cannot use line_profiler in jupyter/python2.7.5 - python-2.7

I'm trying to use the line magic %lprun in jupyter notebook running a python 2.7.5 kernel.
The code I'm trying to run is the following:
%load_ext line_profiler
distance = 20
veg_slope = 0
slope = 10
%lprun test = bal.run('forest', veg_slope, slope, distance, FFDI=100)
The bal.run code is a bit complicated, but executing the code with those parameters will output the following tuple:
(35.02405579440225, 'BAL-40')
However if I try to use the %lprun magic I get the following error:
File "<string>", line 1
test = bal.run(\'forest\', veg_slope, slope, distance, FFDI=100)
^
SyntaxError: unexpected character after line continuation character
Not sure what is happening there, but as a test I tried to run a simple python function like print together with the line_profiler, and that worked.
Anyone has any idea of what the problem could be?

I have just had the same issue in Jupyter / Python 3.6 using example code from a presentation available here.
I'm still a newbie but the traceback ends with the same syntax error:
File "", line 1
df[\'high_rate_normalized\'] = normalize(df, df[\'high_rate\'])
^
SyntaxError: unexpected character after line continuation character

Change to double quotes "
%lprun test = bal.run("forest", veg_slope, slope, distance, FFDI=100)

Related

Windows Error using XGBoost with python

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.

How to get a row from a file that starts with multiple prefixes

I'm trying to get the rows that starts with 's1', 's2'. Below is the code.
import os
f = open("test.csv","r")
lines = f.readlines()
f.close()
f = open("test.csv","w")
for line in lines:
if (line.startsWith("s1") || line.startsWith("s2"))
f.write(line)
f.close()
When I run this script I'm getting syntax error near '||'.
I looked into this link and made changes accordingly -
How to check if a string starts with one of several prefixes?
Please let me know if I'm doing anything wrong.
FYI, I'm using Python 2.7.
Thanks!

error bash: syntax error near unexpected token `(' my code is correct

I am trying to implement a hash function and here is my code:
import BitVector
import glob
path = '/home/vguda/Desktop/.txt'
files=glob.glob(path)
hash = BitVector.BitVector(size = 32)
hash.reset(0)
i = 0
for file in files:
bv = BitVector.BitVector( filename = file )
while 1 :
bv1 = bv.read_bits_from_file(8)
if str(bv1) == "":
break
hash[0:8] = bv1 ^ hash[0:8]
hash >> 8
i = i+1
hash_str = ""
hash_str = str( hash )
text_file = open("/home/vguda/Desktop/result.txt ","w")
text_file.write("Hash Code is %s" %hash_str)
text_file.close()
print hash
print (i)
The displayed error is:
"bash: syntax error near unexpected token `('
First, perhaps this happened in copy and paste, but your indenting in your loop is all messed up, I'm not sure which blocks go where.
When you run things in the shell, you need to either tell python to run it:
python myscript.py
Or, put the following line as the first thing in your program to tell bash to run it as a python program:
#!/usr/bin/python
Currently, bash is trying to run your python program as a bash script, and obviously running into syntax errors.

EOF Error in Python 2

I am solving a problem on http://hackerrank.com using Python 2
The compiler is giving an error
Traceback (most recent call last):
File "/run-Lx3mHJ3G2jHRLRW9bjbX/solution.py", line 4, in
t = raw_input()
EOFError: EOF when reading a line
This is the code :
import sys
a = []
while 1:
t = raw_input()
if t=="":
break
else:
s = [i for i in t]
s.reverse()
a.append(s)
a.reverse()
for i in a:
for j in i:
sys.stdout.write(j)
sys.stdout.write('\n')
When I run it on my computer, it works fine.
Is it a problem I should report with the HackerRank interpreter or am I doing something wrong?
For sake of complete information, I already tried using "input()", "str(input())" and other possible variants.
HackerRank seems not to support the python idiom of repeating raw_input() until it gets an empty line. HackerRank apparently requires the submitted code to use the test description parameters in the header section (first line or two of input) to control the number of lines read.
Attempting to read past the last expected input line triggered a similar EOFError in my trials:
...
def main():
lines = []
line = raw_input()
while line:
lines.append(line)
line = raw_input() # line 232
...
resulted in
Status: EOFError thrown on line 232
Rewriting the input code to read just the expected number of lines was enough for the revised submission to pass. For example, for the 'Service Lane' warmup exercise in the Algorithms section:
...
first_line = raw_input()
freeway_length, testcase_count = parse_session_controls(first_line)
second_line = raw_input()
widths = parse_widths(second_line, freeway_length)
for _unused in range(testcase_count):
testcase_line = raw_input()
entrance_num, exit_num = parse_testcase(testcase_line, freeway_length)
print(measure_bottleneck(widths, entrance_num, exit_num))
...

Sublime Text 2 EOFError

I'm studying Python using Sublime Text 2.
I typed just the following two statements:
usr = raw_input('input any letters: ')
print usr
After pressing CMD+B, the following error message occurred.
input any letters: Traceback (most recent call last):
File "/Users/jun/Documents/workspace/studyPython/test.py", line 1, in <module>
usr = raw_input('input any letters: ')
EOFError: EOF when reading a line
[Finished in 0.3s with exit code 1]
How can I fix it? (I'm using Python 2.7.3 in OS X 10.8.2)
The problem is that raw_input isn't getting any input when you run the file in Sublime Text 2, so Python is throwing an error.
In the console that appears (where you see the error), there isn't anywhere for you to type in your arguments. You need to run the script at the command line to make it work. At a shell prompt (in OS X, probably Terminal, found in /Applications/Utilities/Terminal.app), type the following line:
python /path/to/script/test.py
The following line then appears:
input any letters:
with a cursor at the end of the line. This is prompting you to enter your raw_input, which allows it to set the use variable. You then type some text, for example:
input any letters: this is some text
and Python will print what you just typed:
this is some text
This doesn't work in SL2, because SL2 (afaik) doesn't have a way to offer you that prompt for raw_input.