import pandas;
import matplotlib;
from pandas.tools.plotting import scatter_matrix;
data = pandas.read_csv("D:\\面积距离车站.csv",engine='python',encoding='utf-8')
显示文件大小
data.shape
data
#绘制多个变量两两之间的散点图:scatter_matrix()方法
font = {
'family' : 'SimHei'
}
matplotlib.rc('font', **font)
scatter_matrix(
data[["area","distance", "money"]],
figsize=(10, 10), diagonal='kde'
) #diagonal参数表示变量与变量本身之间的绘图方式,kde代表直方图
#求相关系数矩阵
data[["area", "distance", "money"]].corr()
x = data[["area", "distance"]]
y = data[["money"]]
from sklearn.linear_model import LinearRegression
#建模
lrModel = LinearRegression()
#训练模型
lrModel.fit(x, y)
#评分
R2=lrModel.score(x, y)
print("R的平方:",R2)
#预测
lrModel.predict([[10, 110],[20, 110]])
#查看参数
lrModel.coef_
#查看截距
lrModel.intercept_
结果如下:
回归方程为:y=41.51x1-0.34x2+65.32
import pandas;
import matplotlib;
from pandas.tools.plotting import scatter_matrix;
data.shape
#绘制多个变量两两之间的散点图:scatter_matrix()方法
font = {
'family' : 'SimHei'
}
matplotlib.rc('font', **font)
scatter_matrix(
data[["area","distance", "money"]],
figsize=(10, 10), diagonal='kde'
) #diagonal参数表示变量与变量本身之间的绘图方式,kde代表直方图
#求相关系数矩阵
data[["area", "distance", "money"]].corr()
x = data[["area", "distance"]]
y = data[["money"]]
from sklearn.linear_model import LinearRegression
#建模
lrModel = LinearRegression()
#训练模型
lrModel.fit(x, y)
#评分
R2=lrModel.score(x, y)
print("R的平方:",R2)
#预测
lrModel.predict([[10, 110],[20, 110]])
#查看参数
lrModel.coef_
#查看截距
lrModel.intercept_