簡體   English   中英

ValueError:輸入包含 NaN、無窮大或對於 dtype('float64') 來說太大的值。 sklearn

[英]ValueError: Input contains NaN, infinity or a value too large for dtype('float64'). sklearn

這是我的代碼:

import pandas as pd 
df = pd.read_csv('train.csv')

gender_dict = {"male": 1, "female": 2}
eye_color_dict = {"amber": 1, "blue": 2, "brown": 3, "gray": 4, "green": 5, "hazel": 6}
race_dict = {"black": 1, "white": 2, "middle_eastern": 3,"asian":4}
accommodation_type_dict = {"apartment": 1, "homeless": 2, "shared_residence": 3, "villa": 4, "other": 5}
education_status_dict = {"associate_degree": 1, "bachelors_degree": 2, "graduate_or_professional_degree": 3, "high_school": 4, "less_than_9th_grade": 5, "not_applicable": 6}
blood_type_dict = {"A+": 1, "A-": 2, "B+": 3, "B-": 4, "O+": 5, "O-": 6, "AB+": 7, "AB-": 8}
occupation_dict = {"agriculture": 1, "art": 2, "business": 3, "education": 4, "engineering": 5, "healthcare": 6, "unemployed": 7, "other": 8}
living_area_dict = {"suburbs": 1, "rural": 2, "urban": 3, "other": 4}
sports_engagement_dict = {"never": 1, "sometimes": 2, "seldom": 3, "regularly": 4}
favorite_music_genre_dict = {"r&b": 1, "rock": 2, "pop": 3, "country": 4, "other": 5, "edm": 6, "classical": 7}
favorite_color_dict = {"green": 1, "orange": 2, "yellow": 3, "purple": 4, "blue": 5, "pink": 6, "red": 7}
owned_car_brand_dict = {"audi": 1, "bmw": 2, "ford": 3, "honda": 4, "hyundai": 5, "kia": 6, "none": 7, "tesla": 8, "other": 9, "mitsubishi": 10}
hours_worked_each_week_dict = {"not_applicable": 1}
owns_a_pet_dict = {"yes": 1, "no": 2}
has_health_insurance_dict = {"yes": 1, "no": 2}
has_cancer_dict = {"yes": 1, "no": 2}
smokes_dict = {"yes": 1, "no": 2}
has_alzheimers_dict = {"yes": 1, "no": 2}
facial_hair_dict = {"long": 1, "short": 2, "none": 3}
diet_type_dict = {"regular": 1, "vegetarian": 2, "keto": 3, "vegan": 4, "low-carb": 5, "paleo": 6}

df['gender'] = df['gender'].map(gender_dict)
df['eye_color'] = df['eye_color'].map(eye_color_dict)
df['race'] = df['race'].map(race_dict)
df['accommodation_type'] = df['accommodation_type'].map(accommodation_type_dict)
df['education_status'] = df['education_status'].map(education_status_dict)
df['blood_type'] = df['blood_type'].map(blood_type_dict)
df['occupation'] = df['occupation'].map(occupation_dict)
df['living_area'] = df['living_area'].map(living_area_dict)
df['sports_engagement'] = df['sports_engagement'].map(sports_engagement_dict)
df['favorite_music_genre'] = df['favorite_music_genre'].map(favorite_music_genre_dict)
df['favorite_color'] = df['favorite_color'].map(favorite_color_dict)
df['owned_car_brand'] = df['owned_car_brand'].map(owned_car_brand_dict)
df['hours_worked_each_week'] = df['hours_worked_each_week'].map(hours_worked_each_week_dict)
df['owns_a_pet'] = df['owns_a_pet'].map(owns_a_pet_dict)
df['has_health_insurance'] = df['has_health_insurance'].map(has_health_insurance_dict)
df['has_cancer'] = df['has_cancer'].map(has_cancer_dict)
df['smokes'] = df['smokes'].map(smokes_dict)
df['has_alzheimers'] = df['has_alzheimers'].map(has_alzheimers_dict)
df['facial_hair'] = df['facial_hair'].map(facial_hair_dict)
df['diet_type'] = df['diet_type'].map(diet_type_dict)

import sklearn
from sklearn import svm, preprocessing

df = sklearn.utils.shuffle(df)

X = df.drop("infected", axis=1).values
X = preprocessing.scale(X)
y = df['infected'].values

test_size = 200

X_train = X[:-test_size]
y_train = y[:-test_size]

X_test = X[-test_size:]
y_test = y[-test_size:]

clf = svm.SVR(kernel="linear")
clf.fit(X_train,y_train)

clf.score(X_test,y_test)

for X,y in zip(X_test, y_test):
    print(f"Model: {clf.predict([X])[0]}, Actual: {y}")
  

我收到價值錯誤:

ValueError:輸入包含 NaN、無窮大或對於 dtype('float64') 來說太大的值。

它告訴我:

<ipython-input-1-8b8c4c2d113b> in <module> 62 63 clf = svm.SVR(kernel="linear") ---> 64 clf.fit(X_train,y_train) 65 66 clf.score(X_test,y_test) `

這是 train.csv 的鏈接

我正在使用 jupyter-notebook,我是 sklearn 的新手,我附上了上面的 CSV 文件,謝謝你的幫助

看起來hours_worked_each_week列包含空值。

如果刪除該列,是否會出現相同的錯誤:

X = df.drop(['infected', 'hours_worked_each_week'], axis=1).values

或者,您可以將空值替換為 0

df.fillna(0,inplace=True)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM