对房价数据集进行处理和数据分析

大家好,我是带我去滑雪,每天教你一个小技巧!

房价数据集通常包含各种各样的特征,如房屋面积、地理位置、建造年份等。通过对数据进行处理和分析,可以更好地理解这些特征之间的关系,以及它们对房价的影响程度。这有助于确定哪些特征是最重要的,从而更有针对性地制定房地产策略。本次使用波士顿房价数据集boston_housing_data.csv,该数据集有城镇人均犯罪率(CRIM)、住宅用地所占比例(ZN)、城镇中非住宅用地所占比例(INDUS)等共计13个特征变量,响应变量为社区房价中位数(MEDV)。实现对房价数据进行可视化和统计分析:如绘制直方图、密度图、箱线图以及查看各个散点图的分布,最后使用支持向量机和KNN等几种机器学习方法进行学习。下面开始实战。

(1)导入相关模块

python 复制代码
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
from numpy import arange
from matplotlib import pyplot
from pandas import read_csv
from pandas import  set_option
from pandas.plotting import scatter_matrix
from sklearn.preprocessing import StandardScaler
from sklearn.model_selection import train_test_split
from sklearn.model_selection import KFold
from sklearn.model_selection import cross_val_predict
from sklearn.model_selection import cross_val_score
from sklearn.model_selection import GridSearchCV
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import Lasso
from sklearn.linear_model import ElasticNet
from sklearn.tree import DecisionTreeRegressor
from sklearn.neighbors import KNeighborsRegressor
from sklearn.svm import SVR
from sklearn.pipeline import Pipeline
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.ensemble import ExtraTreesRegressor
from sklearn.ensemble import AdaBoostRegressor
from sklearn.metrics import mean_squared_error
from sklearn.feature_selection import SelectKBest
from sklearn.feature_selection import f_regression

(2)导入数据并进行可视化分析

python 复制代码
def testHouse():
    data = pd.read_csv("house_data.csv")
    set_option('display.column_space', 120)
    print(data.shape)
    print(data.isnull().any().sum())
    prices = data['MEDV']
    features = data.drop('MEDV', axis=1)
    # 直方图
    data.hist(sharex=False, sharey=False, xlabelsize=1, ylabelsize=1)
    pyplot.show()
    # 密度图
    data.plot(kind='density', subplots=True, layout=(4, 4), sharex=False, fontsize=1)
    pyplot.show()
    # 箱线图
    data.plot(kind='box', subplots=True, layout=(4, 4), sharex=False, sharey=False, fontsize=8)
    pyplot.show()
    # 查看各个特征的散点分布
    scatter_matrix(data, alpha=0.7, figsize=(10, 10), diagonal='kde')
    pyplot.show()
    # Heatmap
testHouse()

结果展示:

绘制房价数据的直方图:

绘制房价数据的密度图:

绘制 房价数据的箱线图:

查看房价数据各个特征的散点分布:

(3)使用支持向量机和KNN等机器学习方法学习

python 复制代码
def featureSelection():
    data = pd.read_csv("house_data.csv")
    x = data[['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD', 'TAX',
              'PTRATIO', 'B', 'LSTAT']]
    # print(x.head())
    y = data['MEDV']
    from sklearn.feature_selection import SelectKBest
    SelectKBest = SelectKBest(f_regression, k=3)
    bestFeature = SelectKBest.fit_transform(x, y)
    SelectKBest.get_support(indices=False)
    # print(SelectKBest.transform(x))
    print(x.columns[SelectKBest.get_support(indices=False)])
    features = data[['RM', 'PTRATIO', 'LSTAT']].copy()
    from sklearn.preprocessing import MinMaxScaler
    scaler = MinMaxScaler()
    for feature in features.columns:
        features.loc[:, '标准化' + feature] = scaler.fit_transform(features[[feature]])
    # 散点可视化,查看特征归一化后的数据
    font = {
        'family': 'SimHei'
    }
    x_train, x_test, y_train, y_test = train_test_split(features[['标准化RM', '标准化PTRATIO', '标准化LSTAT']], y,
                                                        test_size=0.3, random_state=33)

    import warnings
    warnings.filterwarnings(action="ignore", module="scipy", message="^internal gelsd") #过滤告警

    lr = LinearRegression()
    lr_predict = cross_val_predict(lr, x_train, y_train, cv=5)
    lr_score = cross_val_score(lr, x_train, y_train, cv=5)
    lr_meanscore = lr_score.mean()
    #SVR
    from sklearn.svm import SVR
    linear_svr = SVR(kernel = 'linear')
    linear_svr_predict = cross_val_predict(linear_svr, x_train, y_train, cv=5)
    linear_svr_score = cross_val_score(linear_svr, x_train, y_train, cv=5)
    linear_svr_meanscore = linear_svr_score.mean()

    poly_svr = SVR(kernel = 'poly')
    poly_svr_predict = cross_val_predict(poly_svr, x_train, y_train, cv=5)
    poly_svr_score = cross_val_score(poly_svr, x_train, y_train, cv=5)
    poly_svr_meanscore = poly_svr_score.mean()
    rbf_svr = SVR(kernel = 'rbf')
    rbf_svr_predict = cross_val_predict(rbf_svr, x_train, y_train, cv=5)
    rbf_svr_score = cross_val_score(rbf_svr, x_train, y_train, cv=5)
    rbf_svr_meanscore = rbf_svr_score.mean()
    knn = KNeighborsRegressor(2, weights='uniform')
    knn_predict = cross_val_predict(knn, x_train, y_train, cv=5)
    knn_score = cross_val_score(knn, x_train, y_train, cv=5)
    knn_meanscore = knn_score.mean()

    dtr = DecisionTreeRegressor(max_depth=4)
    dtr_predict = cross_val_predict(dtr, x_train, y_train, cv=5)
    dtr_score = cross_val_score(dtr, x_train, y_train, cv=5)
    dtr_meanscore = dtr_score.mean()

    evaluating = {
        'lr': lr_score,
        'linear_svr': linear_svr_score,
        'poly_svr': poly_svr_score,
        'rbf_svr': rbf_svr_score,
        'knn': knn_score,
        'dtr': dtr_score
    }
    evaluating = pd.DataFrame(evaluating)
    print(evaluating)

def main():
if __name__ == "__main__":
    main()

输出结果:

复制代码
Index(['RM', 'PTRATIO', 'LSTAT'], dtype='object')
         lr  linear_svr  poly_svr   rbf_svr       knn       dtr
0  0.738899    0.632970  0.866308  0.758355  0.806363  0.787402
1  0.755418    0.618558  0.865458  0.772783  0.888141  0.871562
2  0.433104    0.386320  0.569238  0.529242  0.590950  0.545247
3  0.604445    0.554785  0.723299  0.740388  0.728388  0.583349
4  0.793609    0.611882  0.805474  0.736040  0.863620  0.824755

需要数据集的家人们可以去百度网盘(永久有效)获取:

链接:https://pan.baidu.com/s/173deLlgLYUz789M3KHYw-Q?pwd=0ly6

提取码:2138


更多优质内容持续发布中,请移步主页查看。

若有问题可邮箱联系:1736732074@qq.com

博主的WeChat:TCB1736732074

点赞+关注,下次不迷路!

相关推荐
武子康3 小时前
大数据-212 数据挖掘 机器学习理论 - 无监督学习算法 KMeans 基本原理 簇内误差平方和
大数据·人工智能·学习·算法·机器学习·数据挖掘
Q8137574604 小时前
数据挖掘在金融交易中的应用:民锋科技的智能化布局
人工智能·科技·数据挖掘
布说在见4 小时前
魅力标签云,奇幻词云图 —— 数据可视化新境界
信息可视化·数据挖掘·数据分析
Tianyanxiao5 小时前
如何利用探商宝精准营销,抓住行业机遇——以AI技术与大数据推动企业信息精准筛选
大数据·人工智能·科技·数据分析·深度优先·零售
FIT2CLOUD飞致云6 小时前
仪表板展示|DataEase看中国:历年双十一电商销售数据分析
数据分析·开源·数据可视化·dataease·双十一
皓7416 小时前
服饰电商行业知识管理的创新实践与知识中台的重要性
大数据·人工智能·科技·数据分析·零售
菜鸟的人工智能之路7 小时前
桑基图在医学数据分析中的更复杂应用示例
python·数据分析·健康医疗
阡之尘埃14 小时前
Python数据分析案例61——信贷风控评分卡模型(A卡)(scorecardpy 全面解析)
人工智能·python·机器学习·数据分析·智能风控·信贷风控
布说在见16 小时前
层次与网络的视觉对话:树图与力引导布局的双剑合璧
信息可视化·数据挖掘·数据分析
全栈开发圈1 天前
新书速览|Spark SQL大数据分析快速上手
sql·数据分析·spark