eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am trying to train a random forest model with sklearn. I have some original data (x, y) that I use to train the RF initially with. from sklearn.ensemble import RandomForestClassifier import numpy as np x = np.random.rand(30,20) y = np.round(np.random.rand(30)) rf = RandomForestClassifier() rf.fit(x,y)  Now I get some new data that I want to use to retrain the model, but I want to keep the already existing trees in the rf untouched. So I set warm_start=True and add additional trees. x_new = np.random.rand(5,20) y_new = np.round(np.random.rand(5)) rf.n_estimators +=100 rf.warm_start = True rf.fit(x_new,y_new)  So far so good. Everything works. But when I make predictions I get an error: rf.predict(x) >>> ValueError: non-broadcastable output operand with shape (30,1) doesn't match the broadcast shape (30,2)  Why does this happen?

Answer: This runs fine for me in colab, with the same sklearn version 1.2.2 mentioned. I suspect the issue is similar to what was indicated by the now-deleted answer, as well as Scikit-learn Randomforest with warm_start results (non-broadcastable output ...): one of your datasets (in this case, the y_new) doesn't have the same classes. I tested this by setting y_new = np.ones(5) instead of random, and I get the same error; so I think you were just unlucky and got all ones or all zeros with whatever random seed numpy had when you ran this the first time.
Question: I am developing a dash web application which is a multi page app. Below is my structure - project/ - pages/ - home.py - contact.py - graph.py - app.py - index.py  In app.py, I have a component dcc.Location(id="url", refresh=True), to load different pages to a html.Div(id="page-content") using a callback. The pages are getting loaded successfully as user click on sidebar having navlinks for each page. Say I am on home page (localhost/home) and I now click to go to contact page (localhost/contact), It works. Here, before leaving the home page, I am want to save the data in dcc.store component. One way is to have hidden button and activate this button callback through javascript. For that, In assets folder I have created a custom-script.js document.addEventListener('visibilitychange', e=>{ if (document.visibilityState === 'visible') { console.log("user is focused on the page") } else { // code to trigger button click event to activate callback console.log("user left the page") } });  But what I am noticing is that even though I am switching from home page to contact page the event is not firing. May be an update of the page content by callback does not seems to affect refresh( even setting as True). But If, I do hard refresh via browser refresh button on home page the event is fired Is there a any other way to detect page change using dash or javascript?

Answer: You will probably need a dash callback and the custom script :  If navigating to a page within your app via one of the Location links doesn't trigger the visibilitychange event, you can still update the store using a dash callback (defined in app.py, as well as the Location and Store components), eg. @app.callback( Output("store-id", "data"), Input('url', 'pathname'), State("store-id", "data"), prevent_initial_call=True, ) def update_store(pathname, data): data['someproperty'] = 'some value' return data   And for the cases when user browser-refreshes the page or goes to another website, the visibilitychange event should fire properly. However, triggering a button click from the event handler is not the best choice I think because the script will probably terminate before the button-click callback resolves (user leaves the page), instead you could directly load and update the storage in the visibilitychange handler, eg. document.addEventListener('visibilitychange', e => { if (document.visibilityState === 'visible') { console.log("user is focused on the page"); } else { // Update store data const data = JSON.parse(localStorage.getItem('store-id')); data.someproperty = 'some value'; localStorage.setItem('store-id', JSON.stringify(data )); console.log("user left the page"); } });
Question: I'm trying to create a lambda layer that includes pydantic/pydantic_core (Lambda python 3.11, x86_64) but getting the following error: "Unable to import module 'reddit_lambda': No module named 'pydantic_core._pydantic_core'" For context, I'm installing this on a windows x64 machine. From reading up about it here, here and here, it seems that pip installing pydantic for an incompatible architecture (win_amd64). Install log: Using cached pydantic-2.4.2-py3-none-any.whl (395 kB) Using cached pydantic_core-2.10.1-cp311-none-win_amd64.whl (2.0 MB)  My question: Is there a way to force pip to install packages into a directory for a specific architecture in powershell? I saw an old discussion here, that this is possible for Linux but not sure how to do it on powershell. FYI: I also tried adding pydantic_core from binary (tried  pydantic_core-2.10.1-pp39-pypy39_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl and  pydantic_core-2.10.1-pp38-pypy38_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl) but I'm a bit confused about which version to choose?

Answer: It is possible as part of the options provided by the pip CLI. For example given a requirements.txt file containing the necessary dependencies, you can run: # Create layer based on requirements.txt in python/ directory pip install -r requirements.txt --platform manylinux2014_x86_64 --target ./python --only-binary=:all:  This command will create a directory named python that will contain the packages downloaded to satisfy the requirements in the requirements.txt file for the manylinux2014_x86_64 platform. You can change the value provided to the --platform flag according to your needs, you can see examples of available platforms in the examples here
Question: My code currently looks like this: df_1 = portfolio_all[0].rename(columns={'Close': 'Close_1'} ) df_2 = portfolio_all[1].rename(columns={'Close': 'Close_2'} ) df_3 = portfolio_all[2].rename(columns={'Close': 'Close_3'} ) df_4 = portfolio_all[3].rename(columns={'Close': 'Close_4'} ) df_5 = portfolio_all[4].rename(columns={'Close': 'Close_5'} ) df_1['daily_return_1'] = df_1['Close_1'].pct_change(1) df_2['daily_return_2'] = df_2['Close_2'].pct_change(1) df_3['daily_return_3'] = df_3['Close_3'].pct_change(1) df_4['daily_return_4'] = df_4['Close_4'].pct_change(1) df_5['daily_return_5'] = df_5['Close_5'].pct_change(1) df_1['perc_ret_1'] = (1 + df_1.daily_return_1).cumprod() - 1 df_2['perc_ret_2'] = (1 + df_2.daily_return_2).cumprod() - 1 df_3['perc_ret_3'] = (1 + df_3.daily_return_3).cumprod() - 1 df_4['perc_ret_4'] = (1 + df_4.daily_return_4).cumprod() - 1 df_5['perc_ret_5'] = (1 + df_5.daily_return_5).cumprod() - 1  Is there a way to dynamically create these dataframes in a for loop or something like that without having to write each dataframe as a line of code?

Answer: You can create using a simple python for-loop and enumerate throught he list import pandas as pd portfolio_all = [df1, df2, df3, df4, df5] for i, df in enumerate(portfolio_all): column_name = f'Close_{i+1}' df.rename(columns={'Close': column_name}, inplace=True) df[f'daily_return_{i+1}'] = df[column_name].pct_change(1) df[f'perc_ret_{i+1}'] = (1 + df[f'daily_return_{i+1}']).cumprod() - 1  Assuming you have all the dataframes as a list. This way, you can avoid repeating the same code for each DataFrame, and it should work for any number of DataFrames in your portfolio_all list. Hope this helps!
Question: In the function1 function of the main.py file, i would like to import textbox1 and textbox2 from the Page1(tk.Frame) class of the page1.py file. I get the error NameError: name 'textbox1' is not defined, because textbox1 and textbox2 are not imported correctly. This is the code I'm using. What am I doing wrong? How to solve? main.py import tkinter as tk from tkinter import ttk from tkinter import * from page1 import Page1 root = tk.Tk() root.geometry('480x320') style = ttk.Style() style.theme_use('default') style.configure('TNotebook', tabposition='wn', background='white', tabmargins=0) style.configure('TNotebook.Tab', background='white', width=10, focuscolor='yellow', borderwidth=0) style.map('TNotebook.Tab', background=[('selected', 'yellow')]) nb = ttk.Notebook(root) nb.place(x=0, y=70) page1 = Page1(nb, width=492, height=905) nb.add(page1, text='Tab 1', compound='left') def function1(textbox1, textbox2): val_1 = "example 1" textbox1.insert(0, val_1) val_2 = "example 1" textbox2.insert(0, val_2) button1 = Button(root, text="Button", command= function1(textbox1, textbox2)) button1.place(x=0, y=0) root.mainloop()  page1.py import tkinter as tk from tkinter import ttk from tkinter import * from tkinter import ttk import tkinter as tk import tkinter.font as tkFont from tkinter import ttk class Page1(tk.Frame): def __init__(self, master, **kw): super().__init__(master, **kw) self.textbox1 = ttk.Entry(self, width=7) self.textbox1.place(x=10, y=10) self.textbox2 = ttk.Entry(self, width=7) self.textbox2.place(x=10, y=40) 

Answer: As per my knowledge, to access textbox1 and textbox2 in the function1 function in main.py, you should make them instance variables by using self.textbox1 and self.textbox2. import tkinter as tk from tkinter import ttk class Page1(tk.Frame): def __init__(self, master, **kw): super().__init__(master, **kw) self.textbox1 = ttk.Entry(self, width=7) self.textbox1.place(x=10, y=10) self.textbox2 = ttk.Entry(self, width=7) self.textbox2.place(x=10, y=40)  And main.py should be something like this import tkinter as tk from tkinter import ttk from tkinter import * from page1 import Page1 def function1(): val_1 = "example 1" page1.textbox1.delete(0, tk.END) # Clear any existing text page1.textbox1.insert(0, val_1) val_2 = "example 2" page1.textbox2.delete(0, tk.END) # Clear any existing text page1.textbox2.insert(0, val_2) button1 = Button(root, text="Button", command=function1) button1.place(x=0, y=0) root.mainloop()  This should possibly solve the issue.Can you try and hope this helps. Note : If you just want to edit you script. you can add this line for button, where page1 object is used button1 = Button(root, text="Button", command= function1(page1.textbox1, page1.textbox2))  I have tested and its working good for me :
Question: I'm trying to implement the mongomery-ladder method for modular exponentiation for RSA (so N=p•q, p,q are primes) in python, as followed in this paper:  My code looks like this:  x stands for base, k for exp, and N for modulus  # uses montgomery-ladder method for modular exponentiation def montgomery_ladder(x, k, N): x %= N k %= N # getting the binary representation of k bin_k = list(map(int, bin(k)[2:])) # Initialize two variables to hold the intermediate results r0 = 1 r1 = x # Loop through each bit of the binary exponent from most significant to the least significant for i in range(len(bin_k)): if bin_k[i] == 0: r1 = (r1 * r0) % N r0 = (r0 ** 2) % N else: r0 = (r0 * r1) % N r1 = (r1 ** 2) % N # The final result is in r0 return r0  it doesn't work well for very large numbers, for example the following test: def main(): x = 412432421343242134234233 k = 62243535235312321213254235 N = 10423451524353243462 print(montgomery_ladder(x, k, N)) print(pow(x, k, N)) if __name__ == '__main__': main()  yields: 7564492758006795519 179467895766154563  pow(x, k, n) returns the correct answer, but my function doesn't. Have any ideas?

Answer: Remove the k %= N at the start.
Question: I am trying to create subclasses of some superclass that has properties (delineated by the property decorator) and a properties() method that returns the properties and their values as a dict. I want the subclasses to be able to make use of the inherited properties() method with minimal boilerplate code needing to be copied and pasted into each subclass definition. I'm using Python 3.8 if it makes a difference here. The emphasis here is on the boilerplate code that I want to refactor. I begin with a simple class, BaseClass, defined as follows: class BaseClass(object): def __init__(self, A, B): self._A = A self._B = B @property def A(self): return self._A @A.setter def A(self, new_value): # ... Validate input ... self._A = new_value @property def B(self): return self._B @B.setter def B(self, new_value): # ... Validate input ... self._B = new_value def properties(self): class_items = self.__class__.__dict__.items() return dict((k, getattr(self, k)) for k, v in class_items if isinstance(v, property))  I should note that I defined the properties() method at the end after reading this other stackoverflow answer. Example usage of BaseClass as defined above: >>> base_instance = BaseClass('foo','bar') >>> base_instance.properties() {'A': 'foo', 'B': 'bar'}  From there, I want to create some subclasses that inherit from BaseClass and have the properties() method work the same way. Consider the following: class SubClass(BaseClass): def __init__(self, A, B): super().__init__(A, B)  But this doesn't behave the way I expected: >>> sub_instance = SubClass('spam',10) >>> sub_instance.A # works as expected 'spam' >>> sub_instance.B # works as expected 10 >>> sub_instance.properties() # expected {'A': 'spam', 'B': 10} {}  I know that the following alternative subclass definition produces the behavior I expected: class SubClass(BaseClass): def __init__(self, A, B): super().__init__(A, B) @BaseClass.A.getter # def A(self): # This is all boilerplate return self._A # that I need to copy and # paste in every subclass @BaseClass.B.getter # of BaseClass I define... def B(self): # return self._B #  >>> sub_instance = SubClass('spam',10) >>> sub_instance.properties() {'A': 'spam', 'B': 10}  Is there a cleaner way to define properties (with the property decorator) inside the superclass BaseClass so that the properties() method "just works", without the need for the boilerplate lines?

Answer: You can try to use inspect.getmro to get all base classes: from inspect import getmro class BaseClass(object): def __init__(self, A, B): self._A = A self._B = B @property def A(self): return self._A @A.setter def A(self, new_value): self._A = new_value @property def B(self): return self._B @B.setter def B(self, new_value): self._B = new_value def properties(self): out = {} for klass in getmro(self.__class__): class_items = klass.__dict__.items() for k, v in class_items: if isinstance(v, property): out[k] = getattr(self, k) return out class SubClass(BaseClass): def __init__(self, A, B): super().__init__(A, B) @property def C(self): return 42 sub_instance = SubClass("spam", 10) print(sub_instance.properties())  Prints: {'C': 42, 'A': 'spam', 'B': 10}
Question: Having model class MyTable(Model): close = ManyToManyField("MyTable")  How to bulk create objects to this relation? With tables not related to itself, one could use db_payload =[MyTable.close.throught(tablea_id=x, tableb_id=y) for x,y in some_obj_list] MyTable.close.through.objects.bulk_create(db_payload)  What would the keyword arguments be in case where relation is to the table itself?

Answer: In that case it uses as fields from_model and to_model. Indeed, we can see this in the source code [GitHub]:  to = make_model_tuple(to_model)[1] from_ = klass._meta.model_name if to == from_: to = "to_%s" % to from_ = "from_%s" % from_   So you can work with: MyTable.close.through.objects.bulk_create( [ MyTable.close.through(from_mytable_id=x, to_mytable_id=y) for x, y in some_obj_list ] )
Question: I have the input in this format: import polars as pl data = {"Name": ['Name_A', 'Name_B','Name_C'], "val_1": ['a',None, 'a'],"val_2": [None,None, 'b'],"val_3": [None,'c', None],"val_4": ['c',None, 'g'],"val_5": [None,None, 'i']} df = pl.DataFrame(data) print(df) shape: (3, 6) ┌────────┬───────┬───────┬───────┬───────┬───────┐ │ Name ┆ val_1 ┆ val_2 ┆ val_3 ┆ val_4 ┆ val_5 │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ str ┆ str ┆ str ┆ str ┆ str │ ╞════════╪═══════╪═══════╪═══════╪═══════╪═══════╡ │ Name_A ┆ a ┆ null ┆ null ┆ c ┆ null │ │ Name_B ┆ null ┆ null ┆ c ┆ null ┆ null │ │ Name_C ┆ a ┆ b ┆ null ┆ g ┆ i │ └────────┴───────┴───────┴───────┴───────┴───────┘  I want the output as: shape: (3, 7) ┌────────┬───────┬───────┬───────┬───────┬───────┬───────────────────┐ │ Name ┆ val_1 ┆ val_2 ┆ val_3 ┆ val_4 ┆ val_5 ┆ combined │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ str ┆ str ┆ str ┆ str ┆ str ┆ str ┆ list[str] │ ╞════════╪═══════╪═══════╪═══════╪═══════╪═══════╪═══════════════════╡ │ Name_A ┆ a ┆ null ┆ null ┆ c ┆ null ┆ ["a", "c"] │ │ Name_B ┆ null ┆ null ┆ c ┆ null ┆ null ┆ ["c"] │ │ Name_C ┆ a ┆ b ┆ null ┆ g ┆ i ┆ ["a", "b","g""i"] │ └────────┴───────┴───────┴───────┴───────┴───────┴───────────────────┘  I want to combine all the columns as a list except the Name column. I have simplified the data for this question but in reality we have many columns of the val_N format and a generic code where I do not have to list each column name would be great.

Answer: For the main answer in the question you can do df.with_columns(combined = pl.concat_list(pl.exclude('Name')))  pl.exclude is how to get all columns BUT the ones given. To get rid of the nulls in the final list, version 0.19.4 just introduced list.drop_nulls. df.with_columns(combined = pl.concat_list(pl.exclude('Name')).list.drop_nulls())
Question: I have a data frame with a column that potentially can be filled with 3 options (a,b, and/or c) with a comma delimiter. import pandas as pd df = pd.DataFrame({'col1':['a,b,c', 'b', 'a,c', 'b,c', 'a,b']})  I want to split this column based on ',' df['col1'].str.split(',', expand=True)  A problem with this is that new columns are filled from the first column where I want to fill the columns based on values. For example all a's in the first column, b's in the second column, c's in the third column.

Answer: Using str.get_dummies: tmp = df['col1'].str.get_dummies(',') out = tmp.mul(tmp.columns)  Output:  a b c 0 a b c 1 b 2 a c 3 b c 4 a b  With NaNs and custom headers: tmp = df['col1'].str.get_dummies(',') out = (tmp.mul(tmp.columns).where(tmp>0) .rename(columns={'a': 'X', 'b': 'Y', 'c': 'Z'}) )  Output:  X Y Z 0 a b c 1 NaN b NaN 2 a NaN c 3 NaN b c 4 a b NaN
Question: I was expecting for example the F bar to have 8+9=17 and not only 9 (the last value for F). import matplotlib.pyplot as plt x = ['A', 'B', 'C', 'D', 'D', 'D', 'D', 'E', 'F', 'F'] y = [ 5 , 8 , 7, 9, 9, 2, 7, 8, 8, 9 ] fig, ax = plt.subplots() ax.bar(x, y) plt.show();  Can someone explain the logic please ? 

Answer: No, it's normal, the bars are superimposed. See for example changing the opacity: ax.bar(x, y, alpha=0.1)   You can use pandas to group the values: pd.Series(y).groupby(x).sum().plot.bar()  Output:  Or in pure python: out = {} for X, Y in zip(x, y): out[X] = out.get(X, 0) + Y fig, ax = plt.subplots() ax.bar(*zip(*out.items()))
Question: Short summary: The app created by python kivy which was converted to Xcode project using kivy-ios/toolchain runs on the simulator and in some cases can be built to my iPhone. But I don’t understand what I am doing different on the occasions where it does not run on my iPhone -Mac M1 arm64 -Ventura 13.5.2 -Xcode15 -iOS17  Here are two of the errors I get when I fail to build. The second one is always the same the first one seems to change the file name on occasion. Error 1: Building for 'iOS', but linking in dylib (/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics.tbd) built for 'iOS-simulator'  *Sometimes Error 1 will be for AudioToolbox.framework/AudioToolbox.tbd file. Error2: Linker command failed with exit code 1 (use -v to see invocation)  I have created a text file that lists the settings of each Xcode project there are a few differences. Below is a screen shot of a comparison of these files. The one on left builds to iPhone oddly enough. I do not where to find the ARCHS = arm64 setting in Xcode.  To make this file I used: xcodebuild -project openmindset.xcodeproj -target openmindset -configuration openmindset -showBuildSettings > openmindset_settings_02.txt  If there is something else better, please share. Here is an abbreviated version of my log with my most recent attempts to try to make sense of what is going on.  Full version: https://1drv.ms/x/s!AmCs1-5fbd9gncEVVGmWSY8B3QyGSA?e=n4fla2 I have seen posts on this topic that have suggested I need to remove arm64 from Excluded Architectures, since I am using an arm64, but that doesn’t seem to matter either. Or maybe I’ve not set the right argument for that parameter?  Honestly, I don’t think I’m in searching in the right ball park. This is mainly to show avenues I’ve searched in. So if there are any ideas even in a different ball park, I’d be greatful. Thanks in advance.

Answer: Ok, this is a little hacky but it works… Change Xcode filename. Background All toolchains commands need Xcode to be the same name as targeted when toolchain is built. Or at least there are locations that toolchain expects Xcode to be based on the setup of the toolchain environment. But then after the Xcode project is built, if I change the file name from “Xcode” to say “Xcode15”. At that point the project builds to my iPhone. I made no adjustments to the Xcode project other than sign in to my Team. My hope is that someone who knows more about this will run across this and be able to diagnose my problem and suggest a better solution. But for the time being this is for me.
Question: Why is this printing 'yes'? I don't understand why. I understand that x or y returns true, but x and z should return false, or is the order it compares different? x=True y=False z=False if(x or y and z): print ("yes") else: print("no") 

Answer: Because and has a higher precedence than or, so it is equivalent to: True or (False and False)  Which is: True or False  which is: True
Question: I have the pylint config: [MAIN] load-plugins=pylint_django django-settings-module=kernel.settings ignore-paths=^kernel/**$ , ^migrations/*$  But whenever i want to run i get this error for kernel regex: re.error: multiple repeat at position 10  How to make it so it ignores all subdirs and files inside kernel?

Answer: It's an re.error. Which means your regular expression syntax is incorrect. so to ignore ALL subdirs and files inside the kernel. simply assign kernel/.* regex value to ignore-paths so it will look like this.   [MAIN] load-plugins=pylint_django django-settings-module=kernel.settings ignore-paths=^kernel/.*    hope this solves the problem.