Python Forum

Full Version: Can't properly shape an array
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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
You don't show how you build the array.
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)
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.