首页 > 解决方案 > python使用sklearn进行线性回归

问题描述

我尝试用 python 做线性回归

例如

from sklearn.linear_model import LinearRegression
import matplotlib.pyplot as plt

x = [[6, 2], [8, 1], [10, 0], [14, 2], [18, 0]]
y = [[7], [9], [13], [17.5], [18]]
model = LinearRegression()
model.fit(x, y)
x_test = [[8, 2]]

现在 example_data 像:

inches city  Pizza_Price
  5       A        10
  6       B        12

英寸是一个明确的数字,但面积不是。

如何将城市转换为数字进行计算?</p>

如何将城市等参数分类为数字进行计算?</p>

标签: pythonlinear-regression

解决方案


代码:

from sklearn import preprocessing

le = preprocessing.LabelEncoder()
cities = ['A','B','C','B'] # for example

le.fit(cities)
cities = le.transform(cities)
print(cities)

输出:

[0, 1, 2, 1]

标签编码器说明


推荐阅读