eshangj/stackoverflow_q_and_a_sample huggingface dataset:
Question: When running the following code, I get the following Error: Traceback (most recent call last): File "/Users/crosseyedbum/Documents/Visual Studio Code/Fundamentals of Python_5.py", line 127, in <module> sumo = reduce(lambda a, b : a[1] + b[1], exp) File "/Users/crosseyedbum/Documents/Visual Studio Code/Fundamentals of Python_5.py", line 127, in <lambda> sumo = reduce(lambda a, b : a[1] + b[1], exp) TypeError: 'int' object is not subscriptable  I am attempting to sum the integers in each tuple and set the value to sumo. from functools import reduce exp = [ ('Dinner', 80), ('Car repair', 120), ('Netflix', 30), ('Rocket Fuel', 32) ] #stored as tuples sumo = reduce(lambda a, b : a[1] + b[1], exp) print(sumo)  OUTPUT: 200  However, the following code runs to sum the integers to 200 just fine. Can someone explain this to me. Or am I only able to use this method with a max of two items in the list** If someone can explain to a beginner why this is behaving the way it is, I would greatly appreciate it. I added tuples to my list and expected the code to continue to add the index one, but I only seem to be able to run the code with two items in my list max. That doesn't make any sense to me. The error makes no sense to me, and even less sense why the number of tuples in the list would cause it not work.

Answer: The issue is that the a parameter to your lambda function is not what you think it is. From the functools.reduce docs:  The left argument, x, is the accumulated value and the right argument, y, is the update value from the iterable  In your case, x is a and y is b due to how you named your parameters. So, a is not a tuple, it is the sum. If you don't provide a default start value, the initial accumulated value will be the first value in the iterator (('Dinner', 80) in your case). Because you cannot add integers and tuples, you get an error. Instead, pass a third parameter that will be the default (use an integer, like 0). Try this code: from functools import reduce exp = [ ('Dinner', 80), ('Car repair', 120), ('Netflix', 30), ('Rocket Fuel', 32) ] sumo = reduce(lambda a, b: a + b[1], exp, 0) print(sumo) # => 262
Question: I want to use Plotly to generate a line chart with a range slider. the range slider shows the displayed line again. this code is just an example. in my case, I have a lot of subplots and everything is shown twice. is it possible to show nothing or only the date in the range slider? import plotly.express as px import yfinance as yf yf.pdr_override() df = yf.download(tickers='aapl' ,period='1d',interval='1m') fig = px.line(df, x = df.index, y = 'Close', title='Apple Stock Price') fig.update_layout( xaxis=dict( rangeselector=dict( buttons=list([ dict(count=1, label="1m", step="month", stepmode="backward"), dict(step="all") ]) ), rangeslider=dict( visible=True ), type="date" ) ) fig.show() 

Answer: Looking through the rangeslider documentation, there aren't any arguments that can directly impact the rangeslider line because I believe that line will have all of the same properties as the line in the figure (including color, visibility, thickness). The best workaround I can come up with is to change the background color of the rangeslider to be the same color as the line (and this only works if all traces in the figure are the same color). I would probably also make the rangeslider have a relatively smaller thickness so it's less obtrusive, but that's a matter of personal taste. Here is an example: fig.update_layout( xaxis=dict( rangeselector=dict( buttons=list([ dict(count=1, label="1m", step="month", stepmode="backward"), dict(step="all") ]) ), rangeslider=dict( visible=True, bgcolor="#636EFA", thickness=0.05 ), type="date" ) ) fig.show()
Question: I am currently using the latest version of Python and attempting to install cx_Oracle through the command pip install cx_Oracle. On my first attempt, I encountered an error that stated: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools".  To address this, I installed both Microsoft C++ Build Tools from this link and Visual C++ 17 from this link. However, upon my second try, I encountered another error: Temp\pip-install-ocqmu9mg\cx-oracle_a585471535c345cea9e48a083457ccd7\odpi\src\dpiImpl.h(34): fatal error C1083: Cannot open include file: 'stdlib.h': No such file or directory  I researched this issue by consulting this, this and this post, but none of them provided a solution for my problem.

Answer: Nice effort. Unfortunately, pre-compiled binaries for Python 3.11 are not currently available. To utilize this version, you can either wait for their release or configure your system properly to build them from source. Alternatively, you could consider downgrading to a previous version of Python, although this is not recommended.
Question: Here is an example of what I want: original matrix: [[[5 1 4]] [[0 9 5]] [[8 0 9]]] matrix I want: [[[5 1 4] [5 1 4]] [[0 9 5] [0 9 5]] [[8 0 9] [8 0 9]]]  I have tried np.repeat(A, 2, axis=0), which apparently does not work since it gives the output: [[[5 1 4]] [[5 1 4]] [[0 9 5]] [[0 9 5]] [[8 0 9]]] 

Answer: You want to repeat on axis=1: np.repeat(A, 2, axis=1)  Output: array([[[5, 1, 4], [5, 1, 4]], [[0, 9, 5], [0, 9, 5]], [[8, 0, 9], [8, 0, 9]]])  NB. remember to check the shape of your arrays: A.shape -> (3, 1, 3). You want to make it (3, 2, 3), not (6, 1, 3).
Question: For a LSTM, I create time sequences by means of tensorflow.keras.utils.timeseries_dataset_from_array(). For some of the features, I would like to do one-hot encoding by means of Keras preprocessing layers. I have the following code: n_timesteps = 20 n_categorical_features = 1 from tensorflow.keras.layers import Input, IntegerLookup, CategoryEncoding cat_inp = keras.layers.Input(shape=(n_timesteps, n_categorical_features), name = "categorical_input") index = IntegerLookup() index.adapt(X["br"]) encoder = CategoryEncoding(num_tokens=index.vocabulary_size(), output_mode = "one_hot")(cat_inp)  However, the last line gives me the error ValueError: Exception encountered when calling layer "category_encoding_22" (type CategoryEncoding). When output_mode is not 'int', maximum supported output rank is 2. Received output_mode one_hot and input shape (None, 20, 1), which would result in output rank 3. The problem seems to be that CategoryEncoding does not support the shape of my input tensor (None, n_timesteps, n_categorical_features). How can I one-hot encode the input tensor produced by timeseries_dataset_from_array()?

Answer: Please try to use TimeDistributed layer: encoder = tf.keras.layers.TimeDistributed(CategoryEncoding(num_tokens=index.vocabulary_size(), output_mode = "one_hot"))(cat_inp)  It will apply CategoryEncoding to each item in your time sequence. Please see https://keras.io/api/layers/recurrent_layers/time_distributed/ for more information.
Question: With a simple setup.py file: from setuptools import setup setup( name='foo', version='1.2.3', )  I can do $> python setup.py --version 1.2.3  without installing the package. Is there similar functionality for the equivalent pyproject.toml file: [project] name = "foo" version = "1.2.3" 

Answer: With Python 3.11+, something like this should work: python3.11 -c "import tomllib; print(tomllib.load(open('pyproject.toml', 'rb'))['project']['version'])"  This parses the TOML file directly, and assumes that version is not dynamic.  In some cases, version is declared dynamic in pyproject.toml, so it can not be parsed directly from this file and a solution (the only one?) is to actually build the project, or at least its metadata. For this purpose, we can use the build.util.project_wheel_metadata() function from the build project, for example with a small script like this: #!/usr/bin/env python import argparse import pathlib import build.util def _main(): args_parser = argparse.ArgumentParser() args_parser.add_argument('path') args = args_parser.parse_args() path_name = getattr(args, 'path') path = pathlib.Path(path_name) # metadata = build.util.project_wheel_metadata(path) version = metadata.get('Version') print(version) if __name__ == '__main__': _main()  Or as a one-liner: python -c "import build.util; print(build.util.project_wheel_metadata('.').get('Version'))"
Question:  pylint: Command line or configuration file:1: UserWarning: Specifying exception names in the overgeneral-exceptions option without module name is deprecated and support for it will be removed in pylint 3.0. Use fully qualified name (maybe 'builtins.BaseException' ?) instead.  Getting PyLint error. I dont have any Except clause in my file and yet I see this error.

Answer: This is just a warning for future pylint releases, you can ignore it. If you want to address it now you will need to open your .pylintrc configuration file (should be located at ~/.pylintrc) and replace: overgeneral-exceptions=BaseException, Exception  with: overgeneral-exceptions=builtins.BaseException, builtins.Exception
Question: I have some code that looks like this: di: dict[str, float] max_val = max(di, key=di.get)  When running mypy on it, it complains error: Argument "key" to "max" has incompatible type overloaded function; expected "Callable[[str], Union[SupportsDunderLT[Any], SupportsDunderGT[Any]]]"  My guess is that this is because the return type of di.get is Union[float, None] in general, but in this specific instance, it should always return a float since the function is only being called on keys from di. The three workarounds I've discovered are to define a custom lambda function, ignore the di.get call, or cast it as follows. get_lambda = lambda key: di[key] max_val = max(di, key=get_lambda) # --------- max_val = max( di, key=di.get, # type: ignore [arg-type] ) # --------- from typing import cast, Callable max_val = max(di, key=cast(Callable[[str], float], di.get))  Is there a more proper/standard way of letting mypy know that the get method will not be returning None?

Answer: The dunder (double underscore) member function __getitem__ of a dict object is what you want here: max(di, key=di.__getitem__)  raises no complains from mypy. The reason your lambda works is because it precisely emulates the intent of __getitem__, which is to implement evaluation of self[key], as explained in the documentation.
Question: Actually i am training a deep learning model and want to save checkpoint of the model but its stopped when power is off then i have to start from that point from which its interrupted like 10 epoches completed and want to resume/start again from epoch 11 with that parameters

Answer: In PyTorch, you can resume from a specific point by using epoch key from the checkpoint dictionary as follows: # Load model checkpoint checkpoint = torch.load("checkpoint.pth") model.load_state_dict(checkpoint['model']) epoch = checkpoint['epoch'] # Resume training from a specific epoch for epoch in range(epoch + 1, num_epochs): ...
Question: I asked ChatGPT to show me how I could use OpenAi's API to interact with it in my terminal window and it generated code which I modified a little bit in order to do what I wanted. Here is the Python code: import requests with open('../api-key.txt','r') as key: data = key.read().strip() api_key = data model="text-danvinci-003" def chat_with_chatgpt(prompt): res = requests.post(f"https://api.openai.com/v1/engines/{model}/jobs", headers = { "Content-Type":"application/json", "Authorization":f"Bearer {api_key}" }, json={ "prompt":prompt, "max_tokens":100 }).json() print(res) return res.choices[0].text while True: prompt = input('Me: ') response = chat_with_chatgpt(prompt) print(f'ChatGPT: {response}')  But when I ran this code I got some messages that said: Me: hello {'error': {'message': 'That model does not exist', 'type': 'invalid_request_error', 'param': None, 'code': None}} Traceback (most recent call last): File "/data/data/com.termux/files/home/python/main.py", line 23, in <module> response = chat_with_chatgpt(prompt) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/data/data/com.termux/files/home/python/main.py", line 19, in chat_with_chatgpt return res.choices[0].text ^^^^^^^^^^^ AttributeError: 'dict' object has no attribute 'choices'  The response I got is an error dict. For some reason, I am not able to install OpenAi via pip install openai on my system, so this is the only option I have.

Answer: I believe the engines API endpoints were deprecated in favour of models. For more info read here: https://help.openai.com/en/articles/6283125-what-happened-to-engines You will want to look at the completions endpoint instead https://platform.openai.com/docs/api-reference/completions Here's an example of the URL structure and headers required, using cURL. curl https://api.openai.com/v1/completions \ -H 'Content-Type: application/json' \ -H 'Authorization: Bearer YOUR_API_KEY' \ -d '{ "model": "text-davinci-003", "prompt": "Say this is a test", "max_tokens": 7, "temperature": 0 }'  The general structure of the code should be fine, you'll just want to swap out the endpoint in use. def chat_with_chatgpt(prompt): res = requests.post(f"https://api.openai.com/v1/completions", headers = { "Content-Type": "application/json", "Authorization": f"Bearer {api_key}" }, json={ "model": model "prompt": prompt, "max_tokens": 100 }).json() return res.choices[0].text
Question: I'm not sure how the two recursive calls and the floor division of the following function interact regarding time complexity and big o notation. def recurse(n: int, k: int) -> int: if n <= 0 or k <= 0: return 1 return recurse(n//2, k) + recurse(n, k//2)  I see how O(2^(nk)) could serve as an upper bound because the k portion of (n//2,k) and the n portion of (n,k//2) dominate the growth rates. However I could also see something along the lines of (nlog(n)*mlog(m)) working as well and I'm not sure what to settle on. Edit: 

Answer: Here's an exact solution. The first thing to note is that, ignoring cases where n < 0 or k < 0, we only care about how many times we need to divide n or k by two (using floor division) before they reach zero. For example, if n = 7, then we have 7, 3, 1, 0, which is three divisions by two. The number of times a non-negative integer v can be divided by two (using floor division) before reaching zero is ceil(log2(v+1)). Let a = ceil(log2(n+1)) and b = ceil(log2(k+1)). Then each recursive call reduces either a or b by one, and continues until either a or b reaches zero. Now consider the call tree. This is a binary tree, but not a balanced tree (since some paths are longer than others). A call is a leaf call if either n or k is zero. Further, each leaf call corresponds to a single unique path through the call tree. Each recursive call reduces either a or b by one, so each leaf call corresponds to a unique monotonic path from (a, b) to either (0, x) or (x, 0), since the recursive calls end when either a or b becomes zero. We can model this by extending each path to (0, 0). Then we just need to count the number of paths from (a, b) to (0, 0). This is just (a+b)!/(a!*b!). This is a well-known result that isn't difficult to derive. It can be expressed as a multicombination (i.e., a combination in which multiple instances of a given value are allowed). So the number of leaf calls is (a+b)!/(a!*b!). The number of non-leaf calls is one less than this. So we have:  leaf calls: (a+b)!/(a!*b!) non-leaf calls: (a+b)!/(a!*b!) - 1 total calls: 2*(a+b)!/(a!*b!) - 1  The exact time complexity of recurse is O((a+b)!/(a!*b!)), where a = ceil(log2(n+1)) and b = ceil(log2(k+1)). The return value of recurse is the number of leaf calls. The function complexity below behaves identically: from math import factorial def complexity(n: int, k: int) -> int: if n <= 0 or k <= 0: return 1 a = ceil_log2(n+1) b = ceil_log2(k+1) return factorial(a + b) // (factorial(a) * factorial(b))  where ceil_log2 is defined as: def ceil_log2(x: int) -> int: if x <= 0: raise ValueError("log2 of zero or negative integer") return (x - 1).bit_length()
Question: I'm using Python 3.10 and I have: a = int(2 ** 1023 * (1 + (1 - 2 ** -52)))  Now, the value of a is the biggest integer value in double precision floating point format. So, I'm expecting float(a + 1) to give an OverflowError error, as noted in here:  If the argument is outside the range of a Python float, an OverflowError will be raised.  But, to my surprise, it doesn't throw the error, instead, it happily returns: 1.7976931348623157e+308  which seems like sys.float_info.max. I also do float(a + 2), float(a + 3), float(a + 4), etc but it still returns 1.7976931348623157e+308. Only until I do float(a + a) then it throws the expected exception: Traceback (most recent call last): File "<stdin>", line 1, in <module> OverflowError: int too large to convert to float  It seems like the smallest number that fails is a + 2 ** 970, as noted in this comment. So, what could be the reason for this?

Answer: As was commented, 2**970 is the smallest addition that rounds up. This makes sense as follows:  1023 =exp - 52 =n, where the "smallest" field is 2**-n ----- 971 L G RS so, the largest is: (2**1023)*1.111...111(1)00 123...012 3 45 // bit #s ^ 555 5 55 2**1022 ^ ^ ^ ^ 2**-971 ^ ^ ^ 2**970  ...so adding 2**970 will round up, per the LGRS=1100 values. These are the Least significant, Guard, Round, and Sticky bits from the IEEE 754 spec. This can be demo'd in python as follows: >>> import sys >>> print("%100.10f" % (sys.float_info.max)) 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0000000000 >>> # note that even though this is even in base 10, the significand is odd in binary since the Least significant bit is 1 ... >>> print("%f" % (sys.float_info.max+2**970)) inf >>> print("%100.10f" % (sys.float_info.max+2**969)) 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0000000000 >>>
Question: I have a 2D array data and a boolean array mask of shapes (M,N). I need to randomly pick an element in each row of data. However, the element I picked should be true in the given mask. Is there a way to do this without looping over every row? In every row, there are at least 2 elements for which mask is true. Minimum Working Example: data = numpy.arange(8).reshape((2,4)) mask = numpy.array([[True, True, True, True], [True, True, False, False]]) selected_data = numpy.random.choice(data, mask, num_elements=1, axis=1)  The 3rd line above doesn't work. I want something like that. I've listed below some valid solutions. selected_data = [0,4] selected_data = [1,5] selected_data = [2,5] selected_data = [3,4] 

Answer: It is easier to work with the indices of the mask. We can get the indices of the True values from the mask and stack them together to create 2D coordinates array. All of the values inside the indices2d are possible to sample. Then we can shuffle the array and get the first index of the unique row values. Since the array is shuffled, it is random choice. Then we can match the selected 2D indices to the original data. See below; import numpy data = numpy.arange(8).reshape((2,4)) mask = numpy.array([[True, True, True, True], [True, True, False, False]]) for _ in range(20): indices2d = numpy.dstack(numpy.where(mask)).squeeze().astype(numpy.int32) numpy.random.shuffle(indices2d) randomElements = indices2d[numpy.unique(indices2d[:, 0], return_index=True)[1]] print(data[randomElements[:,0],randomElements[:,1]])   Output  [0 5] [1 4] [0 5] [1 4] [1 5] [0 5] [1 4] [1 5] [3 4] [2 5] [2 4] [3 5] [2 4] [0 4] [0 4] [0 4] [0 5] [3 5] [3 5] [1 4]  12.7 ms ± 80.9 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
Question: I have 2 directories containing tests: project/ | |-- test/ | | | |-- __init__.py | |-- test_1.py | |-- my_submodule/ | |-- test/ | |-- __init__.py |-- test_2.py  How can I run all tests? python -m unittest discover . only runs test_1.py and obviously python -m unittest discover my_submodule only runs test_2.py

Answer: unittest currently sees project/my_submodule as an arbitrary directory to ignore, not a package to import. Just add project/my_submodule/__init__.py to change that.
Question: I am trying to loop through a Polars recordset using the following code: import polars as pl df = pl.DataFrame({ "start_date": ["2020-01-02", "2020-01-03", "2020-01-04"], "Name": ["John", "Joe", "James"] }) for row in df.rows(): print(row)  ('2020-01-02', 'John') ('2020-01-03', 'Joe') ('2020-01-04', 'James')  Is there a way to specifically reference 'Name' using the named column as opposed to the index? In Pandas this would look something like: import pandas as pd df = pd.DataFrame({ "start_date": ["2020-01-02", "2020-01-03", "2020-01-04"], "Name": ["John", "Joe", "James"] }) for index, row in df.iterrows(): df['Name'][index]  'John' 'Joe' 'James' 

Answer: You can specify that you want the rows to be named for row in mydf.rows(named=True): print(row)  It will give you a dict: {'start_date': '2020-01-02', 'Name': 'John'} {'start_date': '2020-01-03', 'Name': 'Joe'} {'start_date': '2020-01-04', 'Name': 'James'}  You can then call row['Name'] Note that:  previous versions returned namedtuple instead of dict. it's less memory intensive to use iter_rows overall it's not recommended to iterate through the data this way   Row iteration is not optimal as the underlying data is stored in columnar form; where possible, prefer export via one of the dedicated export/output methods.
Question: I have a 2d numpy array like so. I want to find the maximum consecutive streak of 1's for every row. a = np.array([[1, 1, 1, 1, 1], [1, 0, 1, 0, 1], [1, 1, 0, 1, 0], [0, 0, 0, 0, 0], [1, 1, 1, 0, 1], [1, 0, 0, 0, 0], [0, 1, 1, 0, 0], [1, 0, 1, 1, 0], ] )  Desired Output: [5, 1, 2, 0, 3, 1, 2, 2] I have found the solution to above for a 1D array: a = np.array([1, 1, 1, 1, 0, 1, 0, 0, 0, 1, 1, 0, 0]) d = np.diff(np.concatenate(([0], a, [0]))) np.max(np.flatnonzero(d == -1) - np.flatnonzero(d == 1)) > 4  On similar lines, I wrote the following but it doesn't work. d = np.diff(np.column_stack(([0] * a.shape[0], a, [0] * a.shape[0]))) np.max(np.flatnonzero(d == -1) - np.flatnonzero(d == 1)) 

Answer: The 2D equivalent of you current code would be using pad, diff, where and maximum.reduceat: # pad with a column of 0s on left/right # and get the diff on axis=1 d = np.diff(np.pad(a, ((0,0), (1,1)), constant_values=0), axis=1) # get row/col indices of -1 row, col = np.where(d==-1) # get groups of rows val, idx = np.unique(row, return_index=True) # subtract col indices of -1/1 to get lengths # use np.maximum.reduceat to get max length per group of rows out = np.zeros(a.shape[0], dtype=int) out[val] = np.maximum.reduceat(col-np.where(d==1)[1], idx)  Output: array([5, 1, 2, 0, 3, 1, 2, 2]) Intermediates: np.pad(a, ((0,0), (1,1)), constant_values=0) array([[0, 1, 1, 1, 1, 1, 0], [0, 1, 0, 1, 0, 1, 0], [0, 1, 1, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 1, 1, 1, 0, 1, 0], [0, 1, 0, 0, 0, 0, 0], [0, 0, 1, 1, 0, 0, 0], [0, 1, 0, 1, 1, 0, 0]]) np.diff(np.pad(a, ((0,0), (1,1)), constant_values=0), axis=1) array([[ 1, 0, 0, 0, 0, -1], [ 1, -1, 1, -1, 1, -1], [ 1, 0, -1, 1, -1, 0], [ 0, 0, 0, 0, 0, 0], [ 1, 0, 0, -1, 1, -1], [ 1, -1, 0, 0, 0, 0], [ 0, 1, 0, -1, 0, 0], [ 1, -1, 1, 0, -1, 0]]) np.where(d==-1) (array([0, 1, 1, 1, 2, 2, 4, 4, 5, 6, 7, 7]), array([5, 1, 3, 5, 2, 4, 3, 5, 1, 3, 1, 4])) col-np.where(d==1)[1] array([5, 1, 1, 1, 2, 1, 3, 1, 1, 2, 1, 2]) np.unique(row, return_index=True) (array([0, 1, 2, 4, 5, 6, 7]), array([ 0, 1, 4, 6, 8, 9, 10])) out = np.zeros(a.shape[0], dtype=int) array([0, 0, 0, 0, 0, 0, 0, 0]) out[val] = np.maximum.reduceat(col-np.where(d==1)[1], idx) array([5, 1, 2, 0, 3, 1, 2, 2])