2021年MathorCup数学建模挑战赛A二手车估价问题数学建模

紧接上文回归模型我给个线性回归的和SVM回归的,大家可以比较看看
优缺点就不说了,像废话一样,直接上程序。

基于二手车估价的线性回归

import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.linear_model import SGDRegressor
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error

data = pd.read_csv(open('C:/Users/Tracy/Desktop/300/2021/附件/附件1.csv'))
data.fillna(0)
data1 = np.array(data)
X = data1[:, 1:-2]
y = data.iloc[:, 37]
#print("Giving dataset has {} data points with {} variables each.".format(*data.shape))
#print(y)
minimum_price = np.min(y)
maximum_price = np.max(y)
mean_price = np.mean(y)
median_price = np.median(y)
std_price = np.std(y)
# 分析回归目标值的差异。
print("The max target price is", np.max(y))
print("The min target price is", minimum_price)
print("The average price value is", mean_price)
#print("The median price value is", median_price)
#print("The std_price price value is", std_price)

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
print(X_train.shape)
print(X_test.shape)
print(y_train.shape)
print(y_test.shape)

# 分别初始化对特征和目标值的标准化器。
ss_X = StandardScaler()
ss_y = StandardScaler()

# 分别对训练和测试数据的特征以及目标值进行标准化处理。
X_train = ss_X.fit_transform(X_train)
X_test = ss_X.transform(X_test)
y_train = ss_y.fit_transform(y_train)
y_test = ss_y.transform(y_test)
st = ss_y.transform(y_test)

接下来的使用默认配置初始化线性回归器LinearRegression,使用训练数据进行参数估计,对测试数据进行回归预测,使用LinearRegression模型自带的评估模块,并输出评估结果;使用r2_score模块,并输出评估结果…
基于二手车估价线性回归的程序想要全部见最下方

基于二手车估价的SVM回归算法

import numpy as np
import pandas as pd
from sklearn import svm,datasets
from sklearn.multiclass import OneVsRestClassifier
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split

clf = OneVsRestClassifier(svm.SVC(kernel='linear'))
df = pd.read_table('C:/Users/Tracy/Desktop/300/2021年MathorCup大数据竞赛赛道A/附件/附件1.txt', 'r', delimiter='\\t', header = None)
x = df.iloc[:5000,:36]
x1 = np.array(df.iloc[:5000,:36]).astype(str)

tdf = pd.read_table('C:/Users/Tracy/Desktop/300/2021年MathorCup大数据竞赛赛道A/附件/附件2.txt', 'r', delimiter='\\t', header = None)
y = tdf.iloc[:]
y1 = np.array(tdf.iloc[:5000,:36]).astype(str)

x_train, x1_test, y_train, y1_test = train_test_split(x, y, test_size=3)

clf.fit(x_train, y_train)

y_pred = clf.predict(x1_test)

rf = pd.DataFrame(list(zip(y_pred, y1_test)), columns=['predicted', 'actual'])
rf['correct'] = rf.apply(lambda r:1 if r['predicted'] == r['actual'] else 0, axis=1)
print(rf)

你可能感兴趣的:(数学建模,笔记,数据分析,回归,支持向量机,机器学习)