我在自己的函数中遇到了一些值错误

在这个函数中,因子k返回字典a中的值,然后将值保存到列表中,接着通过将fid_t作为scaler进行Fit_transforming返回列表,但是出现了错误。问题出在哪里?

def determineRank2(t, target, bid_t, k):    encode = LabelEncoder()    scaler = MinMaxScaler()    # x = np.concatenate((t,n,bid_t,w,h,k),axis = 1).reshape(1,6,1)    t = categorize_time(t)    bid_t = scaler.fit_transform(bid_t)    a = {'a':0, 'b':1, 'c':2, 'd':3, 'e':4, 'f':5, 'g':6, 'h':7, 'i':8,     'j':9, 'k':10, 'l':11, 'm':12, 'n':13, 'o':14, 'p':15, 'q':16, 'r':17,'w':18,     'x':19, 'y':20, 'z':21, 'ab':22, 'cd':23, 'ef':24, 'gh':25, 'qw':26, 'er':27,     'dz':28, 'df':29}    new_list = []    new_list = [t,target,bid_t,k]    new_list = np.array(new_list)    new_list = new_list.reshape(1, 1, 4)    rank = model.predict(new_list)    rank = round(rank.item(0))    return rank

当我输入类似这样的值时

determineRank2("21:30:04", 3620,2 , "a")

错误显示如下

<ipython-input-76-cea3690890e8> in determineRank2(t, target, bid_t, k) 11     # x = np.concatenate((t,n,bid_t,w,h,k),axis = 1).reshape(1,6,1) 12     t = categorize_time(t)---> 13     bid_t = scaler.fit_transform(bid_t)/usr/local/lib/python3.6/dist-packages/sklearn/base.py in fit_transform(self, X, y, **fit_params)551         if y is None:552             # fit method of arity 1 (unsupervised transformation)--> 553             return self.fit(X, **fit_params).transform(X)554         else:555             # fit method of arity 2 (supervised transformation)/usr/local/lib/python3.6/dist-packages/sklearn/preprocessing/data.py in fit(self, X, y)323         # Reset internal state before fitting324         self._reset()--> 325         return self.partial_fit(X, y)326 327     def partial_fit(self, X, y=None):/usr/local/lib/python3.6/dist-packages/sklearn/preprocessing/data.py in partial_fit(self, X, y)351         X = check_array(X, copy=self.copy,352                         estimator=self, dtype=FLOAT_DTYPES,--> 353                         force_all_finite="allow-nan")354 355         data_min = np.nanmin(X, axis=0)/usr/local/lib/python3.6/dist-packages/sklearn/utils/validation.py in check_array(array, accept_sparse, accept_large_sparse, dtype, order, copy, force_all_finite, ensure_2d, allow_nd, ensure_min_samples, ensure_min_features, warn_on_dtype, estimator)512                     "Reshape your data either using array.reshape(-1, 1) if "513                     "your data has a single feature or array.reshape(1, -1) "--> 514                     "if it contains a single sample.".format(array))515             # If input is 1D raise error516             if array.ndim == 1:ValueError: Expected 2D array, got scalar array instead:array=2.0.Reshape your data either using array.reshape(-1, 1) if your data has a single feature or array.reshape(1, -1) if it contains a single sample.

我该如何修复它?


回答:

正如错误信息所述,你应该重塑你的 bid_t 值。

sklearnscaler 文档 中可以看到:

参数: X: 形状为 [n_samples, n_features] 的 numpy 数组

这里你的 bid_t 甚至不是一个数组。所以你必须让它看起来像一个数组:

determineRank2("21:30:04", 3620, np.array([[2]]) , "a")

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注