kaggel-汽车价格预测项目

news2024/10/5 9:03:11

1.读取数据,查看数据基本概况

import pandas  as pd
data=pd.read_csv(r'./car_price_prediction.csv')

#查看前5行数据
print(data.head(5))

output:
             ID  Price  Levy  ...             Wheel   Color  Airbags
0  45654403  13328  1399  ...        Left wheel  Silver       12
1  44731507  16621  1018  ...        Left wheel   Black        8
2  45774419   8467     -  ...  Right-hand drive   Black        2
3  45769185   3607   862  ...        Left wheel   White        0
4  45809263  11726   446  ...        Left wheel  Silver        4

[5 rows x 18 columns]

print(data.info)

output:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 19237 entries, 0 to 19236
Data columns (total 18 columns):
 #   Column            Non-Null Count  Dtype  
---  ------            --------------  -----  
 0   ID                19237 non-null  int64  
 1   Price             19237 non-null  int64  
 2   Levy              19237 non-null  object 
 3   Manufacturer      19237 non-null  object 
 4   Model             19237 non-null  object 
 5   Prod. year        19237 non-null  int64  
 6   Category          19237 non-null  object 
 7   Leather interior  19237 non-null  object 
 8   Fuel type         19237 non-null  object 
 9   Engine volume     19237 non-null  object 
 10  Mileage           19237 non-null  object 
 11  Cylinders         19237 non-null  float64
 12  Gear box type     19237 non-null  object 
 13  Drive wheels      19237 non-null  object 
 14  Doors             19237 non-null  object 
 15  Wheel             19237 non-null  object 
 16  Color             19237 non-null  object 
 17  Airbags           19237 non-null  int64  
dtypes: float64(1), int64(4), object(13)

#可以看到数据有19237行,有18列,其中没有缺失值

#查看重复的数据
print(data.duplicated().sum())

output:
    313
#去除重复列
data.drop_duplicates(inplace=True)

#查看每一列数据的取值情况
for col in  data.columns:
    print(col)
    print(data[col].value_counts())

output:
    ID
45815365    8
45815361    8
45815363    7
45815368    7
45723475    7
           ..
45774312    1
45732621    1
45773011    1
45774019    1
45813273    1
Name: ID, Length: 18924, dtype: int64
------------------------------
Price
15681    280
470      274
14113    244
392      242
314      235
        ... 
42601      1
149        1
54349      1
54954      1
22075      1
Name: Price, Length: 2315, dtype: int64
------------------------------
Levy
-       5819
765      486
891      461
639      410
640      405
        ... 
3156       1
2908       1
1279       1
1719       1
1901       1
Name: Levy, Length: 559, dtype: int64
------------------------------
Manufacturer
HYUNDAI          3769
TOYOTA           3662
MERCEDES-BENZ    2076
FORD             1111
CHEVROLET        1069
                 ... 
TESLA               1
PONTIAC             1
SATURN              1
ASTON MARTIN        1
GREATWALL           1
Name: Manufacturer, Length: 65, dtype: int64
------------------------------
Model
Prius                    1083
Sonata                   1079
Camry                     938
Elantra                   922
E 350                     542
                         ... 
Feroza                      1
C-MAX C-MAX                 1
X1 4X4                      1
Land Cruiser Prado RX       1
Prius C aqua                1
Name: Model, Length: 1590, dtype: int64
------------------------------
Prod. year
2012    2155
2014    2124
2013    1963
2011    1612
2015    1549
2010    1483
2016    1476
2017     959
2008     737
2009     601
2018     500
2007     464
2005     402
2003     367
2004     364
2006     317
2019     306
2002     296
2000     279
2001     254
1998     213
1999     207
1997     151
1996     114
1995     105
2020      47
1994      42
1992      30
1993      23
1990      18
1988      12
1991      10
1986       6
1989       6
1987       5
1984       5
1985       5
1953       4
1983       3
1939       3
1978       2
1980       2
1965       2
1977       2
1974       2
1964       2
1943       1
1976       1
1957       1
1968       1
1947       1
1982       1
1981       1
1973       1
Name: Prod. year, dtype: int64
------------------------------
Category
Sedan          8736
Jeep           5473
Hatchback      2847
Minivan         647
Coupe           532
Universal       364
Microbus        306
Goods wagon     233
Pickup           52
Cabriolet        36
Limousine        11
Name: Category, dtype: int64
------------------------------
Leather interior
Yes    13954
No      5283
Name: Leather interior, dtype: int64
------------------------------
Fuel type
Petrol            10150
Diesel             4036
Hybrid             3578
LPG                 892
CNG                 494
Plug-in Hybrid       86
Hydrogen              1
Name: Fuel type, dtype: int64
------------------------------
Engine volume
2            3916
2.5          2277
1.8          1760
1.6          1462
1.5          1321
             ... 
6.8             1
6.7             1
3.1             1
0.8 Turbo       1
1.1 Turbo       1
Name: Engine volume, Length: 107, dtype: int64
------------------------------
Mileage
0 km         721
200000 km    183
150000 km    161
160000 km    120
100000 km    119
            ... 
63083 km       1
28750 km       1
25077 km       1
77452 km       1
186923 km      1
Name: Mileage, Length: 7687, dtype: int64
------------------------------
Cylinders
4.0     14367
6.0      3462
8.0       991
5.0       169
3.0       107
2.0        42
1.0        38
12.0       38
10.0       12
16.0        5
7.0         4
9.0         1
14.0        1
Name: Cylinders, dtype: int64
------------------------------
Gear box type
Automatic    13514
Tiptronic     3102
Manual        1875
Variator       746
Name: Gear box type, dtype: int64
------------------------------
Drive wheels
Front    12874
4x4       4058
Rear      2305
Name: Drive wheels, dtype: int64
------------------------------
Doors
04-May    18332
02-Mar      777
>5          128
Name: Doors, dtype: int64
------------------------------
Wheel
Left wheel          17753
Right-hand drive     1484
Name: Wheel, dtype: int64
------------------------------
Color
Black            5033
White            4489
Silver           3792
Grey             2375
Blue             1396
Red               639
Green             322
Orange            253
Brown             187
Carnelian red     179
Golden            145
Beige             134
Sky blue          122
Yellow            106
Purple             39
Pink               26
Name: Color, dtype: int64
------------------------------
Airbags
4     5823
12    5654
0     2405
8     1608
6     1311
2     1066
10     849
5      104
16      93
7       86
1       76
9       63
3       37
11      33
14      20
15       7
13       2
Name: Airbags, dtype: int64
------------------------------

        可以看到在Levy中‘-’有5819条记录,Engine volume这个特征有些带有单位Turbo,Doors特征带有特殊英文和大于号,Mileage特征带有km单位,Prod. year 特征代表的是汽车是哪一年生产的,需要根据这个计算汽车的已经使用了多少年 。我们将在接下来的数据清洗步骤,解决这些问题。

2.数据清洗

#数据清洗
#Levy是税的意思,'-'应该表示的是没有税,所以应该将'-'替换为0

data['Levy']=data['Levy'].apply(lambda x:x.replace('-','0')).astype(int)

#除去单位km
data['Mileage']= data['Mileage'].str.replace('km','').astype(float)

#提取正确的车门数
data["Doors"]=data["Doors"].apply(lambda x: x.split("-")[0] if "-" in x else x.replace(">", "")).astype(int)

#新增加1列特征表示是否为turbo
data['is_turbo']=data['Engine volume'].apply(lambda x:1 if 'Turbo' in x else 0)
#将Engine volume中带有的turbo单位去掉
data['Engine volume']=data['Engine volume'].str.replace('Turbo','').astype(float)
#查看特征中不同取值的个数
print(data.nunique())
output:
ID                  18924
Price                2315
Levy                  559
Manufacturer           65
Model                1590
Prod. year             54
Category               11
Leather interior        2
Fuel type               7
Engine volume         107
Mileage              7687
Cylinders              13
Gear box type           4
Drive wheels            3
Doors                   3
Wheel                   2
Color                  16
Airbags                17
is_turbo                2

from date_time import date_time

years_now=datetime.now().year

data[age']=years_now-data['Prod. year']

print(data[['Prod. year','age']].head(5))

output:
       Prod. year  age
0        2010       14
1        2011       13
2        2006       18
3        2011       13
4        2014       10

#可以看到在数据中有许多特征是类别,需要进行编码转换

category_col=['Manufacturer','Model','Category','Leather interior','Fuel type',
         'Gear box type','Drive wheels','Wheel','Color']

from sklearn.preprocessing import LabelEncoder
le = LabelEncoder()
for col in category_col:
    data[col]=le.fit_transform(data[col])

#去掉没用的特征
data.drop(columns=['ID','Prod. year'],inplace=True)

#查看是否有异常值,并且删除异常值

num_col=['Price','Levy','age','Engine volume','Mileage','Airbags']

for col in num_col:
    q1 = data[col].quantile(0.25)
    q3 = data[col].quantile(0.75)
    iqr = q3 - q1
    low = q1 - 1.5*iqr
    high = q3 + 1.5*iqr
    outlier = ((data[col] > high) | (data[col] < low)).sum()
    total_outliers = data[col].shape[0]
    print(f"Total Outliars in {col} are : {outlier} : {round(100*(outlier)/total_outliers,2)}%")
    if outlier>0:
        data=data.loc[(data[col]<=high) & (data[col]>=low)]
output:

Total Outliars in Price are : 1055 : 5.57%
Total Outliars in Levy are : 161 : 0.9%
Total Outliars in age are : 1468 : 8.29%
Total Outliars in Engine volume are : 951 : 5.86%
Total Outliars in Mileage are : 623 : 4.07%
Total Outliars in Airbags are : 0 : 0.0%

3.数据标准化

from sklearn.preprocessing import  StandardScaler

scaler=StandardScaler()
data.dropna(inplace=True)
scaled_df = scaler.fit_transform(data.drop(["Price"],axis=1))

4.建模

X=scaled_df
y=data["Price"]
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.1, random_state=42)
print("X_train shape: ", X_train.shape)
print("y_train shape: ", y_train.shape)

print("X_val shape: ", X_val.shape)
print("y_val shape: ", y_val.shape)

print("X_test shape: ", X_test.shape)
print("y_test shape: ", y_test.shape)

output:

X_train shape:  (10558, 17)
y_train shape:  (10558,)
X_val shape:  (1174, 17)
y_val shape:  (1174,)
X_test shape:  (2934, 17)
y_test shape:  (2934,)

from sklearn.linear_model import LinearRegression
from sklearn.tree import DecisionTreeRegressor
from sklearn.ensemble import RandomForestRegressor
from sklearn.ensemble import GradientBoostingRegressor
from sklearn.svm import SVR
from xgboost import XGBRegressor
from sklearn.model_selection import train_test_split,GridSearchCV

from sklearn.metrics import r2_score,mean_squared_error

def models(model):
    model.fit(X_train,y_train)
    pre=model.predict(X_test)
    r2=r2_score(y_test,pre)
    R2.append(r2)
    rmse=np.sqrt(mean_squared_error(y_test,pre))
    RMSE.append(rmse)
    score=model.score(X_test,y_test)
    #print(f"the score of the model is : {score}")

Algorithms=['LinearRegression','DecisionTreeRegressor','RandomForestRegressor','GradientBoostingClassifier','XGBRegressor','SVR']
R2=[]
RMSE=[]
model1=LinearRegression()
model2=DecisionTreeRegressor(random_state=42)
model3=RandomForestRegressor()
model4=GradientBoostingRegressor()
model5=XGBRegressor()
model6=SVR()
models(model1)
models(model2)
models(model3)
models(model4)
models(model5)
models(model6)
df1=pd.DataFrame({'Algorithms':Algorithms,'R2_score':R2,'RMSE':RMSE})

output:

                   Algorithms  R2_score          RMSE
0            LinearRegression  0.233513  10128.813642
1       DecisionTreeRegressor  0.582993   7470.986717
2       RandomForestRegressor  0.772239   5521.352505
3  GradientBoostingClassifier  0.637696   6963.746857
4                XGBRegressor  0.749141   5794.573718
5                         SVR -0.014145  11650.813825
5.深度学习模型
from keras.models import Sequential
from keras.layers import Dense
model = Sequential()
model.add(Dense(64, input_dim=X_train.shape[1], activation="linear"))
model.add(Dense(32, activation="linear"))
model.add(Dense(1, activation="linear"))
model.compile(optimizer="adam", loss="mean_squared_error", metrics=["mean_squared_error"])
result=model.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=20, verbose=0)
# print(model.evaluate())
print(np.sqrt(result.history["val_loss"]))

output:
    [18120.89683427 15209.23247943 11820.44869939 10217.87591285
  9988.96333788  9967.14313751  9955.55527899  9952.93869747
  9956.04175707  9956.86708593  9956.54835753  9952.67119581
  9959.13493298  9955.99503163  9955.22404599  9953.39106622
  9955.59464297  9956.9463529   9958.24650314  9957.58592009]

plt.figure(figsize=(15,8))
plt.plot(result.history["loss"])
plt.plot(result.history["val_loss"])
plt.title("Loss Graph")
plt.ylabel("Loss")
plt.xlabel("Epochs")
plt.legend(["Training", "Validation"])
plt.show()

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1878821.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

llama3模型部署时遇到的问题及解决方案

在llama3模型部署时&#xff0c;会遇到一系列问题&#xff0c;这里就作者所遇到的问题与解决方法分享一下。 注意&#xff1a;这里是从llama3 github主页上给的方法一步步做的&#xff0c;不适用于其他部署大模型的方法。 文章目录 ERROR 403&#xff1a;Forbidden安装依赖时出…

50-3 内网信息收集 - 域环境搭建

搭建准备: 在搭建准备阶段,我们需要准备三台 Windows 虚拟机:Windows Server 2012、Windows 7 和 Windows Server 2008。接下来,我们将配置 Windows Server 2012 作为域控制器,而 Windows 7 和 Windows Server 2008 将作为成员机加入域。建议保持这三台虚拟机的内存不超过…

Go环境安装---附带每一步截图

Windows环境 Go安装包下载 下载后直接安装步骤按照即可。 测试 winR 输入cmd 在命令行输出go version可以看到自己的版本。 go env 查看环境变量 在桌面创建hello.go的文件 编写代码。注意&#xff0c;编码必修是UTF-8 在命令行输入路径刚刚代码所在的路径&#x…

云原生之容器编排实践-OpenEuler23.09在线安装Kubernetes与KubeSphere

背景 前几篇文章中介绍了如何将 ruoyi-cloud 项目部署到 Kubernetes 集群中&#xff0c;包括网关服务、认证服务和系统服务并且对全部服务采用 YAML 文件的方式来进行部署&#xff0c;这虽然有助于理解 K8S 组织管理资源的风格与底层机制&#xff0c;但是对于团队中不太熟悉命…

隧道FM调频广播信号泄漏电缆+天线覆盖方案

泄露电缆信号具有信号均匀&#xff0c;覆盖效果好等特点&#xff0c;但是由于造价昂贵及工程施工量大让一部分工程望而却步&#xff0c;现介绍一种性价比稍高一点的&#xff0c;泄漏电缆&#xff0b;宽带天线的方案。如图&#xff0c;去掉泄露电缆末端的匹配假负载 &#xff0c…

图书管理系统(附源码)

前言&#xff1a;前面一起和小伙伴们学习了较为完整的Java语法体系&#xff0c;那么本篇将运用这些知识连串在一起实现图书管理系统。 目录 一、总体设计 二、书籍与书架 书籍&#xff08;Book&#xff09; 书架&#xff08;Booklist&#xff09; 三、对图书的相关操作 I…

[C++][设计模式][适配器模式]详细讲解

目录 1.动机2.模式定义3.要点总结4.代码感受 1.动机 在软件系统中&#xff0c;由于应用环境的变化&#xff0c;常常需要将”一些现存的对象“放在新的环境中应用&#xff0c;但是新环境要求的接口是这些现存对象所不满足如何应对这些”迁移的变化“&#xff1f;如何既能利用现…

第一节:如何开发第一个spring boot3.x项目(自学Spring boot 3.x的第一天)

大家好&#xff0c;我是网创有方&#xff0c;从今天开始&#xff0c;我会记录每篇我自学spring boot3.x的经验。只要我不偷懒&#xff0c;学完应该很快&#xff0c;哈哈&#xff0c;更新速度尽可能快&#xff0c;想和大佬们一块讨论&#xff0c;如果需要讨论的欢迎一起评论区留…

基于MongoDB的电影影评分析

项目源码及资料 项目介绍 1、从豆瓣网爬取Top10的电影数据 爬取网址: https://movie.douban.com/top250 1.1 爬取Top10的影视信息 mv_data [] i 0 for x in soup.select(.item):i 1mv_name re.search(>([^<])<, str(x.select(.info > .hd > a > .tit…

【VMware】VMware 开启的虚拟机无法联网的解决方案

目录 &#x1f30a;1. 问题说明 &#x1f30a;2. 解决方案 &#x1f30d;2.1 查看虚拟网络编辑器 &#x1f30d;2.2 设置 vmnet &#x1f30d;2.3 设置虚拟机网络 &#x1f30d;2.4 Xshell连接虚拟机 &#x1f30a;1. 问题说明 虚拟机 ping 其他网页显示失败,比如&#…

嵌入式linux系统中动态链接库实现详解

大家好,linux系统中动态库是如何实现相互链接的?今天简单聊聊动态链接库的实现原理。 假设有这样两段代码,第一段代码定义了一个全量变量a以及函数foo,函数foo中引用了下一段代码中定义的全局变量b。 第二段代码定义了全局变量b以及main函数,同时在main函数中调用了第一个…

【数据仓库与数据挖掘】期末复习重点资料

题型&#xff1a; 选择题10个2分 填空题10空2分 简答题6个5分 大题1个&#xff08;20分10分&#xff09; 第一章 数据仓库的概念与体系结构 1.1 数据仓库的基本概念 1、元数据 元数据&#xff08;Metadata&#xff09;是描述数据仓库中数据的数据结构和构建方法的数据。…

关于Redisson分布式锁的用法

关于Redisson分布式锁的用法 Redisson是一个基于Redis的Java分布式对象和服务框架&#xff0c;它提供了多种分布式锁的实现&#xff0c;包括可重入锁、公平锁、读写锁等。Redisson实现分布式锁的核心原理主要依赖于Redis的数据结构和Redisson框架提供的高级功能。以下详细讲解…

java之动态代理

1 代理模式 代理模式提供了对目标对象额外的访问方式&#xff0c;即通过代理对象访问目标对象&#xff0c;这样可以在不修改原目标对象的前提下&#xff0c;提供额外的功能操作&#xff0c;扩展目标对象的功能。简言之&#xff0c;代理模式就是设置一个中间代理来控制访问原目标…

普通集群与镜像集群配置

一. 环境准备 关闭防火墙和selinux&#xff0c;进行时间同步 主机名系统IP服务rabbitmq-1 Rocky_linux9.4 192.168.226.22RabbitMQ&#xff0c;MySQLrabbitmq-2Rocky_linux9.4192.168.226.23RabbitMQrabbitmq-3Rocky_linux9.4192.168.226.24RabbitMQ 修改主机名#192.168.226…

机械硬盘故障分析及损坏处理(坏道屏蔽)

机械硬盘故障分析&#xff1a; 1、加电后没有声音就是电机不转&#xff0c;是电路问题&#xff0c;更换电路板解决。 2、加电后电机转&#xff0c;有连续敲击声音&#xff0c;或有异响&#xff0c;磁头损坏或机械故障。 3、加电后电机转&#xff0c;运行正常&#xff0c;BIOS无…

docker pull 镜像的时候遇到Pulling fs layer问题

最近遇到一个很奇怪的问题,docker pull 镜像的时候,总是出现Pulling fs layer问题,导致镜像拉取不成功,以前是安装好docker,正常拉取镜像都是没什么问题的,在这里记录一下这个问题的解决方法,当然,可能并不通用。 1、进入阿里云容器服务 地址:https://cr.console.aliy…

【反者道之动,弱者道之用】统计学中的哲理——回归均值 Regression to the mean

&#x1f4a1;&#x1f4a1;在统计学中&#xff0c;回归均值(Regression toward the Mean/Regression to the Mean) 指的是如果变量在其第一次测量时是极端的&#xff0c;则在第二次测量时会趋向于接近平均值的现象。   在金融学中&#xff0c; 回归均值是指股票价格无论高于…

【计算机组成原理实验】——运算器组成实验

计组TEC4实验——运算器组成实验 1. 实验目的 (1&#xff09;掌握算术逻辑运算加、减、乘、与的工作原理。 (2) 熟悉简单运算器的数据传送通路。 (3) 验证实验台运算器的8位加、减、与、直通功能。 (4) 验证实验台的4位乘4位功能。 (5) 按给定数据&#xff0c;完成几种指…

JS乌龟吃鸡游戏

代码&#xff1a; <!DOCTYPE html> <html lang"en"> <head><meta charset"UTF-8"><title>乌龟游戏</title><script type"text/javascript">function move(obj){//乌龟图片高度var wuGui_height 67;…