python - 如何使用波士顿住房数据集进行预测?
问题描述
我正在研究预测房价的教程。该代码有效,但我试图对一个新的未知数组进行预测,但我不断出错。
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 数组?
解决方案
我想我需要将数组更改为 float 以获得结果: my_array = np.array(X_new, dtype=float) reg.predict(my_array)]
我的结果如下: array([[29.99003492]])
对于波士顿住房数据集,我乘以 30 乘以 10000 得到预测的房价?那么30万是预测房价吗?
推荐阅读
- html - 为什么我的 manifest.json 文件找不到我的图标文件?
- python-3.x - 在python中使用asyncio运行两个无限运行的函数
- winforms - 使用 winform dll 运行控制台应用程序(.net CORE)时控制台未显示(仅显示表单)
- c# - 重构静态工厂类,其他工厂类需要使用其私有方法
- elasticsearch - 无法将 fluentd 中的缓冲区刷新到 elasticsearch
- docker - Docker 无法获取特定 ip
- excel - 为什么不显示边框?
- node.js - Mongoose 版本密钥和 _id 设置为 false 不工作 npm 版本 5.12.9
- javascript - 如何告诉间隔等待最后一个http调用?
- javascript - 在 React 中使用 Webpack 创建缩小的 CSS 文件