【问题标题】:Why this errror appears during fit while creating decision Tree Classifier为什么在创建决策树分类器时出现此错误
【发布时间】:2016-09-17 07:39:04
【问题描述】:

您好,我正在通过观看此视频 Hello World - Machine Learning Recipes #1 Google Developers 尝试决策树分类器。

这是我的代码。

#Import the Pandas library
  import pandas as pd
#Load the train and test datasets to create two DataFrames
train_url = "http://s3.amazonaws.com/assets.datacamp.com/course/Kaggle/train.csv" train = pd.read_csv(train_url)
#Print the head of the train and test dataframes
train.head() 
test_url = "http://s3.amazonaws.com/assets.datacamp.com/course/Kaggle/test.csv" test = pd.read_csv(test_url)
#Print the head of the train and test dataframes
test.head()
#from sklearn import tree
from sklearn import tree
#find the best feature to predict Survival rate
#define X_features and Y_labels
col_names=['Pclass','Age','SibSp','Parch']
X_features= train[col_names]
#assign survial to label
Y_labels= train.Survived
#create a decision tree classifier
clf=tree.DecisionTreeClassifier()
#fit (find patterns in Data)
clf=clf.fit(X_features, Y_labels)
clf.predict(test[col_names])

出现错误

ValueError Traceback (last last call last) in () 13#Y_train_sparse=Y_labels.to_sparse() 14 # fit (find patterns in Data) ---> 15 clf=clf.fit(X_features, Y_labels) 16 #clf .predict(test[col_names])

C:\Users\nitinahu\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\tree\tree.py 适合(自我,X,y,sample_weight,check_input,X_idx_sorted)152 random_state = check_random_state(self.random_state) 153 如果 check_input: --> 154 X = check_array(X, dtype=DTYPE, accept_sparse="csc") 155 if issparse(X): 156 X.sort_indices()

C:\Users\nitinahu\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\utils\validation.py 在 check_array(array, accept_sparse, dtype, order, copy, force_all_finite,ensure_2d,allow_nd,ensure_min_samples, ensure_min_features,warn_on_dtype,估计器)396%(array.ndim, estimator_name)) 397 if force_all_finite: --> 398 _assert_all_finite(array) 399 400 shape_repr = _shape_repr(array.shape)

C:\Users\nitinahu\AppData\Local\Continuum\Anaconda3\lib\site-packages\sklearn\utils\validation.py 在 _assert_all_finite(X) 52 而不是 np.isfinite(X).all()): 53 raise ValueError("Input contains NaN, infinity" ---> 54 " or a value too 对于 %r 来说很大。" % X.dtype) 55 56

ValueError: 输入包含 NaN、无穷大或一个太大的值 dtype('float32').

【问题讨论】:

  • 您好,您应该参考stackoverflow.com/help/how-to-ask来帮助您入门。一般来说,当海报发布代码并询问为什么它不起作用时,一个问题并没有得到很好的接受。这表明您付出的努力很少。
  • 感谢您的建议
  • 不是valueError,可能是某些值超出了允许范围?

标签: pandas machine-learning classification decision-tree


【解决方案1】:

只需检查您在回复中获得的所有值。

一两个给出的值超出了界限,导致发生溢出。

【讨论】:

  • 我检查了'Age'列中有NaN,如何在训练和测试数据集中的'Age'列中找到NaN并替换为一些真实值
猜你喜欢
  • 2017-12-17
  • 2012-11-19
  • 2019-10-08
  • 2021-10-14
  • 2022-01-13
  • 2014-12-16
  • 2019-04-20
  • 2021-04-22
  • 2018-04-21
相关资源
最近更新 更多