以下是重构后的代码:
```python
import re
import numpy
from sklearn import linear_model
from matplotlib import pyplot as plt
# 导入数据
fn = open("C:/Users/***/Desktop/Python数据分析与数据化运营/chapter1/data.txt")
all_data = fn.readlines()
fn.close()
# 数据预处理
x = []
y = []
for single_data in all_data:
temp_data = re.split("\t|
", single_data)
x.append(float(temp_data[0]))
y.append(float(temp_data[1]))
x = numpy.array(x).reshape([100, 1])
y = numpy.array(y).reshape([100, 1])
# 数据分析
plt.scatter(x, y)
plt.show()
# 数据建模
model = linear_model.LinearRegression()
model.fit(x, y)
# 模型评估
model_coef = model.coef_ # 获取模型自变量系数并赋值给model_coef
model_intercept = model.intercept_ # 获取模型的截距并赋值给model_intercept
r2 = model.score(x, y) # 回归方程 y = model_coef*x + model_intercept
print("R2:", r2)
# 销售预测
new_x = [84610]
pre_y = model.predict(new_x)
print("预测销售额:", pre_y[0])
```