- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 KNNClassifier 训练模型。我将数据拆分如下:
X_train, X_test, y_train, y_test = train_test_split(X_bow, y, test_size=0.30, random_state=42)
y_train= y_train.astype('int')
neigh = KNeighborsClassifier(n_neighbors=3)
neigh.fit(X_train, y_train)
当我尝试测试它时,出现值错误。
pre = neigh.predict(y_test)
Expected 2D array, got 1D array instead:
array=[0. 1. 1. ... 0. 0. 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.
我的 y_test 类型为 pandas.core.series.Series
那么如何将 pandas.core.series.Series 转换为 2D 数组以使此测试有效?
我尝试将 y_test 转换为数据帧,然后转换为数组,但出现另一个值错误,并且陷入困境。
y_test = pd.DataFrame(y_test)
y_test = y_test.as_matrix().reshape(-1,1)
pre = neigh.predict(y_test)
ValueError: Incompatible dimension for X and Y matrices: X.shape[1] == 1 while Y.shape[1] == 6038
最佳答案
我想你需要使用你的X_test
变量/数组,而不是y_test
。
X_test
是用于测试模型准确性的独立
变量/特征,y_test
是实际的目标
将与预测值进行比较的值。
示例:
pre = neigh.predict(X_test)
测量准确性:
from sklearn.metrics import accuracy_score
accuracy_score(y_test, pre)
关于python - 如何将 pandas.core.series.Series 类型转换为二维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55644465/
有时,我倾向于(重复)执行next next a 来获取特定元素。当您需要 2 次或更少的遍历时,这很有效。然而,它很快就会变得很麻烦。对于这个简单的情况,循环的开销太大。 幸运的是,如果您知道位置,
我在使用值为 numpy 数组的 pandas.Series 时遇到了以下奇怪的行为。 % s = pd.Series([5,2], index=[6,7]) %s.loc[6] 5 <-- ret
我有一个看起来像这样的数据框(小版本): A B C 0 125 ADB [AF:12] 1 189 ACB [AF:78, AF:85, AF:98] 2 148 ADB
我在 Pandas (s1) 中创建了一个系列,用于根据原始 DataFrame 中的列 ('d1') 计算这些固定数字 (1-14) 的实例数。我想要的显示在这里(时报); s1 Last
pandas series 有两个密切相关的属性:Series.index 和 Series.index.values。 这两个中的第一个返回某些 pandas 索引类型的当前索引。它是可变的,可用于
我正在尝试使用 KNNClassifier 训练模型。我将数据拆分如下: X_train, X_test, y_train, y_test = train_test_split(X_bow, y, t
我只是尝试对我的数据框进行排序并使用了以下函数: df[df.count >= df.count.quantile(.95)] 返回错误: AttributeError: 'function' obj
我试过了 print(type(numbers[2])) numbers[2].tolist() print(type(numbers[2])) 那是行不通的。我得到了 Numbers 是一个矩阵
我想从时间戳中减去日期。settings.dataset_end_date 是一个 pandas._libs.tslibs.timestamps.Timestamp引用['date_of_patent
我有一个带有数据的 pandas.core.series.Series 0 [00115840, 00110005, 001000033, 00116000... 1 [00267285,
s = pd.Series( nr.randint( 0, 10, 5 ), index=nr.randint(0, 10, 5 ) ) s 输出 1 3 7 6 2 0 9
pandas.DataFrame.query() 方法非常适合在加载或绘图时(预/后)过滤数据。它对于方法链特别方便。 我发现自己经常想将相同的逻辑应用于 pandas.Series,例如在完成诸如返
这个问题在这里已经有了答案: Difference between map, applymap and apply methods in Pandas (11 个回答) 去年关闭。 Series.ma
我正在总结一系列中的值,但根据我如何做,我会得到不同的结果。我试过的两种方法是: sum(df['series']) df['series'].sum() 为什么它们会返回不同的值? 示例代码。 s
我有一个字符串说 type(abc) >>str 我想把它转换成 pandas.core.series.Series。 我在 pandas 文档中看到有一段代码 pd.to_string() 将 pa
我有一个字符串说 type(abc) >>str 我想把它转换成 pandas.core.series.Series。 我在 pandas 文档中看到有一段代码 pd.to_string() 将 pa
这个问题在这里已经有了答案: Pandas: select DF rows based on another DF (5 个答案) 关闭 5 年前。 如果我有一个包含开始时间和结束时间的 DataF
我尝试了 Series.index 和 Series.keys() 并且输出是相似的。我找不到它们之间的显着差异。它们是否适用于某些特殊条件? 我在 Anaconda 上的 Jupyter Noteb
我有一个(非常大的)系列,其中包含关键字(例如,每行包含多个由“-”分隔的关键字 In[5]: word_series Out[5]: 0 the-cat-is-pink 1
我需要使用 pandas.read_excel 通过 Python 获取 Excel 电子表格最后一个单元格的值。该单元格包含一个日期,我需要将其分配给 Python 脚本中的变量。格式为2018-1
我是一名优秀的程序员,十分优秀!