in Education by
In [31]: print(np.poly1d((3,2))) 3 x + 2 In [32]: a = np.array(( np.poly1d((3,2)), np.poly1d((3,2)) )) I expected that array a were a (2,) shaped array of numpy.lib.polynomial.poly1d objects, BUT In [33]: a.shape Out[33]: (2, 2) In [34]: type(a[0,0]) Out[34]: numpy.int64 In [35]: a Out[35]: array([[2, 2], [2, 2]]) What's going on? It's worth to be mentioned that In [36]: a = np.array(( np.poly1d((3,2,1)), np.poly1d((3,2,1)) )) raises an error --------------------------------------------------------------------------- ValueError Traceback (most recent call last) in ----> 1 a = np.array(( np.poly1d((3,2,1)), np.poly1d((3,2,1)), np.poly1d((3,2,1)) )) ValueError: cannot copy sequence with size 2 to array axis with dimension 3 JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
A poly1d object is iterable In [1]: np.poly1d((3,2)) Out[1]: poly1d([3, 2]) In [2]: list(_) Out[2]: [3, 2] np.array tries to makes a multidimensional numeric array from its inputs, iterating where possible. That's why making an array from these poly1d object ends up looking like you did np.array([[3,2],[3,2]]). The most reliable way to create an object dtype array is to initialize a 'blank' one and fill it. In [12]: arr = np.empty(2, object) In [13]: arr[:] = [np.poly1d((3,2)), np.poly1d((4,2))] In [14]: arr Out[14]: array([poly1d([3, 2]), poly1d([4, 2])], dtype=object) But do you really need an object dtype array? Why not stick with a list of the poly1d objects? === We've observed in other SO that attempting to create a object dtype array can go several different ways. Some times it works, sometimes you get a numeric array, and sometimes an error. In [17]: np.array([np.poly1d((3,2)), np.poly1d((3,2,1))]) Out[17]: array([poly1d([3, 2]), poly1d([3, 2, 1])], dtype=object) In [18]: np.array([np.poly1d((3,2,1)), np.poly1d((3,2,1))]) --------------------------------------------------------------------------- ValueError Traceback (most recent call last) in ----> 1 np.array([np.poly1d((3,2,1)), np.poly1d((3,2,1))]) ValueError: cannot copy sequence with size 2 to array axis with dimension 3

Related questions

0 votes
    I’ve Found A Bug in Scipy. What Do I Do?...
asked Apr 25, 2021 by JackTerrance
0 votes
    Generate random integers between 0 and 9 In Python, How do I generate random integers between 0 to 9 ? Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I am trying to alter the data in the panda's df. Using below, where X >=5, I want to change the corresponding Y row to 1. Where X...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    I have a variable of z storing features of length and height of image files where z is z = [ ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 1, 2022 in Education by JackTerrance
0 votes
    I have a variable of z storing features of length and height of image files where z is z = [ ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 27, 2022 in Education by JackTerrance
0 votes
    When I try to generate a model with acumos I get this error : File "/Users/fredericchantrel/.pyenv/ ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked May 14, 2022 in Education by JackTerrance
0 votes
    I have a dataset that only contains y-values in one column. I want to insert a column of x- ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 26, 2022 in Education by JackTerrance
0 votes
    I want to encode a 1-D numpy array: x = array([1,0,3]) As a 2-D 1-hot array y = array([ ... some faster technique other than looping. Select the correct answer from above options...
asked Jan 22, 2022 in Education by JackTerrance
0 votes
    I have the 4D array, which is defined as follows: B = np.array( [[[[0.5000, 0.5625], [0.5000, ... loops and ideally one function call. Select the correct answer from above options...
asked Jan 19, 2022 in Education by JackTerrance
0 votes
    How do you think I can get the intersection of the below-given array? a=np.array([[[[0,0],[0,1]],[[1,1],[1,1]]] ... , [[0, 1], [1, 1]]] Select the correct answer from above options...
asked Jan 9, 2022 in Education by JackTerrance
0 votes
    I have col1 in a pandas df. I want to make col2: col1 col2 1 1 1 2 1 3 1 4 2 2 ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 22, 2022 in Education by JackTerrance
0 votes
    I'm trying to install some machine learning libraries on a new Windows laptop (I usually have Mac laptops ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I'm trying to install some machine learning libraries on a new Windows laptop (I usually have Mac laptops ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 12, 2022 in Education by JackTerrance
0 votes
    I'm trying to install some machine learning libraries on a new Windows laptop (I usually have Mac laptops ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 10, 2022 in Education by JackTerrance
0 votes
    Every time I want to config something with AWS I get the following error : "The config profile (myname) ... encrypt my credentials. Select the correct answer from above options...
asked Jan 30, 2022 in Education by JackTerrance
...