【问题标题】:How to make prediction using the Boston housing dataset?如何使用波士顿住房数据集进行预测?
【发布时间】:2020-03-12 02:43:30
【问题描述】:

我正在学习预测房价的教程。该代码有效,但我试图对一个新的未知数组进行预测,但我不断收到错误。

import pandas as pd
import numpy as np
from sklearn import linear_model
from sklearn.model_selection import train_test_split
from sklearn.datasets import load_boston
boston = load_boston()

reg = linear_model.LinearRegression()
x_train, x_test, y_train, y_test = train_test_split(df_x, df_y, test_size=0.33, random_state=42)

reg.fit(x_train, y_train)

代码有效,但我想测试一个新示例:

X_new = [['15.7','20.5','18.9', '21.7', '20.4', '18.2', '19.9', '23.1', '17.5', '20.2', '18.2', 
'13.6', '19.6']]

reg.predict(X_new)

我收到以下错误消息:“UFuncTypeError: ufunc 'matmul' 不包含签名匹配类型的循环 (dtype('dtype('

我不确定我做错了什么。我是否必须将 X_new 更改为字符串列表,或者将它们保留为 numpy 数组?

【问题讨论】:

  • 值得将您的x_test 与您的x_new 在数据类型和形状方面进行比较。您能否在您的问题中提供一行x_test 进行比较?
  • x_test.shape (167, 13) X_new [['15.7', '20.5', '18.9', '21.7', '20.4', '18.2', '19.9', '23.1' , '17.5', '20.2', '18.2', '13.6', '19.6']]
  • 请根据发布指南提供预期的minimal, reproducible example

标签: python machine-learning scikit-learn


【解决方案1】:

我想我需要将数组更改为浮动以获得结果: my_array = np.array(X_new, dtype=float) reg.predict(my_array)]

我的结果如下:array([[29.99003492]])

对于波士顿住房数据集,我将 30 乘以 10000 得到预测的房价?那么30万是预测的房价吗?

【讨论】:

    猜你喜欢
    • 2017-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-24
    • 2013-01-13
    • 2019-01-10
    • 2018-04-20
    相关资源
    最近更新 更多