eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I have four pandas Series and I plot them using a violin plot as follows: import seaborn seaborn.violinplot([X1['total'], X2['total'], X3['total'], X4['total']])  I would like to plot the values on top of the violin plot so I added: seaborn.stripplot([X1['total'], X2['total'], X3['total'], X4['total']])  But this gives:  It plots all the points over the first violin plot. What am I doing wrong?

Answer: Currently (seaborn 0.12.1), sns.violinplot seems to accept a list of lists as data, and interprets it similar to a wide form dataframe. sns.striplot (as well as sns.swarmplot), however, interpret this as a single dataset. On the other hand, sns.stripplot accepts a dictionary of lists and interprets it as a wide form dataframe. But sns.violinplot refuses to work with that dictionary. Note that seaborn is being actively reworked internally to allow a wider set of data formats, so one of the future versions will tackle this issue. So, a list of lists for the violin plot, and a dictionary for the stripplot allows combining both: import seaborn as sns import pandas as pd import numpy as np X1, X2, X3, X4 = [pd.DataFrame({'total': np.random.normal(.1, 1, np.random.randint(99, 300)).cumsum()}) for _ in range(4)] ax = sns.violinplot([X1['total'], X2['total'], X3['total'], X4['total']], inner=None) sns.stripplot({0: X1['total'], 1: X2['total'], 2: X3['total'], 3: X4['total']}, edgecolor='black', linewidth=1, palette=['white'] * 4, ax=ax)
Question: Consider this class: class Product(models.Model): name = models.Charfield(verbose_name="Product Name", max_length=255) class Meta: verbose_name = "Product Name"  I looked at the Django docs and it says:  For verbose_name in a field declaration: "A human-readable name for the field." For verbose_name in a Meta declaration: "A human-readable name for the object, singular".  When would I see either verbose_name manifest at runtime? In a form render? In Django admin?

Answer: The verbose_name in the Meta deals with the name of the model, not field(s) of that model. It thus likely should be 'Product', not 'Product Name': class Product(models.Model): name = models.Charfield(verbose_name='Product Name', max_length=255) class Meta: verbose_name = 'Product' This thus specifies the table name in the model admin, whereas the verbose_name of the field(s) will show up in ModelForms and when you display or edit records.
Question: I'm doing something wrong in the code below. I have a method (update_dictonary) that changes a value or values in a dictionary based on what is specificed in a tuple (new_points). Before I update the dictionary, I want to save that version in a list (history) in order to be able to access previous versions. However, my attempt below updates all dictionaries in history to be like the latest version. I can't figure out what I'm doing wrong here. test_dict = {'var0':{'var1':{'cond1':1, 'cond2':2, 'cond3':3} } } class version_control: def __init__ (self, dictionary): self.po = dictionary self.history = list() self.version = 0 def update_dictionary(self, var0, var1, new_points): po_ = self.po self.history.append(po_) for i in new_points: self.po[var0][var1][i[0]] = i[1] self.version += 1 def get_history(self, ver): return self.history[ver]  a = version_control(test_dict) new_points = [('cond1', 2), ('cond2', 0)] a.update_dictionary('var0', 'var1', new_points) new_points = [('cond3', -99), ('cond2', 1)] a.update_dictionary('var0', 'var1', new_points) print(a.get_history(0)) print(a.get_history(1)) 

Answer: Try this from copy import deepcopy ... def update_dictionary(self, var0, var1, new_points): po_ = deepcopy(self.po) self.history.append(po_) for i in new_points: self.po[var0][var1][i[0]] = i[1] self.version += 1 ...  The problem here is that when you assign po_= self.po you expect po_ to a new variable with a new memory id but actually, you just make a shallow copy(same memory id) of your dictionary. This means if you update the self.po then op_ will automatically update.  To solve this problem by using deepcopy from the copy module(Built-in). It will create a new variable.  You can use this code to save the data into a JSON file. import json class version_control: def __init__(self, dictionary): self.po = dictionary self.version = 0 self.ZEROth_version() def update_dictionary(self, var0, var1, new_points, version=None): self.version += 1 for i in new_points: self.po[var0][var1][i[0]] = i[1] # set the ver to version if given else set to self.version ver = self.version if version is None else version with open("version.json", "r") as jsonFile: # loading the data from the file. data = json.load(jsonFile) data[str(ver)] = self.po with open("version.json", "w") as jsonFile: # save the updated dictionary in json file json.dump(data, jsonFile, indent=4) def get_history(self, ver): try: with open("version.json", "r") as jsonFile: # I don't use .get here. I will catch key errors in except block. I don't want to add an if statement to check for None. But string if you want you can add that. return json.load(jsonFile)[str(ver)] # Checking if the file not exists or is empty except (json.decoder.JSONDecodeError, FileNotFoundError, KeyError) as e: print("File or Version not found") def ZEROth_version(self): with open("version.json", "w") as f: data = {0: self.po} json.dump(data, f, indent=4)  I have explained some main points if you want more explanation then comment, I will reply as soon as possible.
Question: I installed a package with poetry add X, and so now it shows up in the toml file and in the venv (mine's at .venv/lib/python3.10/site-packages/). Now to remove that package, I could use poetry remove X and I know that would work properly. But sometimes, it's easier to just go into the toml file and delete the package line there. So that's what I tried by removing the line for X. I then tried doing poetry install but that didn't do anything When I do ls .venv/lib/python3.10/site-packages/, I still see X is installed there. I also tried poetry lock but no change with that either. So is there some command to take the latest toml file and clean up packages from being installed that are no longer present in the toml?

Answer: When ever you manual edit the pyproject.toml you have to run poetry lock --no-update to sync the locked dependencies in the poetry.lock file. This is necessary because Poetry will use the resolved dependencies from the poetry.lock file on install if this file is available. Once the pyproject.toml and poetry.lock file are in sync run poetry install --sync to get the venv in sync with the poetry.lock file.
Question: I have two lists A1 and J1 containing many sublists. From each sublist of A1[0], I want to remove the element specified in J1[0]. I present the current and expected outputs. A1 = [[[1, 3, 4, 6], [0, 2, 3, 5]], [[1, 3, 4, 6], [1, 3, 4, 6]]] J1 = [[[1], [2]], [[1], [4]]] arD = [] for i in range(0,len(A1)): for j in range(0,len(J1)): C=set(A1[i][j])-set(J1[i][j]) D=list(C) arD.append(D) D=list(arD) print("D =",D)  The current output is D = [[3, 4, 6], [0, 3, 5], [3, 4, 6], [1, 3, 6]]  The expected output is D = [[[3, 4, 6], [0, 3, 5]],[[3, 4, 6],[1, 3, 6]]] 

Answer: Code:- A1 = [[[1, 3, 4, 6], [0, 2, 3, 5]], [[1, 3, 4, 6], [1, 3, 4, 6]]] J1 = [[[1], [2]], [[1], [4]]] arD=[] for i in range(0,len(A1)): tmp=[] #Created a tmp variable list for j in range(0,len(J1)): C=set(A1[i][j])-set(J1[i][j]) tmp.append(list(C)) #Appending result in tmp variable arD.append(tmp) #Storing tmp list as a list of lists in arD. print("D =",arD)  Output:- D = [[[3, 4, 6], [0, 3, 5]], [[3, 4, 6], [1, 3, 6]]]
Question: Is it possible to draw 2D and 3D contour plot like this in python.  Sorry I couldn't provide much detail on the plot in terms of mathematical equations and all.

Answer: Use plot_surface along with contour to project the contour. It is not limited to the Z plane; you can do this to the X and Y planes as well. There is an example in the official documentation of Matplotlib: https://matplotlib.org/stable/gallery/mplot3d/contourf3d_2.html#sphx-glr-gallery-mplot3d-contourf3d-2-py Note that an offset is needed to move the contour to the bottom of the 3D plot. You can set the offset equal to the lower bound of the y limit. I created an example: import matplotlib.pyplot as plt import numpy as np x = y = np.arange(-3.0, 3.0, 0.02) X, Y = np.meshgrid(x, y) Z1 = np.exp(-X ** 2 - Y ** 2) Z2 = np.exp(-(X - 1) ** 2 - (Y - 1) ** 2) Z3 = np.exp(-(X + 1) ** 2 - (Y + 1) ** 2) Z = (Z1 - Z2 - Z3) * 2 fig, ax = plt.subplots(subplot_kw={"projection": "3d"}) # draw surface plot surf = ax.plot_surface(X, Y, Z, lw=0.1, cmap='coolwarm', edgecolor='k') # add color bar fig.colorbar(surf, shrink=0.5, aspect=10) # projecting the contour with an offset ax.contour(X, Y, Z, 20, zdir='z', offset=-2, cmap='coolwarm') # match the lower bound of zlim to the offset ax.set(zlim=(-2, 1)) plt.tight_layout() plt.show()
Question: I am trying to scrape lineups from https://www.rotowire.com/hockey/nhl-lineups.php I would like a resulting dataframe like the following     Team Position Player Line     CAR C Sebastian Aho Power Play #1   CAR LW Stefan Noesen Power Play #1     .... This is what I have currently, but am unsure how to get the team and line to matchup with the players/positions as well as put into a dataframe import requests, pandas as pd from bs4 import BeautifulSoup url = "https://www.rotowire.com/hockey/nhl-lineups.php" soup = BeautifulSoup(requests.get(url).text, "html.parser") lineups = soup.find_all('div', {'class':['lineups']})[0] names = lineups.find_all('a', title=True) for name in names: name = name.get('title') print(name) positions = lineups.find_all('div', {'class':['lineup__pos']}) for pos in positions: pos = pos.text print(pos) 

Answer: Try: import pandas as pd import requests from bs4 import BeautifulSoup url = "https://www.rotowire.com/hockey/nhl-lineups.php" soup = BeautifulSoup(requests.get(url).content, "html.parser") all_data = [] for a in soup.select(".lineup__player a"): name = a["title"] pos = a.find_previous("div").text line = a.find_previous(class_="lineup__title").text lineup = a.find_previous(class_="lineup__list")["class"][-1] team = a.find_previous(class_=f"lineup__team {lineup}").img["alt"] all_data.append((team, pos, name, line)) df = pd.DataFrame(all_data, columns=["Team", "Pos", "Player", "Line"]) print(df.to_markdown(index=False))  Prints:     Team Pos Player Line     CAR C Sebastian Aho POWER PLAY #1   CAR LW Stefan Noesen POWER PLAY #1   CAR RW Andrei Svechnikov POWER PLAY #1   CAR LD Brent Burns POWER PLAY #1   CAR RD Martin Necas POWER PLAY #1
Question: Was taking a look at the dropout implementation in flax: def __call__(self, inputs, deterministic: Optional[bool] = None): """Applies a random dropout mask to the input. Args: inputs: the inputs that should be randomly masked. deterministic: if false the inputs are scaled by `1 / (1 - rate)` and masked, whereas if true, no mask is applied and the inputs are returned as is. Returns: The masked inputs reweighted to preserve mean. """ deterministic = merge_param( 'deterministic', self.deterministic, deterministic) if (self.rate == 0.) or deterministic: return inputs # Prevent gradient NaNs in 1.0 edge-case. if self.rate == 1.0: return jnp.zeros_like(inputs) keep_prob = 1. - self.rate rng = self.make_rng(self.rng_collection) broadcast_shape = list(inputs.shape) for dim in self.broadcast_dims: broadcast_shape[dim] = 1 mask = random.bernoulli(rng, p=keep_prob, shape=broadcast_shape) mask = jnp.broadcast_to(mask, inputs.shape) return lax.select(mask, inputs / keep_prob, jnp.zeros_like(inputs))  Particularly, I'm interested in last line lax.select(mask, inputs / keep_prob, jnp.zeros_like(inputs)). Wondering why lax.select is used here instead of: return jnp.where(mask, inputs / keep_prob, 0)  or even more simply: return mask * inputs / keep_prob 

Answer: jnp.where is basically the same as lax.select, except more flexible in its inputs: for example, it will broadcast inputs to the same shape or cast to the same dtype, whereas lax.select requires more strict matching of inputs: >>> import jax.numpy as jnp >>> from jax import lax >>> x = jnp.arange(3)  # Implicit broadcasting >>> jnp.where(x < 2, x[:, None], 0) DeviceArray([[0, 0, 0], [1, 1, 0], [2, 2, 0]], dtype=int32) >>> lax.select(x < 2, x[:, None], 0) TypeError: select cases must have the same shapes, got [(), (3, 1)].  # Implicit type promotion >>> jnp.where(x < 2, jnp.zeros(3), jnp.arange(3)) DeviceArray([0., 0., 2.], dtype=float32) >>> lax.select(x < 2, jnp.zeros(3), jnp.arange(3)) TypeError: lax.select requires arguments to have the same dtypes, got int32, float32. (Tip: jnp.where is a similar function that does automatic type promotion on inputs).  Library code is one place where the stricter semantics can be useful, because rather than smoothing-over potential implementation bugs and returning an unexpected output, it will complain loudly. But performance-wise (especially once JIT-compiled) the two are essentially equivalent. As for why the flax developers chose lax.select vs. multiplying by a mask, I can think of two reasons:  Multiplying by a mask is subject to implicit type promotion semantics, and it takes a lot more thought to anticipate problematic outputs than a simple select, which is specifically-designed for the intended operation. Using multiplication causes the compiler to treat this operation as a multiplication, which it is not. A select is a much more narrow and precise operation than a multiplication, and by specifying operations precisely it often allows the compiler to optimize the results to a greater extent.
Question: I created the following for loop for ItemTemplate in ItemTemplates: x = 0 self.needle_images.append(cv.imread(ItemTemplate, cv.IMREAD_UNCHANGED)) self.needle_widths.append(self.needle_images[x].shape[1]) self.needle_heights.append(self.needle_images[x].shape[0]) x = x+1  I originally tried to write the for loop like this: for ItemTemplate in ItemTemplates: self.needle_images.append(cv.imread(ItemTemplate, cv.IMREAD_UNCHANGED)) self.needle_widths.append(self.needle_images.shape[1]) self.needle_heights.append(self.needle_images.shape[0])  I was assuming I didn't need to add the list entries to this code and perhaps there is a better way to do this but my Python skills are very young. The top example is fine and my code runs ok with it, but I am looking to see if there was a better way to accomplish this task.

Answer: The last two lines of the loop are always using the last image appended to needle_images. The index of the last item in a list is -1. for ItemTemplate in ItemTemplates: self.needle_images.append(cv.imread(ItemTemplate, cv.IMREAD_UNCHANGED)) self.needle_widths.append(self.needle_images[-1].shape[1]) self.needle_heights.append(self.needle_images[-1].shape[0])  Or just bite the bullet and assign the image to a name at the top of the loop. for ItemTemplate in ItemTemplates: temp = cv.imread(ItemTemplate, cv.IMREAD_UNCHANGED) self.needle_images.append(temp) self.needle_widths.append(temp.shape[1]) self.needle_heights.append(temp.shape[0])  Or even... for ItemTemplate in ItemTemplates: temp = cv.imread(ItemTemplate, cv.IMREAD_UNCHANGED) w,h = temp.shape self.needle_images.append(temp) self.needle_widths.append(w) self.needle_heights.append(h)
Question: The current version of tflite-runtime is 2.11.0: https://pypi.org/project/tflite-runtime/ Here is a testing for downloading the tflite-runtime to the tmp folder: mkdir -p /tmp/test cd /tmp/test echo "tflite-runtime == 2.11.0" > ./test.txt pip3 download -r ./test.txt  Here is the error: ERROR: Could not find a version that satisfies the requirement tflite-runtime==2.11.0 (from versions: none) ERROR: No matching distribution found for tflite-runtime==2.11.0  Here is the pip3 version: # pip3 --version pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)  What's wrong in the above pip3 download? Why can't it find the latest version? And how to fix?

Answer: tflite-runtime 2.11.0 released packages: https://pypi.org/project/tflite-runtime/2.11.0/#files Python 3.7, 3.8 and 3.9. Only Linux, different Intel and ARM 64-bit architectures. No Python 3.10 and no source code. Use Python 3.9 if you don't want to compile from sources.
Question: I am trying to create a venv virtual enviroment for Python in Window's command prompt. I created the enviroment; however, I am having difficulties using it because when I run the "activate" command it is not working. I think the problem is related to that the virtual enviroment does not have a scripts file like other window machines do, but rahter a bin file which has the activate script. When I run the activate command with the bin in the file directory I still get an error. enter image description here I have been trying to solve the problem for the past 4-5 hours and am completely stuck. I tried destroying and reconstructing the virtual enviroment, I tried using different extensions (.bat, .exe, .ps1, and just \activate), and tried using powershell. Please let me know if you have any ideas what I am doing wrong!

Answer: Open a command prompt terminal by either searching command prompt in the Windows search bar, or press the Windows Key + R and enter cmd. Create the virtual environment in a desired directory using the following command: python -m venv env  This will create a new folder called env inside the directory where you executed the command. You can activate the created virtual environment by running the following command in the same directory where you executed the last command: cd env/Scripts && activate && cd ../../  I hope this helps.
Question: import torch from torch.autograd import Variable x = Variable(torch.FloatTensor([11.2]), requires_grad=True) y = 2 * x print(x) print(y) print(x.data) print(y.data) print(x.grad_fn) print(y.grad_fn) y.backward() # Calculates the gradients print(x.grad) print(y.grad)   Error: C:\Users\donhu\AppData\Local\Temp\ipykernel_9572\106071707.py:2: UserWarning: The .grad attribute of a Tensor that is not a leaf Tensor is being accessed. Its .grad attribute won't be populated during autograd.backward(). If you indeed want the .grad field to be populated for a non-leaf Tensor, use .retain_grad() on the non-leaf Tensor. If you access the non-leaf Tensor by mistake, make sure you access the leaf Tensor instead. See github.com/pytorch/pytorch/pull/30531 for more informations. (Triggered internally at aten\src\ATen/core/TensorBody.h:485.) print(y.grad)  Source code https://github.com/donhuvy/Deep-learning-with-PyTorch-video/blob/master/1.5.variables.ipynb How to fix?

Answer: Call y.retain_grad() before calling y.backward(). The reason is because by default PyTorch only populate .grad for leaf variables (variables that aren't results of operations), which is x in your example. To ensure .grad is also populated for non-leaf variables like y, you need to call their .retain_grad() method. Also worth noting that it's a warning rather than an error.
Question: PEP 593 added extended annotations via Annotated. But neither the PEP nor the documentation for Annotated describes how we're supposed to access the underlying annotations. How are we supposed to read the extra annotations stored in the Annotated object? from typing import Annotated class Foo: bar: Annotated[int, "save"] = 5 hints = get_type_hints(Foo, include_extras=True) # {'bar': typing.Annotated[int, 'save']} # Get list of data in Annotated object ??? hints["bar"].get_list_of_annotations() 

Answer: To avoid accessing dunder names, which are reserved by python (such access should be avoided whenever possible):  [...] Any use of __*__ names, in any context, that does not follow explicitly documented use, is subject to breakage without warning.  You should use typing.get_args helper. It is actually smarter than getting __args__ attribute because of additional expansion step, see source code for details. It is a public API, so this should be preferred to manual dunder attribute examination. from typing import Annotated, get_type_hints, get_args class Foo: bar: Annotated[int, "save"] = 5 hints = get_type_hints(Foo, include_extras=True) annotations = get_args(hints['bar']) print(annotations) # (<class 'int'>, 'save')
Question: I have a 2D NumPy array V: import numpy as np np.random.seed(10) V = np.random.randint(-10, 10, size=(6,8))  This gives V as: [[ -1 -6 5 -10 7 6 7 -2] [ -1 -10 0 -2 -6 9 6 -6] [ 5 1 1 -9 -2 -6 4 7] [ 9 3 -5 3 9 3 2 -9] [ -6 8 3 1 0 -1 5 8] [ 6 -3 1 7 4 -3 1 -9]]  Now, I have 2 lists, r1 and r2, containing column indices as follows: r1 = [1, 2, 5] r2 = [3, 4, 7]  What I want is to add the columns of V based on the indices pair (r1, r2) and store it in column indices r1. That is, for this case, add columns (1, 3), (2, 4) and (5, 7) and store them respectively in columns 1, 2 and 5 of V. It can be done easily like this: V[:, 1] = V[:, [1,3]].sum(axis=1) V[:, 2] = V[:, [2,4]].sum(axis=1) V[:, 5] = V[:, [5,7]].sum(axis=1)  which gives V as: [[ -1 -16 12 -10 7 4 7 -2] [ -1 -12 -6 -2 -6 3 6 -6] [ 5 -8 -1 -9 -2 1 4 7] [ 9 6 4 3 9 -6 2 -9] [ -6 9 3 1 0 7 5 8] [ 6 4 5 7 4 -12 1 -9]]  My concern is that is there a way we can do it without loops? Thanks in advance :)

Answer: Just add V[:, r2] at V[:, r2], like below: V[:, r1] += V[:, r2] print(V)  Output [[ -1 -16 12 -10 7 4 7 -2] [ -1 -12 -6 -2 -6 3 6 -6] [ 5 -8 -1 -9 -2 1 4 7] [ 9 6 4 3 9 -6 2 -9] [ -6 9 3 1 0 7 5 8] [ 6 4 5 7 4 -12 1 -9]]
Question: I know there are other ways to check if dict is empty or not using match/case (for example, dict(data) if len(data) == 0), but I can't understand why python give different answers for list and dict types while we check for emptiness data = [1, 2] match data: case []: print("empty") case [1, 2]: print("1, 2") case _: print("other") # 1, 2  data = {1: 1, 2: 2} match data: case {}: print("empty") case {1: 1, 2: 2}: print("1, 2") case _: print("other") # empty 

Answer: There are different patterns used on the match case Python implementation. When a list or tuple is used, the pattern used is the one called Sequence Patterns. Here's an example found on the PEP: match collection: case 1, [x, *others]: print("Got 1 and a nested sequence") case (1, x): print(f"Got 1 and {x}")  So let's say that we set collection as [1, 2] The output will be: Got 1 and 2  If we set is as [1, [2, 3]] the output will be: Got 1 and a nested sequence  This happens because the Sequence Pattern, tries to match a given sequence on the list. So in the case you passed, it is going to try to match exactly [1, 2]. On the other hand, when a dict is used, the Mapping Patterns is the rule. If you execute this code, you will understand better. config1 = {"router": "CNC", "network": "lan"} config2 = {"router": "MAC"} config3 = {"router": None} config4 = {"network": "lan"} configs = [config1, config2, config3, config4] for config in configs: match config: case {"router": "MAC"}: print("Configuring MAC router") case {"router": "CNC"}: print("Configuring CNC router") case {"router": None} | {}: print("No router available, please check config again")  The output is going to be this: Configuring CNC router Configuring MAC router No router avaiable, please cheg config again No router avaiable, please cheg config again  So, as you can see, the code actually does not compare using == or is is actually only look on the configs, by the keys defined on the case. So if all the keys and values for the case dict can be found on the config dict, they are going to enter on that case. This happens because the idea is that you do not wanna compare the dict object itself, but check if some keys are matching with what you expecte to execute a certain action. Basically all the keys that are present on the config dict but are not on the cases dicts is going to be ignored (not relevant for the match case) When you put a case with an empty dict, you have no key, values, so any dict passed on the match will be accepted. More information about this subject can be found on the official PEP: https://peps.python.org/pep-0622/#sequence-patterns