eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: I am trying to scrape comments from the Yahoo Finance conversation page (e.g. TSLA) using Python Selenium. I would like to extract all comments together with their replies. As Yahoo Finance does not automatically show all the replies under each comment and have no unique identifier for individual comment, there are also problems of deleted comments, what would be the most efficient way to do it?

Answer: If you'll inspect the network tab, you'll notice the API that the client communicates with to fetch the comments and related data. It required some data like spotId and uuid. I guess this is to identify the article. With this information, you can simply use BeautifulSoup and requests to make the process much more efficient and faster than using Selenium. Some example code: url = 'https://finance.yahoo.com/quote/TSLA/community?p=TSLA' response = requests.get(url, headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0'}) soup = BeautifulSoup(response.text) data = json.loads(soup.select_one('#spotim-config').get_text(strip=True))['config'] url = "https://api-2-0.spot.im/v1.0.0/conversation/read" payload = json.dumps({ "conversation_id": data['spotId'] + data['uuid'].replace('_', '$'), "count": 250, "offset": 0 }) headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:109.0) Gecko/20100101 Firefox/110.0', 'Content-Type': 'application/json', 'x-spot-id': data['spotId'], 'x-post-id': data['uuid'].replace('_', '$'), } response = requests.post(url, headers=headers, data=payload) data = response.json()  Since the API is paginated, play around with the values of count and offset to get subsequent pages.
Question: I am currently trying to replicate ngroup behaviour in polars to get consecutive group indexes (the dataframe will be grouped over two columns). For the R crowd, this would be achieved in the dplyr world with dplyr::group_indices or the newer dplyr::cur_group_id. As shown in the repro, I've tried couple avenues without much succcess, both approaches miss group sequentiality and merely return row counts by group. Quick repro: import polars as pl import pandas as pd df = pd.DataFrame( { "id": ["a", "a", "a", "a", "b", "b", "b", "b"], "cat": [1, 1, 2, 2, 1, 1, 2, 2], } ) df_pl = pl.from_pandas(df) print(df.groupby(["id", "cat"]).ngroup()) # This is the desired behaviour # 0 0 # 1 0 # 2 1 # 3 1 # 4 2 # 5 2 # 6 3 # 7 3 print(df_pl.select(pl.len().over("id", "cat"))) # This is only counting observation by group # ┌─────┐ # │ len │ # │ --- │ # │ u32 │ # ╞═════╡ # │ 2 │ # │ 2 │ # │ 2 │ # │ 2 │ # │ 2 │ # │ 2 │ # │ 2 │ # │ 2 │ # └─────┘ print(df_pl.group_by("id", "cat").agg(pl.len().alias("test"))) # shape: (4, 3) # ┌─────┬─────┬──────┐ # │ id ┆ cat ┆ test │ # │ --- ┆ --- ┆ --- │ # │ str ┆ i64 ┆ u32 │ # ╞═════╪═════╪══════╡ # │ a ┆ 1 ┆ 2 │ # │ a ┆ 2 ┆ 2 │ # │ b ┆ 1 ┆ 2 │ # │ b ┆ 2 ┆ 2 │ # └─────┴─────┴──────┘ 

Answer: We can use rank for this: (df.with_row_index() .with_columns( pl.first("index").over("id", "cat").rank("dense") - 1 ) )  shape: (8, 3) ┌───────┬─────┬─────┐ │ index ┆ id ┆ cat │ │ --- ┆ --- ┆ --- │ │ u32 ┆ str ┆ i64 │ ╞═══════╪═════╪═════╡ │ 0 ┆ a ┆ 1 │ │ 0 ┆ a ┆ 1 │ │ 1 ┆ a ┆ 2 │ │ 1 ┆ a ┆ 2 │ │ 2 ┆ b ┆ 1 │ │ 2 ┆ b ┆ 1 │ │ 3 ┆ b ┆ 2 │ │ 3 ┆ b ┆ 2 │ └───────┴─────┴─────┘
Question: I put these on the top of a module: # mypy: disable-error-code=misc # mypy: disable-error-code=attr-defined  but only the last line is honoured, the first one is ignored. The same with reversed order or with three lines. In each case all lines except the last one are ignored. I was also trying to aggregate it into one line but failed. What is the correct syntax to suppress multiple mypy errors in the whole module?

Answer: Use both quotes and commas: # mypy: disable-error-code="misc,attr-defined"  (Credit goes to STerliakov, who discovered this but did not post an answer.)
Question: I am using Python 3.9.16. When I try to build an application like so: (base) G:\>pyinstaller --onefile grp.py  I get an error that says: The 'pathlib' package is an obsolete backport of a standard library package and is incompatible with PyInstaller. Please remove this package (located in C:\Users\alpha\anaconda3\lib\site-packages) using conda remove then try again.  I tried uninstalling and reinstalling pyinstaller, as well as upgrading conda, but none of this helped. How can I fix the problem?

Answer: If conda remove pathlib can't find the package, go to the lib folder and delete a folder called path-list-.....
Question: The CSV file I have is 70 Gb in size. I want to load the DF and count the number of rows, in lazy mode. What's the best way to do so? As far as I can tell, there is no function like shape in lazy mode according to the documentation. I found this answer which provide a solution not based on Polars, but I wonder if it is possible to do this in Polars as well.

Answer: For polars 0.20.5+ To get the row count using polars. First load it into a lazyframe... lzdf=pl.scan_csv("mybigfile.csv")  Then count the rows and return the result lzdf.select(pl.len()).collect()  If you just want a python scalar rather than a table as a result then just subset it lzdf.select(pl.len()).collect().item()  For older versions To get the row count using polars. First load it into a lazyframe... lzdf=pl.scan_csv("mybigfile.csv")  Then count the rows and return the result lzdf.select(pl.count()).collect()  If you just want a python scalar rather than a table as a result then just subset it lzdf.select(pl.count()).collect().item()
Question: What is the best way to find the differences between 2 Polars dataframes? The equals method tells me if there is a difference, I want to find where is the difference. Example: import polars as pl df1 = pl.DataFrame([ {'id': 1,'col1': ['a',None],'col2': ['x']}, {'id': 2,'col1': ['b'],'col2': ['y', None]}, {'id': 3,'col1': [None],'col2': ['z']}] ) ┌─────┬─────────────┬─────────────┐ │ id ┆ col1 ┆ col2 │ │ --- ┆ --- ┆ --- │ │ i64 ┆ list[str] ┆ list[str] │ ╞═════╪═════════════╪═════════════╡ │ 1 ┆ ["a", null] ┆ ["x"] │ │ 2 ┆ ["b"] ┆ ["y", null] │ │ 3 ┆ [null] ┆ ["z"] │ └─────┴─────────────┴─────────────┘ df2 = pl.DataFrame([ {'id': 1,'col1': ['a'],'col2': ['x']}, {'id': 2,'col1': ['b', None],'col2': ['y', None]}, {'id': 3,'col1': [None],'col2': ['z']}] ) ┌─────┬─────────────┬─────────────┐ │ id ┆ col1 ┆ col2 │ │ --- ┆ --- ┆ --- │ │ i64 ┆ list[str] ┆ list[str] │ ╞═════╪═════════════╪═════════════╡ │ 1 ┆ ["a"] ┆ ["x"] │ │ 2 ┆ ["b", null] ┆ ["y", null] │ │ 3 ┆ [null] ┆ ["z"] │ └─────┴─────────────┴─────────────┘  The difference in the example is for id = 1 and id = 2. I can join the dataframes: df1.join(df2, on='id', suffix='_df2') ┌─────┬─────────────┬─────────────┬─────────────┬─────────────┐ │ id ┆ col1 ┆ col2 ┆ col1_df2 ┆ col2_df2 │ │ --- ┆ --- ┆ --- ┆ --- ┆ --- │ │ i64 ┆ list[str] ┆ list[str] ┆ list[str] ┆ list[str] │ ╞═════╪═════════════╪═════════════╪═════════════╪═════════════╡ │ 1 ┆ ["a", null] ┆ ["x"] ┆ ["a"] ┆ ["x"] │ │ 2 ┆ ["b"] ┆ ["y", null] ┆ ["b", null] ┆ ["y", null] │ │ 3 ┆ [null] ┆ ["z"] ┆ [null] ┆ ["z"] │ └─────┴─────────────┴─────────────┴─────────────┴─────────────┘  Expected result I would like to either:  add a boolean columns that shows True in the rows with a difference filter and only display rows with a difference.  The example has only 2 columns, but there are more columns in the dataframe.

Answer: Here's the filter approach ( df1.join(df2, on='id', suffix='_df2') .filter(pl.any_horizontal( pl.col(x).ne_missing(pl.col(f"{x}_df2")) for x in df1.columns if x!='id' )) )  If you wanted the bool column then you just change the filter to with_columns and add an alias. ( df1.join(df2, on='id', suffix='_df2') .with_columns( has_diff = pl.any_horizontal( pl.col(x).ne_missing(pl.col(f"{x}_df2")) for x in df1.columns if x!='id' ) ) )  This assumes that each df has all the same columns other than 'id'.
Question: I'm new to YOLOv8, I just want the model to detect only some classes, not all the 80 classes the model trained on. How can I specify YOLOv8 model to detect only one class? For example only person. from ultralytics import YOLO model = YOLO('YOLOv8m.pt')  I remember we can do this with YOLOv5, but I couldn't do same with YOLOv8: model = torch.hub.load("ultralytics/yolov5", 'custom', path='yolov5s.pt') model.classes = [0] # Only person model.conf = 0.6 

Answer: Just specify classes in predict with the class IDs you want to predict from ultralytics.yolo.engine.model import YOLO model = YOLO("yolov8n.pt") model.predict(source="0", show=True, stream=True, classes=0) # [0, 3, 5] for multiple classes for i, (result) in enumerate(results): print('Do something with class 0')
Question: I have a problem with following code: from pandasql import sqldf import pandas as pd df = pd.DataFrame({'column1': [1, 2, 3], 'column2': [4, 5, 6]}) query = "SELECT * FROM df WHERE column1 > 1" new_dataframe = sqldf(query) print(new_dataframe)  When I submit, I have this error: Traceback (most recent call last): File ~\AppData\Local\Programs\Spyder\Python\lib\site-packages\sqlalchemy\engine\base.py:1410 in execute meth = statement._execute_on_connection AttributeError: 'str' object has no attribute '_execute_on_connection' The above exception was the direct cause of the following exception: Traceback (most recent call last): File ~\AppData\Local\Programs\Spyder\pkgs\spyder_kernels\py3compat.py:356 in compat_exec exec(code, globals, locals) File c:\users\yv663dz\downloads\untitled1.py:18 new_dataframe = sqldf(query) File ~\AppData\Local\Programs\Spyder\Python\lib\site-packages\pandasql\sqldf.py:156 in sqldf return PandaSQL(db_uri)(query, env) File ~\AppData\Local\Programs\Spyder\Python\lib\site-packages\pandasql\sqldf.py:61 in __call__ result = read_sql(query, conn) File ~\AppData\Local\Programs\Spyder\Python\lib\site-packages\pandas\io\sql.py:592 in read_sql return pandas_sql.read_query( File ~\AppData\Local\Programs\Spyder\Python\lib\site-packages\pandas\io\sql.py:1557 in read_query result = self.execute(*args) File ~\AppData\Local\Programs\Spyder\Python\lib\site-packages\pandas\io\sql.py:1402 in execute return self.connectable.execution_options().execute(*args, **kwargs) File ~\AppData\Local\Programs\Spyder\Python\lib\site-packages\sqlalchemy\engine\base.py:1412 in execute raise exc.ObjectNotExecutableError(statement) from err ObjectNotExecutableError: Not an executable object: 'SELECT * FROM df WHERE column1 > 1'  I installed the latest versions of pandas, pandasql and sqlalchemy and I use Spyder as IDE. Could someone help me please?

Answer: SQLAlchemy 2.0 (released 2023-01-26) requires that raw SQL queries be wrapped by sqlalchemy.text. The general solution for this error message is to pass the query text to sqlalchemy.text() from sqlalchemy import text ... query = text("SELECT * FROM some_table WHERE column1 > 1")  However in this case the OP is using pandasql, which expects a string. There does not seem to be a straightforward way to make pandasql compatible with SQLAlchemy >= 2.0, and the package seems to be unmaintained, so the only solutions are to find a fork that has fixed the problem (there are some), fork the project yourself and fix it, or downgrade your SQLAlchemy installation using your Python package manager. For example, if you use pip: python3 -m pip install --upgrade 'sqlalchemy<2.0'
Question: I have a class model using Pydantics. I try to supply my own ID but it gives me two id fields in the MongoDB database. The one I gave it and the one it makes automatically. Here is the result of my post method:  here is my class in models/articleModel.py: class ArticleModel(BaseModel): _id: int title: str body: str tags: Optional[list] = None datetime: Optional[datetime] = None caption: Optional[str] = None link: Optional[str] = None class Config: orm_mode = True allow_population_by_field_name = True arbitrary_types_allowed = True  here is my code for the post method in routers/article_router: @router.post("/article/", status_code=status.HTTP_201_CREATED) def add_article(article: articleModel.ArticleModel): article.datetime = datetime.utcnow() try: result = Articles.insert_one(article.dict()) pipeline = [ {'$match': {'_id': result.inserted_id}} ] new_article = articleListEntity(Articles.aggregate(pipeline))[0] return new_article except DuplicateKeyError: raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=f"Article with title: '{article.id}' already exists") 

Answer: I had to change the articleModel to a dictionary and add a new key called _id. @router.post("/article/", status_code=status.HTTP_201_CREATED) def add_article(article: articleModel.ArticleModel): article.datetime = datetime.utcnow() article_new_id = article.dict() article_new_id['_id'] = article_new_id['id'] del article_new_id['id'] try: result = Articles.insert_one(article_new_id) pipeline = [ {'$match': {'_id': result.inserted_id}} ] new_article = articleListEntity(Articles.aggregate(pipeline))[0] return new_article except DuplicateKeyError: raise HTTPException(status_code=status.HTTP_409_CONFLICT, detail=f"Article with title: '{article.id}' already exists")
Question: I have access to six 24GB GPUs. When I try to load some HuggingFace models, for example the following from transformers import AutoTokenizer, AutoModelForSeq2SeqLM tokenizer = AutoTokenizer.from_pretrained("google/ul2") model = AutoModelForSeq2SeqLM.from_pretrained("google/ul2")  I get an out of memory error, as the model only seems to be able to load on a single GPU. However, while the whole model cannot fit into a single 24GB GPU card, I have 6 of these and would like to know if there is a way to distribute the model loading across multiple cards, to perform inference. HuggingFace seems to have a webpage where they explain how to do this but it has no useful content as of today.

Answer: When you load the model using from_pretrained(), you need to specify which device you want to load the model to. Thus, add the following argument, and the transformers library will take care of the rest: model = AutoModelForSeq2SeqLM.from_pretrained("google/ul2", device_map = 'auto') Passing "auto" here will automatically split the model across your hardware in the following priority order: GPU(s) > CPU (RAM) > Disk. Of course, this answer assumes you have cuda installed and your environment can see the available GPUs. Running nvidia-smi from a command-line will confirm this. Please report back if you run into further issues.
Question: I am failing to get a minimal working example running with the following setup:  azure function in docker container python as language, specifically the "new Python programming model V2"  I followed the instructions from here but added the V2 flag, specifically:  # init directory func init --worker-runtime python --docker -m V2 # build docker image docker build -t foo . # run functions locally docker run -p 80:80 foo  Whatever I tried, the runtime seems to not pick up the auto generated http trigger function # function_app.py (autogenerated by func init ...) import azure.functions as func app = func.FunctionApp() @app.function_name(name="HttpTrigger1") @app.route(route="hello") # HTTP Trigger def test_function(req: func.HttpRequest) -> func.HttpResponse: return func.HttpResponse("HttpTrigger1 function processed a request!!!")  I think the relevant part of the logs is: info: Host.Startup[327] 1 functions found info: Host.Startup[315] 0 functions loaded info: Host.Startup[0] Generating 0 job function(s) warn: Host.Startup[0] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.). info: Microsoft.Azure.WebJobs.Script.WebHost.WebScriptHostHttpRoutesManager[0] Initializing function HTTP routes No HTTP routes mapped  because when I use the "programming model V1", then the Microsoft.Azure.WebJobs.Script.WebHost.WebScriptHostHttpRoutesManager actually prints some info about the mapped routes. How can I fix this? Is this not supported at the moment?

Answer: It seems there are ongoing changes on this. I was able to get it working by changing the environment variables in the auto generated dockerfile: # To enable ssh & remote debugging on app service change the base image to the one below # FROM mcr.microsoft.com/azure-functions/python:4-python3.10-appservice FROM mcr.microsoft.com/azure-functions/python:4-python3.10 ENV AzureWebJobsScriptRoot=/home/site/wwwroot \ AzureFunctionsJobHost__Logging__Console__IsEnabled=true \ AzureWebJobsFeatureFlags=EnableWorkerIndexing \ # added by me AzureWebJobsStorage=UseDevelopmentStorage=true # added by me COPY requirements.txt / RUN pip install -r /requirements.txt COPY . /home/site/wwwroot
Question: I tried many times installing several virtual environments using pyenv, but the system shows a error in missingno library. This is : msno.matrix(df) `ValueError Traceback (most recent call last) Cell In[17], line 1 ----> 1 msno.matrix(df) File c:\Users\sarud\.pyenv\venvs\ETLs\lib\site-packages\missingno\missingno.py:72, in matrix(df, filter, n, p, sort, figsize, width_ratios, color, fontsize, labels, sparkline, inline, freq, ax) 70 # Remove extraneous default visual elements. 71 ax0.set_aspect('auto') ---> 72 ax0.grid(b=False) 73 ax0.xaxis.tick_top() 74 ax0.xaxis.set_ticks_position('none') File c:\Users\sarud\.pyenv\venvs\ETLs\lib\site-packages\matplotlib\axes\_base.py:3196, in _AxesBase.grid(self, visible, which, axis, **kwargs) 3194 _api.check_in_list(['x', 'y', 'both'], axis=axis) 3195 if axis in ['x', 'both']: -> 3196 self.xaxis.grid(visible, which=which, **kwargs) 3197 if axis in ['y', 'both']: 3198 self.yaxis.grid(visible, which=which, **kwargs) File c:\Users\sarud\.pyenv\venvs\ETLs\lib\site-packages\matplotlib\axis.py:1655, in Axis.grid(self, visible, which, **kwargs) 1652 if which in ['major', 'both']: 1653 gridkw['gridOn'] = (not self._major_tick_kw['gridOn'] 1654 if visible is None else visible) -> 1655 self.set_tick_params(which='major', **gridkw) 1656 self.stale = True ... 1073 % (key, allowed_keys)) 1074 kwtrans.update(kw_) 1075 return kwtrans ValueError: keyword grid_b is not recognized; valid keywords are ['size', 'width', 'color', 'tickdir', 'pad', 'labelsize', 'labelcolor', 'zorder', 'gridOn', 'tick1On', 'tick2On', 'label1On', 'label2On', 'length', 'direction', 'left', 'bottom', 'right', 'top', 'labelleft', 'labelbottom', 'labelright', 'labeltop', 'labelrotation', 'grid_agg_filter', 'grid_alpha', 'grid_animated', 'grid_antialiased', 'grid_clip_box', 'grid_clip_on', 'grid_clip_path', 'grid_color', 'grid_dash_capstyle', 'grid_dash_joinstyle', 'grid_dashes', 'grid_data', 'grid_drawstyle', 'grid_figure', 'grid_fillstyle', 'grid_gapcolor', 'grid_gid', 'grid_in_layout', 'grid_label', 'grid_linestyle', 'grid_linewidth', 'grid_marker', 'grid_markeredgecolor', 'grid_markeredgewidth', 'grid_markerfacecolor', 'grid_markerfacecoloralt', 'grid_markersize', 'grid_markevery', 'grid_mouseover', 'grid_path_effects', 'grid_picker', 'grid_pickradius', 'grid_rasterized', 'grid_sketch_params', 'grid_snap', 'grid_solid_capstyle', 'grid_solid_joinstyle', 'grid_transform', 'grid_url', 'grid_visible', 'grid_xdata', 'grid_ydata', 'grid_zorder', 'grid_aa', 'grid_c', 'grid_ds', 'grid_ls', 'grid_lw', 'grid_mec', 'grid_mew', 'grid_mfc', 'grid_mfcalt', 'grid_ms']`  I don't know the error, but I installed a similar virtualenv using conda doesn't show that error. I installed different python versions using pyenv (3.11.2, 3.7.6, 3.9.13, 3.9.5), but in each one shows the same error when I install missingno. I show the image on error, I use VS code as IDE. At beginning, I thought it was the VS code version, but installing libraries using conda the error doesn't appeear.

Answer: I believe argument b has been renamed to visible. An earlier version: matplotlib/axes/_base.py A recent version: matplotlib/axes/_base.py
Question: From the YOLOv8 documentation, it is not clear to me which loss metric the YOLOv8 trainer class uses in determining the best loss model that is saved in a training run. Is it based on the validation or training loss? Specifically, when I look at the outputs from a YOLOv8 training run, I do not see any metadata indicating which epoch resulted in the best loss model saved at runs/train/weights/best.pt

Answer: In the save_model function you can see that it uses the maximum fitness to save the best model. The fitness is defined as the weighted combination of 4 metrics [P, R, mAP@0.5, mAP@0.5:0.95]. P and R are disregarded for some reason. mAP@0.5, mAP@0.5:0.95 are weighted 0.1 and 0.9 respectively. If fitness cannot be found, loss is used instead
Question: I have two 2-d tensors, which align via broadcasting, so if I add/subtract them, I incur a huge 3-d tensor. I don't really need that though, since I'll be performing a mean on one dimension. In this demo, I unsqueeze the tensors to show how they align, but they are 2-d otherwise. x = torch.tensor(...) # (batch , 1, B) y = torch.tensor(...) # (1, , A, B) out = torch.cos(x - y).mean(dim=2) # (batch, B)  Possible Solutions:  An algebraic simplification, but for the life of me I haven't solved this yet.  Some PyTorch primitive that'll help? This is cosine similarity, but, a bit different than torch.cosine_similarity. I'm applying it to complex numbers' .angle()s.  Custom C/CPython code that loops efficiently.  Other?  

Answer: To save memory I recommend using torch.einsum: We can make use of the trigonometric identity cos(x-y) = cos(x)*cos(y) + sin(x)*sin(y)  In this case we can apply einsum where the usual summing will be the averaging, and the + between the two produces will be another operation later, so in short xs, ys = torch.sin(x), torch.sin(y) xc, yc = torch.cos(x), torch.cos(y) # use einsum for sin/cos products and averaging sum, use + for sum of products: out = (torch.einsum('i k, j k -> i k', xs, ys) + torch.einsum('i k, j k -> i k', xc, yc)) / y.shape[1]  While measuring the memory consumption is a little bit tedious, I resorted to just measuring time as a proxy. Here you can see your original method and my proposal for various sizes of inputs. (The script for generating these plots is attached below.)  import matplotlib.pyplot as plt import torch import time def main(): ns = torch.logspace(1, 3.2, 20).to(torch.long) tns = []; tes = [] for n in ns: tn, te = compare(n) tns.append(tn); tes.append(te) plt.loglog(ns, tns, ':.'); plt.loglog(ns, tes, '.-'); plt.loglog(ns, 1e-6*ns**1, ':'); plt.loglog(ns, 1e-6*ns**2, ':'); plt.legend(['naive', 'einsum', 'x^1', 'x^2']); plt.show() def compare(n): batch = a = b = n x = torch.zeros((batch, b)) # (batch , 1, B) y = torch.zeros((a, b)) # (1, , A, B) t = time.perf_counter(); ra = af(x.unsqueeze(1), y.unsqueeze(0)); print('naive method', tn := time.perf_counter() - t) t = time.perf_counter(); rb = bf(x, y); print('einsum method', te := time.perf_counter() - t) print((ra-rb).abs().max()) # verify we have same results return tn, te def af(x, y): return torch.cos(x - y).mean(dim=2) def bf(x, y): xs, ys = torch.sin(x), torch.sin(y) xc, yc = torch.cos(x), torch.cos(y) return (torch.einsum('i k, j k -> i k', xs, ys) + torch.einsum('i k, j k -> i k', xc, yc)) / y.shape[1] main()
Question: I understand Polars Series can be exported to a Python list. However, is there any way I can convert a Polars Dataframe to a Python list? In addition, if there is a one single column in the Polars Dataframe, how can I convert that into a Polars Series? I tried to use the pandas commands but it didn't work. I also checked the official Polars website to see any related built-in functions, but I didn't see any.

Answer: How about the rows() method? df = pl.DataFrame( { "a": [1, 3, 5], "b": [2, 4, 6], } )  df.rows()  [(1, 2), (3, 4), (5, 6)] df.rows(named=True)  [{'a': 1, 'b': 2}, {'a': 3, 'b': 4}, {'a': 5, 'b': 6}] Alternatively, you could get all the DataFrame's columns using the get_columns() method, which would give you a list of Series, which you can then convert into a list of lists using the to_list() method on each Series in the list. If that's not what you're looking for be sure to hit me up with a reply, maybe then I'll be able to be of help to you.