-1

私は長い間、1 つの間違いを修正しようとしてきました。

この問題は、データフレームの最初の行 (おそらく最初の 2 行) を削除することで解決できます (そう思います)。ところで。私はGoogle Colab ..xで働いています

誰でも問題を解決する方法を知っていますか?

def preprocess_df(df):
    df = df.drop("future", 1) 

    for col in df.columns:  
        if col != "target":  
            df[col] = df[col].pct_change()  
            df.dropna(inplace=True)
            df[col] = preprocessing.scale(df[col].values) 
    df.dropna(inplace=True) 

...

main_df = pd.DataFrame() 

ratios = ["EURCZK=X"]
for ratio in ratios:
    dataset = f'EURCZK=X/{ratio}.csv'
    df = pd.read_csv('EURCZK=X.csv', names=['Date', 'High', 'Low', 'Open', 'Close', 'Volume', 'Adj Close'], skiprows=2) 
    
    df.rename(columns={"close": f"{ratio}_close", "volume": f"{ratio}_volume"}, inplace=True)
    
    df.set_index("Date", inplace=True)
    df = df[[f"Close", f"Volume"]]  

    if len(main_df)==0:  
        main_df = df  
    else:  
        main_df = main_df.join(df)

main_df.fillna(method="ffill", inplace=True) 
main_df.dropna(inplace=True)
#print(main_df.head())  
main_df['future'] = main_df[f'{RATIO_TO_PREDICT}'].shift(-FUTURE_PERIOD_PREDICT)
main_df['target'] = list(map(classify, main_df[f'Close'], main_df['future']))

main_df.dropna(inplace=True)
#print(main_df.tail(10)) 

Date = sorted(main_df.index.values)
last_5pct = sorted(main_df.index.values)[-int(0.05*len(Date))]  

validation_main_df = main_df[(main_df.index >= last_5pct)]  
main_df = main_df[(main_df.index < last_5pct)]  

print(preprocess_df)
print(df.head)

imputer = imputer(missing_values="NaN", strategy="mean", axis=0)
train_x, train_y = preprocess_df(main_df)
validation_x, validation_y = preprocess_df(validation_main_df) #Preprocess dat

#print(f"train data: {len(train_x)} validation: {len(validation_x)}")
#print(f"Dont buys: {train_y.count(0)}, buys: {train_y.count(1)}")
#print(f"VALIDATION Dont buys: {validation_y.count(0)}, buys: {validation_y.count(1)}")

出力は次のとおりです。

<function preprocess_df at 0x7fc2568ceb70>
<bound method NDFrame.head of                 Close  Volume     future  target
Date                                            
2003-12-02  32.337502     0.0  32.580002       1
2003-12-03  32.410000     0.0  32.349998       0
2003-12-04  32.580002     0.0  32.020000       0
2003-12-05  32.349998     0.0  32.060001       0
2003-12-08  32.020000     0.0  32.099998       1
...               ...     ...        ...     ...
2020-07-28  26.263800     0.0  26.212500       0
2020-07-29  26.196301     0.0  26.238400       1
2020-07-30  26.212500     0.0  26.258400       1
2020-08-02  26.238400     0.0  26.105101       0
2020-08-03  26.258400     0.0  26.228500       0

[4302 rows x 4 columns]>
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-10-49204f0a12cf> in <module>()
     80 
     81 #imputer = imputer(missing_values="NaN", strategy="mean", axis=0)
---> 82 train_x, train_y = preprocess_df(main_df)
     83 validation_x, validation_y = preprocess_df(validation_main_df)
     84 

2 frames
<ipython-input-10-49204f0a12cf> in preprocess_df(df)
     28             df[col] = df[col].pct_change() 
     29             df.dropna(inplace=True) 
---> 30             df[col] = preprocessing.scale(df[col].values) 
     31 
     32     df.dropna(inplace=True)

/usr/local/lib/python3.6/dist-packages/sklearn/preprocessing/_data.py in scale(X, axis, with_mean, with_std, copy)
    140     X = check_array(X, accept_sparse='csc', copy=copy, ensure_2d=False,
    141                     estimator='the scale function', dtype=FLOAT_DTYPES,
--> 142                     force_all_finite='allow-nan')
    143     if sparse.issparse(X):
    144         if with_mean:

/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)
    584                              " minimum of %d is required%s."
    585                              % (n_samples, array.shape, ensure_min_samples,
--> 586                                 context))
    587 
    588     if ensure_min_features > 0 and array.ndim == 2:

ValueError: Found array with 0 sample(s) (shape=(0,)) while a minimum of 1 is required by the scale function.

84 行目の '#' を削除すると ("imputer = imputer(missing_values="NaN", strategy="mean", axis=0)")、答えが返されます: 'name' imputer 'is not defined'。問題は、この「Imputer」を定義する方法がわからない..

4

1 に答える 1