Samar Pratap Singh
Samar Pratap Singh

Reputation: 531

MinMaxScaler is showing weird output on any of NumPy array

I am having a numpy array that you can get by the folowing lines of code:

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import requests
from sklearn.preprocessing import MinMaxScaler
url = "https://query1.finance.yahoo.com/v7/finance/download/RELIANCE.BO?period1=1577110559&period2=1608732959&interval=1d&events=history&includeAdjustedClose=true"
r = requests.get(url)
open(stock+'.csv','wb').write(r.content) #________download the stock data and save it in a csv
r = pd.read_csv(r'RELIANCE.csv',date_parser='Date') #________read the dataset
r.head(1) #view the sample of dataset

I want to convert it to numbers between 0 and 1 with the MinMaxScaler,So for that I wrote the following code:

rc = r['Close'] #________select only the Close column
rc = np.array(rc) #________convert it into a np.array
def remove_nan(ac): #________the dataset has an NaN value, so to remove it I made this function 
    array1 = np.array(ac)
    nan_array = np.isnan(array1)
    not_nan_array = ~ nan_array
    ac = array1[not_nan_array]
    return ac
rc = remove_nan(rc)
rc = rc.reshape(1, -1) #________reshaping the data for conversion, as asked in the fucntion
scale = MinMaxScaler() #________initialize the scaler
rc = scale.fit_transform(rc) #________transform the data
print('rc') #see the result

But on implementing the code, I got an np.array that contains only 0s:

[[0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.
  0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]]

Upvotes: 1

Views: 90

Answers (1)

mandulaj
mandulaj

Reputation: 763

MinMaxScaler expects the first dimension to index the individual samples. That is, the shape in your case must be (NUM_SAMPLES, 1). But you reshaped it to be (1, NUM_SAMPLES). Try the code with:

rc = rc.reshape(-1, 1)

Upvotes: 1

Related Questions