python 数据科学 - 【回归分析】 ☞ 线性回归(1)
发布日期:2021-06-30 19:51:18 浏览次数:3 分类:技术文章

本文共 1904 字,大约阅读时间需要 6 分钟。

基本线性回归、多次线性回归、多元线性回归:

from sklearn.preprocessing import PolynomialFeaturesfrom sklearn.linear_model import LinearRegression'''高次线性回归'''poly_reg = PolynomialFeatures(degree=2)X_ = poly_reg.fit_transform(X)regr = LinearRegression()regr.fit(X_, Y)X2 = X.sort_values(['year'])X2_ = poly_reg.fit_transform(X2)plt.scatter(X, Y, color='black')# plt.plot(X, regr.predict(X_), color='blue', linewidth=3)plt.plot(X2, regr.predict(X2_), color='blue', linewidth=3)

df = pd.read_csv('Data/house-prices.csv')# dummy variablehouse = pd.concat([df, pd.get_dummies(df['Brick']), pd.get_dummies(df['Neighborhood'])], axis=1)# multicollinearitydel house['No']del house['West']del house['Brick']del house['Neighborhood']del house['Home']house.head()

实例(房天下-上海-房地产数据):

import pandas as pddf = pd.read_excel('Data/house_price_regression.xlsx')df.head()

df['age'] = df['age'].map(lambda e : 2017 - int(e.strip().strip('建筑年代:')))df[['room', 'l_room']] = df['layout'].str.extract('(\d+)室(\d+)厅')df['total_floor'] = df['floor_info'].str.extract('共(\d+)层')df['floor'] = df['floor_info'].str.extract('^(.)层')df['direction'] = df['direction'].map(lambda e : e.strip())

df = pd.concat([df, pd.get_dummies(df['direction']), pd.get_dummies(df['floor'])], axis=1)df.head()

% pylab inlinedf[['price', 'area']].plot(kind='scatter', x='area', y='price', figsize=[15, 5])

X = df[['area']]Y = df['price']from sklearn.linear_model import LinearRegressionregr = LinearRegression()regr.fit(X, Y)print('Coefficient:', regr.coef_)print('Intercept:', regr.intercept_)regr.predict(65)

plt.scatter(X, Y, color='blue')plt.plot(X, regr.predict(X), linewidth=3, color='red')plt.xlabel('area')plt.ylabel('price')

X = df[['age', 'area', 'room', 'l_room', 'total_floor', '东南向', '东向','南北向', '南向', '西向', '中', '低']]Y = df['price']from sklearn.linear_model import LinearRegressionregr = LinearRegression()regr.fit(X, Y)regr.predict([19,65,2,1,6,0,0,1,0,0,1,0])

转载地址:https://lipenglin.blog.csdn.net/article/details/78045623 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:python 数据科学 - 【回归分析】 ☞ 线性回归(2)
下一篇:python - 抓取汇率数据分析美元和欧元对RMB的变化曲线

发表评论

最新留言

网站不错 人气很旺了 加油
[***.192.178.218]2024年04月22日 02时39分58秒