gpt4 book ai didi

python - Python:朴素贝叶斯拟合函数给出TypeError:float()参数必须是字符串或数字

转载 作者:太空宇宙 更新时间:2023-11-04 05:08:29 24 4
gpt4 key购买 nike

以下Python 2.7代码给出了错误:

beta=0.5

from sklearn.naive_bayes import GaussianNB
nb = GaussianNB()
print "Y_Train data: ",y_train
print "X_Train data: ",X_train
nb.fit(X_train,y_train)

nb.fit(X_train,y_train)

Traceback (most recent call last):
File "<ipython-input-116-c1ccb2227816>", line 1, in <module>
nb.fit(X_train,y_train)
File "C:\Anaconda3\envs\py27\lib\site-packages\sklearn\naive_bayes.py", line
173, in fit
X, y = check_X_y(X, y)
File "C:\Anaconda3\envs\py27\lib\site-packages\sklearn\utils\validation.py",
line 510, in check_X_y
ensure_min_features, warn_on_dtype, estimator)
File "C:\Anaconda3\envs\py27\lib\site-packages\sklearn\utils\validation.py",
line 393, in check_array
array = array.astype(np.float64)
TypeError: float() argument must be a string or a number

print train.dtypes


id int64

时间戳记datetime64 [ns]

full_sq float64

product_type int64

子区域int64

area_m float64

raion_popul int64

green_zone_part float64

indust_part float64

children_preschool int64

preschool_education_centers_raion int64

children_school int64

school_education_centers_raion int64

school_education_centers_top_20_raion float64

Healthcare_centers_raion int64

university_top_20_raion float64

sport_objects_raion float64

Additional_education_raion float64

culture_objects_top_25 int64

culture_objects_top_25_raion float64

shopping_centers_raion float64

office_raion float64

Thermal_power_plant_raion int64

incineration_raion int64

oil_chemistry_raion int64

radiation_raion int64

railroad_terminal_raion int64

big_market_raion int64

nuclear_reactor_raion int64

detention_facility_raion int64

full_all float64

male_f float64

female_f float64

young_all int64

young_male int64

young_female int64

work_all int64

work_male int64

work_female int64

ekder_all int64

ekder_male int64

ekder_female int64

0_6_all int64

0_6_male int64

0_6_female int64

7_14_all int64

7_14_male int64

7_14_female int64

0_17_all int64

0_17_male int64

0_17_female int64

16_29_all float64

16_29_male float64

16_29_female float64

0_13_all int64

0_13_male int64

0_13_female int64

ID_metro int64

metro_min_avto float64

metro_km_avto float64

幼儿园_公里float64

school_km float64

park_km float64

green_zone_km float64

industrial_km float64

water_treatment_km float64

cemetery_km float64

incineration_km float64

railroad_station_avto_km float64

railroad_station_avto_min float64

ID_railroad_station_avto int64

public_transport_station_km float64

public_transport_station_min_walk float64

water_km float64

water_1line int64

mkad_km float64

ttk_km float64

sadovoe_km float64

bulvar_ring_km float64

克里姆林宫_公里float64

big_road1_km float64

ID_big_road1 int64

big_road1_1line int64

big_road2_km float64

ID_big_road2 int64

railroad_km float64

railroad_1line int64

zd_vokzaly_avto_km float64

ID_railroad_terminal int64

bus_terminal_avto_km float64

ID_bus_terminal int64

oil_chemistry_km float64

nuclear_reactor_km float64

radiation_km float64

power_transmission_line_km float64

Thermal_power_plant_km float64

ts_km float64

big_market_km float64

market_shop_km float64

Fitness_km float64

swim_pool_km float64

ice_rink_km float64

Stadium_km float64

篮球_公里float64

hospice_morgue_km float64

detention_facility_km float64

public_healthcare_km float64

university_km float64

workshops_km float64

shopping_centers_km float64

office_km float64

Additional_education_km float64

preschool_km float64

big_church_km float64

church_synagogue_km float64

mosque_km float64

theatre_km float64

museum_km float64

Exhibition_km float64

catering_km float64

生态学

green_part_500 float64

prom_part_500 float64

office_count_500 float64

office_sqm_500 float64

trc_count_500 float64

trc_sqm_500 float64

cafe_count_500 float64

cafe_count_500_na_price float64

cafe_count_500_price_500 float64

cafe_count_500_price_1000 float64

cafe_count_500_price_1500 float64

cafe_count_500_price_2500 float64

cafe_count_500_price_4000 float64

cafe_count_500_price_high float64

big_church_count_500 float64

church_count_500 float64

mosque_count_500 float64

leisure_count_500 float64

sport_count_500 float64

market_count_500 float64

green_part_1000 float64

prom_part_1000 float64

office_count_1000 float64

office_sqm_1000 float64

trc_count_1000 float64

trc_sqm_1000 float64

cafe_count_1000 float64

cafe_count_1000_na_price float64

cafe_count_1000_price_500 float64

cafe_count_1000_price_1000 float64

cafe_count_1000_price_1500 float64

cafe_count_1000_price_2500 float64

cafe_count_1000_price_4000 float64

cafe_count_1000_price_high float64

big_church_count_1000 float64

church_count_1000 float64

mosque_count_1000 float64

leisure_count_1000 float64

sport_count_1000 float64

market_count_1000 float64

green_part_1500 float64

prom_part_1500 float64

office_count_1500 float64

office_sqm_1500 float64

trc_count_1500 float64

trc_sqm_1500 float64

cafe_count_1500 float64

cafe_count_1500_na_price float64

cafe_count_1500_price_500 float64

cafe_count_1500_price_1000 float64

cafe_count_1500_price_1500 float64

cafe_count_1500_price_2500 float64

cafe_count_1500_price_4000 float64

cafe_count_1500_price_high float64

big_church_count_1500 float64

church_count_1500 float64

mosque_count_1500 float64

leisure_count_1500 float64

sport_count_1500 float64

market_count_1500 float64

green_part_2000 float64

prom_part_2000 float64

office_count_2000 float64

office_sqm_2000 float64

trc_count_2000 float64

trc_sqm_2000 float64

cafe_count_2000 float64

cafe_count_2000_na_price float64

cafe_count_2000_price_500 float64

cafe_count_2000_price_1000 float64

cafe_count_2000_price_1500 float64

cafe_count_2000_price_2500 float64

cafe_count_2000_price_4000 float64

cafe_count_2000_price_high float64

big_church_count_2000 float64

church_count_2000 float64

mosque_count_2000 float64

leisure_count_2000 float64

sport_count_2000 float64

market_count_2000 float64

green_part_3000 float64

prom_part_3000 float64

office_count_3000 float64

office_sqm_3000 float64

trc_count_3000 float64

trc_sqm_3000 float64

cafe_count_3000 float64

cafe_count_3000_na_price float64

cafe_count_3000_price_500 float64

cafe_count_3000_price_1000 float64

cafe_count_3000_price_1500 float64

cafe_count_3000_price_2500 float64

cafe_count_3000_price_4000 float64

cafe_count_3000_price_high float64

big_church_count_3000 float64

church_count_3000 float64

mosque_count_3000 float64

leisure_count_3000 float64

sport_count_3000 float64

market_count_3000 int64

green_part_5000 float64

office_count_5000 float64

office_sqm_5000 float64

trc_count_5000 int64

trc_sqm_5000 int64

cafe_count_5000 float64

cafe_count_5000_na_price float64

cafe_count_5000_price_500 float64

cafe_count_5000_price_1000 float64

cafe_count_5000_price_1500 float64

cafe_count_5000_price_2500 float64

cafe_count_5000_price_4000 float64

cafe_count_5000_price_high float64

big_church_count_5000 float64

church_count_5000 float64

mosque_count_5000 int64

leisure_count_5000 float64

sport_count_5000 int64

market_count_5000 int64

price_doc float64

以上是其数据类型的列。
为什么您认为我会收到此错误?
预先感谢您的回答。

最佳答案

找出问题的原因是“怪异”。错误消息对此没有任何提示。

出现此错误的真正原因:

TypeError: float() argument must be a string or a number


在各个列中是否存在空值,这与错误所说的float()无关。
因此,我清理了数据,删除了所有NA,然后重新运行。该代码运行良好,没有任何更改。因此,如果将来在python 2.7上遇到类似的错误,则必须查看基础数据,它肯定会具有无法转换为float或数字的NA或值。删除它们,然后代码将运行正常。

关于python - Python:朴素贝叶斯拟合函数给出TypeError:float()参数必须是字符串或数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43709854/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com