预测旧金山的犯罪,ValueError

predicting crime in san francisco, ValueError

我 运行 在尝试做一个项目时遇到这个错误: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]

使用 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])

参考:

已接受的答案状态:"The Dimensions of my input array were skewed, as my input csv had empty spaces."

检查你的源文件。