首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用SciKit-Learn预测新的房价?

如何使用SciKit-Learn预测新的房价?
EN

Stack Overflow用户
提问于 2021-09-03 21:17:07
回答 1查看 87关注 0票数 1

我目前没有机器学习的经验,所以我决定尝试一下在线课程。我正在尝试的项目是波士顿住房数据集。

我想知道如何将新的DataFrame boston_df2添加到我当前的DataFrame boston_df1中,这样我就可以做出新的预测。我尝试使用下面的追加选项。我的最终目标是对boston_df_append (boston_df1 + boston_df2)做一个价格预测。

我注意到有人问了一个非常类似的问题,但我没有一个明确的答案:How to make prediction using the Boston housing dataset?

请不要因为我问了类似的问题而贬低我。我还在学习。=)

代码语言:javascript
复制
#import boston housing dataset

import pandas as pd

import numpy as np

from sklearn.model_selection import train_test_split

from sklearn.datasets import load_boston

from sklearn.ensemble import RandomForestRegressor

#load boston data

boston = load_boston()

boston;

#create boston_df1 DataFrame

boston_df1 = pd.DataFrame(boston['data'], columns = boston['feature_names'])

boston_df1['target'] = pd.Series(boston['target'])

#Random seed

np.random.seed(42)

#create the data

X = boston_df1.drop('target', axis=1)

y = boston_df1['target']

#split into train and test

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2)

#instantiate and fit model

model = RandomForestRegressor().fit(X_train, y_train)

#make predictions on new data   

y_preds = model.predict(X_test)

y_preds[-1]

#check model score (accuracy)

model.score(X_test, y_test)

我想添加一个新房子的数据,boston_df2,来预测房子的价格。

代码语言:javascript
复制
#add boston_df2 DataFrame

boston_df2 = {'CRIM': 0.6, 'ZN': 0.0, 'INDUS': 2.5, 'CHAS': 0.0, 'NOX': 0.6, 'RM': 8.0, 'AGE': 80, 'DIS': 5.0, 'RAD': 2.0, 'TAX': 300.0, 'PTRATIO': 20.0, 'B': 400.0, 'LSTAT': 10.0}

boston_df_append = boston_df1.append(boston_df2, ignore_index=True)

有没有人对我如何实现这一点有什么建议?预先感谢您的帮助!=)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-09-03 21:49:09

从你的字典开始预测新的价格:

代码语言:javascript
复制
boston_df2 = pd.DataFrame.from_dict(boston_df2, orient='index').T
boston_df2['target'] = model.predict(boston_df2)
代码语言:javascript
复制
>>> boston_df2['target']
0    35.981
Name: target, dtype: float64

>>> boston_df2
   CRIM   ZN  INDUS  CHAS  NOX   RM   AGE  DIS  RAD    TAX  PTRATIO      B  LSTAT  target
0   0.6  0.0    2.5   0.0  0.6  8.0  80.0  5.0  2.0  300.0     20.0  400.0   10.0  35.981
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69050634

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档