Python 分析二手汽车售价

Python 分析二手汽车售价

分析二手汽车的售价对于买家和卖家都是至关重要的,可以通过使用Python轻松地做出有根据的决策。通过利用Python的数据分析和可视化功能,可以从可用的数据集中获得有价值的见解。

本文探讨了数据预处理、清理以及使用各种图表分析售价的过程。此外,它还介绍了使用线性回归模型来预测售价。凭借Python强大的库,如pandas、matplotlib、seaborn和scikit-learn,这个分析提供了一个全面的方法来理解影响二手车价格的因素,并进行准确的价格预测。

如何使用Python分析二手车的售价

按照以下步骤使用Python分析二手车的售价:

步骤1:导入重要的库

import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error

步骤2:读取数据集并将其存储在pandas的数据框中

data = pd.read_csv('C:/Users/Tutorialspoint/Documents/autos.csv', encoding='latin1')

步骤3:检查数据集的结构

data.info()
print(data.head())

输出

dateCrawled  seller offerType  price vehicleType  \
3 2016-03-17 16:54:04  privat   Angebot   1500  kleinwagen   
4 2016-03-31 17:25:20  privat   Angebot   3600  kleinwagen   
5 2016-04-04 17:36:23  privat   Angebot    650   limousine   
6 2016-04-01 20:48:51  privat   Angebot   2200      cabrio   
7 2016-03-21 18:54:38  privat   Angebot      0   limousine   

   yearOfRegistration  gearbox  powerPS    model  kilometer  \
3                2001  manuell       75     golf     150000   
4                2008  manuell       69    fabia      90000   
5                1995  manuell      102      3er     150000   
6                2004  manuell      109  2_reihe     150000   
7                1980  manuell       50   andere      40000   

   monthOfRegistration fuelType       brand notRepairedDamage dateCreated  \
3                    6   benzin  volkswagen              nein  2016-03-17   
4                    7   diesel       skoda              nein  2016-03-31   
5                   10   benzin         bmw                ja  2016-04-04   
6                    8   benzin     peugeot              nein  2016-04-01   
7                    7   benzin  volkswagen              nein  2016-03-21   

              lastSeen  
3  2016-03-17 17:40:17  
4  2016-04-06 10:17:21  
5  2016-04-06 19:17:07  
6  2016-04-05 18:18:39  
7  2016-03-25 16:47:58

步骤4:处理缺失值

data = data.dropna()

步骤5:仅在必要时转换数据类型

data['dateCrawled'] = pd.to_datetime(data['dateCrawled'])
data['dateCreated'] = pd.to_datetime(data['dateCreated'])

步骤6:数据清洗

# Remove irrelevant columns
columns_to_drop = ['name', 'abtest', 'nrOfPictures', 'postalCode']
data = data.drop(columns=columns_to_drop)

步骤7:使用图表分析销售价格

示例1:销售价格直方图

plt.figure(figsize=(10, 6))
sns.histplot(data['price'], bins=20, kde=True)
plt.xlabel('Price')
plt.ylabel('Count')
plt.title('Histogram of Selling Price')
plt.show()

输出

Python 分析二手汽车售价

示例2:按车辆类型绘制箱线图的销售价格

plt.figure(figsize=(10, 6))
sns.boxplot(x='vehicleType', y='price', data=data)
plt.xlabel('Vehicle Type')
plt.ylabel('Price')
plt.title('Boxplot of Selling Price by Vehicle Type')
plt.show()

输出

Python 分析二手汽车售价

示例3:注册年份与价格的散点图

plt.figure(figsize=(10, 6))
sns.scatterplot(x='yearOfRegistration', y='price', data=data)
plt.xlabel('Year of Registration')
plt.ylabel('Price')
plt.title('Scatter plot of Year of Registration vs. Price')
plt.show()

输出

Python 分析二手汽车售价

步骤8:使用线性回归预测销售价格

选择相关特征和目标变量

features = ['yearOfRegistration', 'powerPS', 'kilometer']
target = 'price'
X = data[features]
y = data[target]

将数据分割为训练集和测试集:

分割数据为训练集和测试集:

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

初始化并训练线性回归模型

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

输出

LinearRegression

步骤9:对测试集进行预测并打印预测的销售价格

# Make predictions on the test set
y_pred = model.predict(X_test)

# Print the predicted selling price
print("Predicted Selling Price:")
for price in y_pred:
   print(price)

输出

Predicted Selling Price:
4697.820983235375
4882.88628493459
2407.3556394173065
4264.297985512414
5801.285403149028
6486.864555639331
18844.05037380848
3615.3753698624205
15154.480417441286
7511.02954521589
5815.107292202709
14360.747495675983
3868.0368050450925
6433.695591624826
3019.621718226932
……………………………………
3723.5291391374194

步骤10:使用均方误差(MSE)评估模型

mse = mean_squared_error(y_test, y_pred)
print(f"Mean Squared Error: {mse}")

输出

Mean Squared Error: 3838157266.6337757

结论

总之,Python的多功能性和强大的库使其成为分析二手车销售价格的理想工具。通过数据预处理、清洗和可视化,可以获得有价值的洞察。此外,使用机器学习算法进行预测建模可以实现准确的价格预测。这种分析使买家和卖家能够在二手车市场上做出明智的决策。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程