Python Forum
Can't properly shape an array
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Can't properly shape an array
#1
Hello everybody. I'm running a script where I'm trying to predict with sklearn the values of a variable, called involvement (measured as a number from 1 to 6) from a variable called valence (number from -1 to 1). These values came from a questionnaire where some people rated a list of sentences (valence) or answered some personality questions (immersion).
I should obtain an array (51, 125) for my independent variable and an array (51, 1) for my dependent variable, but when I apply the numpy.array method to my list I only get one dimensional array.

To be clear I should get something like this:
train_valence (20, 125) [[1. 2. 4. ... 5. 5. 6.]
 [5. 3. 5. ... 4. 4. 6.]
 [5. 5. 4. ... 5. 6. 7.]
 ...
 [4. 4. 3. ... 4. 4. 4.]
 [6. 6. 7. ... 6. 7. 6.]
 [6. 2. 7. ... 4. 4. 5.]]
but instead I get
train_valence (51,) [list([4.0, 2.0, 3.0, 2.0, 2.0, 4.0, 3.0, 4.0, 4.0, 3.0, 2.0, 4.0, 3.0, 4.0, 3.0, 3.0, 4.0, 2.0, 3.0, 3.0, 2.0, 2.0, 2.0, 1.0, 1.0, 4.0, 3.0, 5.0, 4.0, 4.0, 2.0, 3.0, 5.0, 4.0, 4.0, 3.0, 2.0, 4.0, 4.0, 3.0, 3.0, 3.0, 5.0, 3.0, 3.0, 2.0, 3.0, 2.0, 2.0, 3.0, 2.0, 3.0, 4.0, 2.0, 3.0, 3.0, 3.0, 2.0, 3.0, 3.0, 3.0, 4.0, 3.0, 3.0, 2.0, 3.0, 3.0, 4.0, 3.0, 3.0, 3.0, 2.0, 3.0, 4.0, 1.0, 3.0, 3.0, 3.0, 4.0, 4.0, 3.0, 4.0, 4.0, 3.0, 3.0, 2.0, 3.0, 3.0, 4.0, 4.0, 3.0, 4.0, 3.0, 3.0, 4.0, 4.0, 4.0, 3.0, 4.0, 4.0, 3.0, 3.0, 3.0, 4.0, 3.0, 4.0, 3.0, 4.0, 3.0, 3.0, 4.0, 3.0, 4.0, 4.0, 2.0, 4.0, 3.0, 3.0, 4.0, 3.0, 3.0, 4.0, 4.0, 4.0, 4.0])
 list([2.0, 5.0, 2.0, 2.0, 1.0, 4.0, 2.0, 2.0, 4.0, 3.0, 3.0, 4.0, 2.0, 3.0, 4.0, 4.0, 2.0, 4.0, 1.0, 2.0, 2.0, 4.0, 4.0, 4.0, 3.0, 2.0, 3.0, 4.0, 4.0, 4.0, 3.0, 2.0, 5.0, 5.0, 4.0, 1.0, 4.0, 2.0, 3.0, 1.0, 2.0, 2.0, 3.0, 2.0, 2.0, 4.0, 5.0, 2.0, 2.0, 2.0, 2.0, 4.0, 2.0, 2.0, 2.0, 3.0, 2.0, 5.0, 3.0, 4.0, 4.0, 2.0, 3.0, 2.0, 2.0, 2.0, 2.0, 2.0, 4.0, 2.0, 2.0, 2.0, 3.0, 2.0, 4.0, 4.0, 4.0, 3.0, 2.0, 3.0, 3.0, 3.0, 5.0, 3.0, 2.0, 4.0, 1.0, 2.0, 4.0, 2.0, 3.0, 4.0, 2.0, 3.0, 3.0, 2.0, 2.0, 2.0, 4.0, 3.0, 4.0, 3.0, 2.0, 2.0, 4.0, 2.0, 2.0, 4.0, 2.0, 3.0, 3.0, 2.0, 4.0, 4.0, 5.0, 3.0, 2.0, 3.0, 4.0, 2.0, 3.0, 4.0, 3.0, 4.0, 3.0]) #(...)
Thanks in advance for your help
Reply
#2
You don't show how you build the array.
Reply
#3
Sorry, you're right
list_train_arousal = []
list_train_valence = []
list_test_arousal = []
list_test_valence = []
for case in cases:                                                              
    for n in case:                                                                 # Extract Arousal and Valence
        if case == train_case:
            set = ratings_set[ratings_set['CASE'] == n]
            list_train_arousal.append(list(set.loc[:,['AROUSAL_RATING']]['AROUSAL_RATING']))
            list_train_valence.append(list(set.loc[:,['VALENCE_RATING_RECODED']]['VALENCE_RATING_RECODED']))
        else:
            set = ratings_set[ratings_set['CASE'] == n]
            list_test_arousal.append(list(set.loc[:,['AROUSAL_RATING']]['AROUSAL_RATING']))
            list_test_valence.append(list(set.loc[:,['VALENCE_RATING_RECODED']]['VALENCE_RATING_RECODED']))


train_arousal = np.array(list_train_arousal)
train_valence = np.array(list_train_valence)


test_arousal = np.array(list_test_arousal)
test_valence = np.array(list_test_valence)
Reply
#4
It is as if they were vectors of lists instead of a matrix, I already tried to use .asarray or to reshape somehow but it doesn't work. I'd really appreciate any help, thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  output shape problem with np.arange alan6690 5 668 Dec-26-2023, 05:44 PM
Last Post: deanhystad
  simplekml change shape&color issac_n 2 2,819 Aug-20-2022, 07:15 PM
Last Post: Joseph_Paintsil
  ValueError: shape mismatched: objects cannot be broadcast to a single shape Laplace12 0 4,500 Jul-14-2020, 11:45 AM
Last Post: Laplace12
  Receiving ValueError("bad input shape {0}".format(shape)) error SuryaCitizen 2 3,503 Jun-01-2020, 06:45 AM
Last Post: pyzyx3qwerty
  Fill a value in triangle shape in Matrix lynx 0 1,869 Dec-07-2019, 06:32 AM
Last Post: lynx
  ValueError: could not broadcast input array from shape (1555200) into shape (0) promach 1 3,870 Jun-18-2018, 08:00 AM
Last Post: promach

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020