Posts

Showing posts with the label Python

Argparse Optional Positional Arguments?

Answer : Use nargs='?' (or nargs='*' if you need more than one dir) parser.add_argument('dir', nargs='?', default=os.getcwd()) extended example: >>> import os, argparse >>> parser = argparse.ArgumentParser() >>> parser.add_argument('-v', action='store_true') _StoreTrueAction(option_strings=['-v'], dest='v', nargs=0, const=True, default=False, type=None, choices=None, help=None, metavar=None) >>> parser.add_argument('dir', nargs='?', default=os.getcwd()) _StoreAction(option_strings=[], dest='dir', nargs='?', const=None, default='/home/vinay', type=None, choices=None, help=None, metavar=None) >>> parser.parse_args('somedir -v'.split()) Namespace(dir='somedir', v=True) >>> parser.parse_args('-v'.split()) Namespace(dir='/home/vinay', v=True) >>> parser.parse_args(''.split()) Nam...

Bytes Vs Bytearray In Python 2.6 And 3

Answer : For (at least) Python 3.7 According to the docs: bytes objects are immutable sequences of single bytes bytearray objects are a mutable counterpart to bytes objects. And that's pretty much it as far as bytes vs bytearray . In fact, they're fairly interchangeable and designed to flexible enough to be mixed in operations without throwing errors. In fact, there is a whole section in the official documentation dedicated to showing the similarities between the bytes and bytearray apis. Some clues as to why from the docs: Since many major binary protocols are based on the ASCII text encoding, bytes objects offer several methods that are only valid when working with ASCII compatible data and are closely related to string objects in a variety of other ways. In Python 2.6 bytes is merely an alias for str . This "pseudo type" was introduced to [partially] prepare programs [and programmers!] to be converted/compatible with Python 3.0 where there is a ...

Boxplot Of Multiple Columns Of A Pandas Dataframe On The Same Figure (seaborn)

Image
Answer : The seaborn equivalent of df.boxplot() is sns.boxplot(x="variable", y="value", data=pd.melt(df)) Complete example: import numpy as np; np.random.seed(42) import pandas as pd import matplotlib.pyplot as plt import seaborn as sns df = pd.DataFrame(data = np.random.random(size=(4,4)), columns = ['A','B','C','D']) sns.boxplot(x="variable", y="value", data=pd.melt(df)) plt.show() This works because pd.melt converts a wide-form dataframe A B C D 0 0.374540 0.950714 0.731994 0.598658 1 0.156019 0.155995 0.058084 0.866176 2 0.601115 0.708073 0.020584 0.969910 3 0.832443 0.212339 0.181825 0.183405 to long-form variable value 0 A 0.374540 1 A 0.156019 2 A 0.601115 3 A 0.832443 4 B 0.950714 5 B 0.155995 6 B 0.708073 7 B 0.212339 8 C 0.731994 9 C ...

Adding L1/L2 Regularization In PyTorch?

Answer : Following should help for L2 regularization: optimizer = torch.optim.Adam(model.parameters(), lr=1e-4, weight_decay=1e-5) This is presented in the documentation for PyTorch. Have a look at http://pytorch.org/docs/optim.html#torch.optim.Adagrad. You can add L2 loss using the weight decay parameter to the Optimization function. For L2 regularization, l2_lambda = 0.01 l2_reg = torch.tensor(0.) for param in model.parameters(): l2_reg += torch.norm(param) loss += l2_lambda * l2_reg References: https://discuss.pytorch.org/t/how-does-one-implement-weight-regularization-l1-or-l2-manually-without-optimum/7951. http://pytorch.org/docs/master/torch.html?highlight=norm#torch.norm.

Calculate The Cumulative Distribution Function (CDF) In Python

Image
Answer : (It is possible that my interpretation of the question is wrong. If the question is how to get from a discrete PDF into a discrete CDF, then np.cumsum divided by a suitable constant will do if the samples are equispaced. If the array is not equispaced, then np.cumsum of the array multiplied by the distances between the points will do.) If you have a discrete array of samples, and you would like to know the CDF of the sample, then you can just sort the array. If you look at the sorted result, you'll realize that the smallest value represents 0% , and largest value represents 100 %. If you want to know the value at 50 % of the distribution, just look at the array element which is in the middle of the sorted array. Let us have a closer look at this with a simple example: import matplotlib.pyplot as plt import numpy as np # create some randomly ddistributed data: data = np.random.randn(10000) # sort the data: data_sorted = np.sort(data) # calculate the proportional ...

Can't Install PyQt5 Using Pip

Answer : Try this: python3 -m pip install PyQt5 You need to upgrade your pip pip install --user --upgrade pip Then do a fresh install pip install PyQt5 or pip3 install PyQt5

AssertEquals Vs. AssertEqual In Python

Answer : Good question! Actually, in Python 2.6, both assertEqual and assertEquals are convenience aliases to failUnlessEqual . The source declares them thus: # Synonyms for assertion methods assertEqual = assertEquals = failUnlessEqual In Python 3, to your point, failUnlessEqual is explicitly deprecated. assertEquals carries this comment :-) # Synonyms for assertion methods # The plurals are undocumented. Keep them that way to discourage use. # Do not add more. Do not remove. # Going through a deprecation cycle on these would annoy many people. So, the upshot appears to be that you should use whatever you like for Python 2.x, but tend toward assertEqual for Python 3. A 3.3 update: From 26.3.7.1.1. Deprecated aliases : For historical reasons, some of the TestCase methods had one or more aliases that are now deprecated. The following table lists the correct names along with their deprecated aliases: Method Name | Deprecated alias | ...

Apply MatplotLib Or Custom Colormap To OpenCV Image

Image
Answer : For Python >= 2.7, cmapy packages this functionality in a convenient way. Install it with: Python 2.7: pip install cmapy Python 3.x: pip3 install cmapy Or, for Anaconda (from conda-forge): conda install -c conda-forge cmapy And use it like this: import cv2 import matplotlib.pyplot as plt import cmapy # Read image. img = cv2.imread('imgs/woman.png') # Colorize. img_colorized = cv2.applyColorMap(img, cmapy.cmap('viridis')) # Display plt.imshow(img_colorized) plt.show() Different colormaps give something like this: See all the available colormaps in action here. Disclaimer: I wrote cmapy (because I needed this functionality for another project), and internally, it does pretty much the same as the other answers. In recent versions of OpenCV (starting with 3.3), there's an overload of applyColorMap , which allows you to provide a custom colormap (either 1 or 3 channel). I've modified verified.human's code to sim...

Change Xticklabels Fontsize Of Seaborn Heatmap

Image
Answer : Consider calling sns.set(font_scale=1.4) before plotting your data. This will scale all fonts in your legend and on the axes. My plot went from this, To this, Of course, adjust the scaling to whatever you feel is a good setting. Code: sns.set(font_scale=1.4) cmap = sns.diverging_palette(h_neg=210, h_pos=350, s=90, l=30, as_cmap=True) sns.clustermap(data=corr, annot=True, fmt='d', cmap="Blues", annot_kws={"size": 16}) Or just use the set_xticklabels: g = sns.clustermap(data=corr_s, annot=True, fmt='d',cmap = "Blues") g.ax_heatmap.set_xticklabels(g.ax_heatmap.get_xmajorticklabels(), fontsize = 16) To get different colors for the ticklabels: import matplotlib.cm as cm colors = cm.rainbow(np.linspace(0, 1, corr_s.shape[0])) for i, ticklabel in enumerate(g.ax_heatmap.xaxis.get_majorticklabels()): ticklabel.set_color(colors[i])

Circular List Iterator In Python

Answer : Use itertools.cycle , that's its exact purpose: from itertools import cycle lst = ['a', 'b', 'c'] pool = cycle(lst) for item in pool: print item, Output: a b c a b c ... (Loops forever, obviously) In order to manually advance the iterator and pull values from it one by one, simply call next(pool) : >>> next(pool) 'a' >>> next(pool) 'b' The correct answer is to use itertools.cycle. But, let's assume that library function doesn't exist. How would you implement it? Use a generator: def circular(): while True: for connection in ['a', 'b', 'c']: yield connection Then, you can either use a for statement to iterate infinitely, or you can call next() to get the single next value from the generator iterator: connections = circular() next(connections) # 'a' next(connections) # 'b' next(connections) # 'c' next(con...

Bar Graph From Dataframe Groupby

Image
Answer : copying data from your link and running df = pd.read_clipboard() then using your code df = df.replace(np.nan,0) df = df.groupby(['home_team'])['arrests'].mean() df.plot.bar() Good one by @piRSuared, and I just buitified his answer :) ## referenced to the answer by @piRSquared df = df.replace(np.nan,0) df = df.groupby(['home_team'])['arrests'].mean() ax = df.plot(kind='bar', figsize=(10,6), color="indigo", fontsize=13); ax.set_alpha(0.8) ax.set_title("My Bar Plot", fontsize=22) ax.set_ylabel("Some Heading on Y-Axis", fontsize=15); plt.show()

Avoid Division By Zero In Numpy.where()

Image
Answer : Simply initialize output array with the fallback values (condition-not-satisfying values) or array and then mask to select the condition-satisfying values to assign - out = a.copy() out[mask] /= b[mask] If you are looking for performance, we can use a modified b for the division - out = a / np.where(mask, b, 1) Going further, super-charge it with numexpr for this specific case of positive values in b (>=0) - import numexpr as ne out = ne.evaluate('a / (1 - mask + b)') Benchmarking Code to reproduce the plot: import perfplot import numpy import numexpr numpy.random.seed(0) def setup(n): a = numpy.random.rand(n) b = numpy.random.rand(n) b[b < 0.3] = 0.0 mask = b > 0 return a, b, mask def copy_slash(data): a, b, mask = data out = a.copy() out[mask] /= b[mask] return out def copy_divide(data): a, b, mask = data out = a.copy() return numpy.divide(a, b, out=out, where=mask) def slash_where(...

Append Existing Excel Sheet With New Dataframe Using Python Pandas

Image
Answer : A helper function for appending DataFrame to existing Excel file: def append_df_to_excel(filename, df, sheet_name='Sheet1', startrow=None, truncate_sheet=False, **to_excel_kwargs): """ Append a DataFrame [df] to existing Excel file [filename] into [sheet_name] Sheet. If [filename] doesn't exist, then this function will create it. Parameters: filename : File path or existing ExcelWriter (Example: '/path/to/file.xlsx') df : dataframe to save to workbook sheet_name : Name of sheet which will contain DataFrame. (default: 'Sheet1') startrow : upper left cell row to dump data frame. Per default (startrow=None) calculate the last row in the existing DF and write to the next row... truncate_sheet : truncate (remove and recreate) [sheet_name] before ...

Alternative To Execfile In Python 3?

Answer : The 2to3 script replaces execfile(filename, globals, locals) by exec(compile(open(filename, "rb").read(), filename, 'exec'), globals, locals) This seems to be the official recommendation. You may want to use a with block to ensure that the file is promptly closed again: with open(filename, "rb") as source_file: code = compile(source_file.read(), filename, "exec") exec(code, globals, locals) You can omit the globals and locals arguments to execute the file in the current scope, or use exec(code, {}) to use a new temporary dictionary as both the globals and locals dictionary, effectively executing the file in a new temporary scope. execfile(filename) can be replaced with exec(open(filename).read()) which works in all versions of Python Newer versions of Python will warn you that you didn't close that file, so then you can do this is you want to get rid of that warning: with open(filename) as infile: ...

Catch Multiple Exceptions In One Line (except Block)

Answer : From Python Documentation: An except clause may name multiple exceptions as a parenthesized tuple, for example except (IDontLikeYouException, YouAreBeingMeanException) as e: pass Or, for Python 2 only: except (IDontLikeYouException, YouAreBeingMeanException), e: pass Separating the exception from the variable with a comma will still work in Python 2.6 and 2.7, but is now deprecated and does not work in Python 3; now you should be using as . How do I catch multiple exceptions in one line (except block) Do this: try: may_raise_specific_errors(): except (SpecificErrorOne, SpecificErrorTwo) as error: handle(error) # might log or have some other default behavior... The parentheses are required due to older syntax that used the commas to assign the error object to a name. The as keyword is used for the assignment. You can use any name for the error object, I prefer error personally. Best Practice To do this in a manner currently and ...

Anaconda Update All Possible Packages?

Answer : TL;DR: dependency conflicts: Updating one requires (by it's requirements) to downgrade another You are right: conda update --all is actually the way to go 1 . Conda always tries to upgrade the packages to the newest version in the series (say Python 2.x or 3.x). Dependency conflicts But it is possible that there are dependency conflicts (which prevent a further upgrade). Conda usually warns very explicitly if they occur. e.g. X requires Y <5.0, so Y will never be >= 5.0 That's why you 'cannot' upgrade them all. Resolving To add: maybe it could work but a newer version of X working with Y > 5.0 is not available in conda. It is possible to install with pip, since more packages are available in pip. But be aware that pip also installs packages if dependency conflicts exist and that it usually breaks your conda environment in the sense that you cannot reliably install with conda anymore. If you do that, do it as a last resort and af...

Calling Private Function Within The Same Class Python

Answer : There is no implicit this-> in Python like you have in C/C++ etc. You have to call it on self . class Foo: def __bar(self, arg): #do something def baz(self, arg): self.__bar(arg) These methods are not really private though. When you start a method name with two underscores Python does some name mangling to make it "private" and that's all it does, it does not enforce anything like other languages do. If you define __bar on Foo , it is still accesible from outside of the object through Foo._Foo__bar . E.g., one can do this: f = Foo() f._Foo__bar('a') This explains the "odd" identifier in the error message you got as well. You can find it here in the docs. __bar is "private" (in the sense that its name has been mangled), but it's still a method of Foo , so you have to reference it via self and pass self to it. Just calling it with a bare __bar() won't work; you have to call i...

Append Multiple Pandas Data Frames At Once

Answer : I think you can use concat : print pd.concat([t1, t2, t3, t4, t5]) Maybe you can ignore_index : print pd.concat([t1, t2, t3, t4, t5], ignore_index=True) More info in docs. Have you simply tried using a list as argument of append? Or am I missing anything? import numpy as np import pandas as pd dates = np.asarray(pd.date_range('1/1/2000', periods=8)) df1 = pd.DataFrame(np.random.randn(8, 4), index=dates, columns=['A', 'B', 'C', 'D']) df2 = df1.copy() df3 = df1.copy() df = df1.append([df2, df3]) print df

Basic 1d Convolution In Tensorflow

Answer : I am sorry to say that, but your first code was almost right. You just inverted x and phi in tf.nn.conv2d : g = tf.Graph() with g.as_default(): # data shape is "[batch, in_height, in_width, in_channels]", x = tf.Variable(np.array([0.0, 0.0, 0.0, 0.0, 1.0]).reshape(1, 1, 5, 1), name="x") # filter shape is "[filter_height, filter_width, in_channels, out_channels]" phi = tf.Variable(np.array([0.0, 0.5, 1.0]).reshape(1, 3, 1, 1), name="phi") conv = tf.nn.conv2d( x, phi, strides=[1, 1, 1, 1], padding="SAME", name="conv") Update: TensorFlow now supports 1D convolution since version r0.11, using tf.nn.conv1d . I previously made a guide to use them in the stackoverflow documentation (now extinct) that I'm pasting here: Guide to 1D convolution Consider a basic example with an input of length 10 , and dimension 16 . The batch size is 32 . We there...

AttributeError: 'str' Object Has No Attribute 'items'

Answer : You are passing in a string ; headers can't ever be a JSON encoded string, it is always a Python dictionary. The print results are deceptive; JSON encoded objects look a lot like Python dictionary representations but they are far from the same thing. The requests API clearly states that headers must be a dictionary: headers – (optional) Dictionary of HTTP Headers to send with the Request . JSON data is something you'd send as content to another server, not something you'd use to communicate with a Python API. I had this issue and I needed to make the header with a content type and pass in a data element as json. import requests import json headerInfo = {'content-type': 'application/json' } payload = {'text': 'okay!!!', 'auth_token': 'aasdasdasdasd'} jLoad = json.dumps(payload) r = requests.post('http://example.com:3030/widgets/init', headers=headerInfo, data=jLoad) print r.text...