使用“ SimpleImputer”替换“ NaN”值时出现错误

时间:2020-05-10 10:54:03

标签: python python-3.x pandas dataframe

我尝试了所有替换这些值的方法,但均失败了! 我正在著名的“泰坦尼克号”数据集上执行此操作! 这是数据的一瞥:

Webpack compilation complete. Watching for file changes.
Webpack build done!
Project successfully prepared (android)
Start sending initial files for device Nexus 6P (2e6517cd-cc2c-45a8-8915-525d6e437822).
Successfully sent initial files for device Nexus 6P (2e6517cd-cc2c-45a8-8915-525d6e437822).
LOG from device Nexus 6P: [Vue warn]: Error in created hook: "TypeError: Cannot read property 'getters' of undefined"
LOG from device Nexus 6P: '{NSVue (Vue: 2.6.10 | NSVue: 2.5.0)} -> CreateElement(NativeFrame)'
LOG from device Nexus 6P: 
LOG from device Nexus 6P: An uncaught Exception occurred on "main" thread.
Unable to start activity ComponentInfo{org.nativescript.preview/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed
TypeError: Cannot read property 'getters' of undefined

StackTrace:
java.lang.RuntimeException: Unable to start activity ComponentInfo{org.nativescript.preview/com.tns.NativeScriptActivity}: com.tns.NativeScriptException: Calling js method onCreate failed
TypeError: Cannot read property 'getters' of undefined
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: com.tns.NativeScriptException: Calling js method onCreate failed
TypeError: Cannot read property 'getters' of undefined
    at com.tns.Runtime.callJSMethodNative(Native Method)
    at com.tns.Runtime.dispatchCallJSMethodNative(Runtime.java:1286)
    at com.tns.Runtime.callJSMethodImpl(Runtime.java:1173)
    at com.tns.Runtime.callJSMethod(Runtime.java:1160)
    at com.tns.Runtime.callJSMethod(Runtime.java:1138)
    at com.tns.Runtime.callJSMethod(Runtime.java:1134)
    at com.tns.NativeScriptActivity.onCreate(NativeScriptActivity.java:20)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    ... 9 more

这是代码(只有不完整的部分):

Survived    Pclass  Sex Age SibSp   Fare    Embarked
0   0   3   male    22.0    1   7.2500  S
1   1   1   female  38.0    1   71.2833 C
2   1   3   female  26.0    0   7.9250  S
3   1   1   female  35.0    1   53.1000 S
4   0   3   male    35.0    0   8.0500  S
... ... ... ... ... ... ... ...
886 0   2   male    27.0    0   13.0000 S
887 1   1   female  19.0    0   30.0000 S
888 0   3   female  NaN 1   23.4500 S
889 1   1   male    26.0    0   30.0000 C
890 0   3   male    32.0    0   7.7500  Q

尝试上面的代码会给我这个错误:

from sklearn.impute import SimpleImputer
imputer = SimpleImputer(strategy = 'mean')
imputer.fit(([stats['Age']]))
imputer.transform(stats['Age'])

另一种尝试:

ValueError: Expected 2D array, got 1D array instead:

此代码给出以下错误:

stats['Age'].reshape(-1,1)
imputer.fit(stats['Age'])
imputer.transform(stats['Age'])

也尝试过:

AttributeError: 'Series' object has no attribute 'reshape'

再次遇到相同的错误!

我也尝试过进行其他一些小的更改,但仍然不断出现不同的错误。

1 个答案:

答案 0 :(得分:0)

问题非常简单,transform函数采用2D矩阵。因此,您要做的就是在使用stats["Age"]时将transform放在方括号内,就像这样:

imputer.transform([ stats['Age'] ])

此外,我建议使用fit_transform,而不要先使用fit,然后再使用transform

相关问题