预测旧金山的犯罪,ValueError

时间:2016-11-08 08:12:10

标签: python scikit-learn jupyter-notebook data-science

我在尝试执行项目时遇到了此错误:ValueError: Found arrays with inconsistent numbers of samples: [878049 884262]

当我尝试在底部运行我的knn分类器时,我得到它。我一直在阅读它,我知道这是因为我的X和y不一样。 X的形状是(878049,2),y是(884262,)。

如何修复此错误以使它们匹配?

代码:

# drop features that we wont be using
# train.head()
df = train.drop(['Descript', 'Resolution', 'Address'], axis=1)

df2 = test.drop(['Address'], axis=1)

# trying to see the times during a day a particular crime occurs, for example
# rapes occur more from 12am-4am during the weekend.
# example below
dow = {
    'Monday':0,
    'Tuesday':1,
    'Wednesday':2,
    'Thursday':3,
    'Friday':4,
    'Saturday':5,
    'Sunday':6
}
df['DOW'] = df.DayOfWeek.map(dow)

# Add column containing time of day
df['Hour'] = pd.to_datetime(df.Dates).dt.hour

# making my feature column
feature_cols = ['DOW', 'Hour']
X = df[feature_cols] 

df2['DOW'] = df2.DayOfWeek.map(dow)


y = df2['DOW']

# columns in X and y don't match
print(X.shape)
print(y.shape)
print(y.head())
print(X.head())

# Knn classifier
k = 5
my_knn_for_cs4661 = KNeighborsClassifier(n_neighbors=k)
my_knn_for_cs4661.fit(X, y)

# KNN (with k=5), Decision Tree accuracy
y_predict = my_knn_for_cs4661.predict(X)
print('\n')
score = accuracy_score(y, y_predict)

print("K=",k,"Has ",score, "Accuracy")
results = pd.DataFrame()
results['actual'] = y
results['prediction'] = y_predict 
print(results.head(10))

堆栈追踪:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-11-5a002c1fd668> in <module>()
      7 k = 5
      8 my_knn_for_cs4661 = KNeighborsClassifier(n_neighbors=k)
----> 9 my_knn_for_cs4661.fit(X, y)
     10 #KNN (with k=5), Decision Tree accuracy
     11 y_predict = my_knn_for_cs4661.predict(X)

C:\Users\Michael\Anaconda3\lib\site-packages\sklearn\neighbors\base.py in fit(self, X, y)
    776         """
    777         if not isinstance(X, (KDTree, BallTree)):
--> 778             X, y = check_X_y(X, y, "csr", multi_output=True)
    779 
    780         if y.ndim == 1 or y.ndim == 2 and y.shape[1] == 1:

C:\Users\Michael\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_X_y(X, y, accept_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, multi_output, ensure_min_samples, ensure_min_features, y_numeric, warn_on_dtype, estimator)
    518         y = y.astype(np.float64)
    519 
--> 520     check_consistent_length(X, y)
    521 
    522     return X, y

C:\Users\Michael\Anaconda3\lib\site-packages\sklearn\utils\validation.py in check_consistent_length(*arrays)
    174     if len(uniques) > 1:
    175         raise ValueError("Found arrays with inconsistent numbers of samples: "
--> 176                          "%s" % str(uniques))
    177 
    178 

ValueError: Found arrays with inconsistent numbers of samples: [878049 884262]

1 个答案:

答案 0 :(得分:0)

使用X.shape检查X和y的形状。堆栈跟踪表示您在X和y中具有不同的实例(没有样本)。这就是fit函数抛出ValueError的原因。

请参阅documentation说明:

"""Fit the model using X as training data and y as target values
        Parameters
        ----------
        X : {array-like, sparse matrix, BallTree, KDTree}
            Training data. If array or matrix, shape [n_samples, n_features],
            or [n_samples, n_samples] if metric='precomputed'.
        y : {array-like, sparse matrix}
            Target values, array of float values, shape = [n_samples]
             or [n_samples, n_outputs]
        """

简单来说,

X is (878049, 2) -> n_samples  = 878049 and n_features = 2
y is (884262,)  -> Here, n_samples = 884262

您正在传递额外的目标值。减少y中的目标值。由于X的n_samples是878049,因此必须传递相同数量的目标值(878049)。

您可以尝试:

my_knn_for_cs4661.fit(X, y[:878049])

参考: sklearn error ValueError: Input contains NaN, infinity or a value too large for dtype('float64')

接受的答案状态:&#34;我的输入数组的尺寸偏斜,因为我的输入csv有空格。&#34;

检查您的源文件。