Ad

Re-arranging A Numpy Array To 1d

- 1 answer

here is a numpy array i call this_col = [18 18 18 ... 24 24 24]

I have tried to reshape my data in many ways

        print("yo")
        print(this_col.shape)
        try:
            min_max_scaler = preprocessing.MinMaxScaler()
            print(this_col)
            this_col = pd.Series(min_max_scaler.fit_transform(this_col))
        except Exception as e:
            print("the exception ")
            print(e)
            try:
                print("no og ")
                this_col = this_col.reshape(-1, 1)
                print(this_col)
                min_max_scaler = preprocessing.MinMaxScaler()
                this_col = pd.Series(min_max_scaler.fit_transform(this_col))
            except Exception as e:
                print("the exception ")
                print(e)
                try:
                    print("no .reshape(-1, 1) ")
                    this_col = this_col.reshape(1, -1)
                    print(this_col)
                    min_max_scaler = preprocessing.MinMaxScaler()
                    this_col = pd.Series(min_max_scaler.fit_transform(this_col))
                except Exception as e:
                    print("the exception ")
                    print(e)
                    print("no .reshape(1, -1) ")
                    print(9/0)

below is the output i recieve from this code

  yo
(34144,)
[18 18 18 ... 24 24 24]
the exception 
Expected 2D array, got 1D array instead:
array=[18. 18. 18. ... 24. 24. 24.].
Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.
no og 
[[18]
 [18]
 [18]
 ...
 [24]
 [24]
 [24]]
the exception 
Data must be 1-dimensional
no .reshape(-1, 1) 
[[18 18 18 ... 24 24 24]]
the exception 
Data must be 1-dimensional
no .reshape(1, -1) 
ZeroDivisionError: division by zero

surely 1 of these 3 arrangments must have worked!!!! >:(

UPDATE: i have redone the example above to include shape and exception messages

UPDATE AGAIN: im starting to suspect the issue may be with the min_max function. the first error states "Expected 2D array, got 1D array instead" and then the second and third error states "Data must be 1-dimensional". what does it want?

Ad

Answer

Your error messages are coming from two different places.

On the one hand, the error message "Expected 2D array, got 1D array instead" comes from passing a 1D array to min_max_scaler.fit_transform() when it needs a 2D array.

On the other hand, the error message "Data must be 1-dimensional" comes from passing a 2D array to the pd.Series() constructor when it needs a 1D array.

The expression pd.Series(min_max_scaler.fit_transform(this_col)) is always going to fail because if this_col is 1D, then min_max_scaler.fit_transform() will fail, and if this_col is 2D, then the output of min_max_scaler.fit_transform() will also be 2D, and pd.Series() cannot accept that output.

You probably want to do something like this:

this_col_transformed = min_max_scaler.fit_transform(this_col.reshape(-1, 1))
this_col_series = pd.Series(this_col_transformed.ravel())
Ad
source: stackoverflow.com
Ad