-1

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?

8
  • What's outputted when you type print(this_col.shape) ? Commented Feb 17, 2022 at 14:15
  • Why do you do print(9/0)??? Why not use raise ZeroDivisionError? Commented Feb 17, 2022 at 14:16
  • Looks like this_col is already initially 1D, before you start trying to reshape it. Commented Feb 17, 2022 at 14:18
  • @EliHarold whats the difference? Commented Feb 17, 2022 at 14:20
  • @jjramsey then surely my first attempt would work Commented Feb 17, 2022 at 14:21

2 Answers 2

1

You can use the flatten() method:

a = np.array([[1,2,3,1],[8,9,4,1],[7,6,5,1],[7,6,5,1]])
print(a.shape) # gives (4,4)
print(a.flatten().shape) # gives(16,)

Edit: You can read further info on the documentation

Sign up to request clarification or add additional context in comments.

2 Comments

i have tried many combinations of flatten and reshape
as @jjramsey has stated, it looks that "this_col" is already 1D. Try looking at this_col.shape
0

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())

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.