读取数据
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')
#reduce_mem_usage 函数通过调整数据类型,减少数据在内存中占用的空间
def reduce_mem_usage(df):
""" iterate through all the columns of a dataframe and modify the data type
to reduce memory usage.
"""
start_mem = df.memory_usage().sum()
print('Memory usage of dataframe is {:.2f} MB'.format(start_mem))
for col in df.columns:
col_type = df[col].dtype
if col_type != object:
c_min = df[col].min()
c_max = df[col].max()
if str(col_type)[:3] == 'int':
if c_min > np.iinfo(np.int8).min and c_max < np.iinfo(np.int8).max:
df[col] = df[col].astype(np.int8)
elif c_min > np.iinfo(np.int16).min and c_max < np.iinfo(np.int16).max:
df[col] = df[col].astype(np.int16)
elif c_min > np.iinfo(np.int32).min and c_max < np.iinfo(np.int32).max:
df[col] = df[col].astype(np.int32)
elif c_min > np.iinfo(np.int64).min and c_max < np.iinfo(np.int64).max:
df[col] = df[col].astype(np.int64)
else:
if c_min > np.finfo(np.float16).min and c_max < np.finfo(np.float16).max:
df[col] = df[col].astype(np.float16)
elif c_min > np.finfo(np.float32).min and c_max < np.finfo(np.float32).max:
df[col] = df[col].astype(np.float32)
else:
df[col] = df[col].astype(np.float64)
else:
df[col] = df[col].astype('category')
end_mem = df.memory_usage().sum()
print('Memory usage after optimization is: {:.2f} MB'.format(end_mem))
print('Decreased by {:.1f}%'.format(100 * (start_mem - end_mem) / start_mem))
return df
sample_feature = reduce_mem_usage(pd.read_csv('data_for_tree.csv'))
Memory usage of dataframe is 62099672.00 MB
Memory usage after optimization is: 16520303.00 MB
Decreased by 73.4%
continuous_feature_names = [x for x in sample_feature.columns if x not in ['price','brand','model','brand']]
线性回归 & 五折交叉验证 & 模拟真实情况
sample_feature = sample_feature.dropna().replace('-', 0).reset_index(drop=True)
sample_feature['notRepairedDamage'] = sample_feature['notRepairedDamage'].astype(np.float32)
train = sample_feature[continuous_feature_names + ['price']]
train_X = train[continuous_feature_names]
train_y = train['price']
简单建模
from sklearn.linear_model import LinearRegression
model = LinearRegression(normalize=True)
model = model.fit(train_X, train_y)
查看训练的线性回归模型的截距(intercept)与权重(coef)
'intercept:'+ str(model.intercept_)
sorted(dict(zip(continuous_feature_names, model.coef_)).items(), key=lambda x:x[1], reverse=True)
[('v_6', 3342612.384537345),
('v_8', 684205.534533214),
('v_9', 178967.94192530424),
('v_7', 35223.07319016895),
('v_5', 21917.550249749802),
('v_3', 12782.03250792227),
('v_12', 11654.925634146672),
('v_13', 9884.194615297649),
('v_11', 5519.182176035517),
('v_10', 3765.6101415594258),
('gearbox', 900.3205339198406),
('fuelType', 353.5206495542567),
('bodyType', 186.51797317460046),
('city', 45.17354204168846),
('power', 31.163045441455335),
('brand_price_median', 0.535967111869784),
('brand_price_std', 0.4346788365040235),
('brand_amount', 0.15308295553300566),
('brand_price_max', 0.003891831020467389),
('seller', -1.2684613466262817e-06),
('offerType', -4.759058356285095e-06),
('brand_price_sum', -2.2430642281682917e-05),
('name', -0.00042591632723759166),
('used_time', -0.012574429533889028),
('brand_price_average', -0.414105722833381),