首页 > 解决方案 > 如何使用波士顿住房数据集进行预测?

问题描述

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

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 数组?

标签: pythonmachine-learningscikit-learn

解决方案


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

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

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


推荐阅读