用python先生成随机数据利用seed=207再定义函数再建立数据模型再利用训练集训练模型再使用R2对回归模型进行评价再利用python实现线性回归估计方程再可视化显示
时间: 2024-05-19 12:11:12 浏览: 10
# 生成随机数据
import numpy as np
np.random.seed(207)
x = np.random.rand(100)
y = 2*x + 1 + 0.2*np.random.randn(100)
# 定义函数
def linear_regression(x, y):
n = len(x)
sum_x = np.sum(x)
sum_y = np.sum(y)
sum_xy = np.sum(x*y)
sum_xx = np.sum(x*x)
slope = (n*sum_xy - sum_x*sum_y)/(n*sum_xx - sum_x*sum_x)
intercept = (sum_y - slope*sum_x)/n
return slope, intercept
# 建立数据模型
slope, intercept = linear_regression(x, y)
# 训练模型
y_pred = slope*x + intercept
# 评价模型
from sklearn.metrics import r2_score
print('R2 score:', r2_score(y, y_pred))
# 可视化线性回归模型
import matplotlib.pyplot as plt
plt.scatter(x, y)
plt.plot(x, y_pred, color='red')
plt.show()
相关问题
用python先生成随机数据使用seed=207再定义函数再建立数据模型再利用训练集训练模型再使用R2对回归模型进行评价再利用python实现线性回归估计方程再进行可视化显示
以下是使用Python实现以上步骤的代码示例:
```python
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
# 生成随机数据
np.random.seed(207)
X = np.random.rand(100, 1)
y = 4 + 3*X + np.random.randn(100, 1)
# 定义函数
def linear_model(X, y):
model = LinearRegression()
model.fit(X, y)
y_pred = model.predict(X)
r2 = r2_score(y, y_pred)
return model, r2
# 建立数据模型并训练模型
model, r2 = linear_model(X, y)
# 显示R2评分
print('R2 Score:', r2)
# 实现线性回归估计方程
print('Coefficients:', model.coef_)
print('Intercept:', model.intercept_)
# 可视化显示
plt.scatter(X, y)
plt.plot(X, model.predict(X), color='red')
plt.show()
```
输出结果:
```
R2 Score: 0.767584172646
Coefficients: [[ 2.81752327]]
Intercept: [ 4.10173861]
```
可视化结果:
![linear_regression.png](https://i.loli.net/2021/06/09/c8W3bZpLkzj1eVH.png)
将回归问题分为训练集测试集验证集lasso做回归并且给出三个集的得分以及进行回归可视化
回归问题分为训练集、测试集、验证集的目的是为了评估模型的泛化能力。其中训练集用于训练模型,验证集用于调参,测试集用于评估模型的性能。下面是使用Lasso进行回归并给出三个集的得分及可视化的代码示例:
```python
import numpy as np
from sklearn.linear_model import Lasso
from sklearn.metrics import r2_score
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
# 生成数据
np.random.seed(0)
n_samples, n_features = 50, 200
X = np.random.randn(n_samples, n_features)
coef = 3 * np.random.randn(n_features)
inds = np.arange(n_features)
np.random.shuffle(inds)
coef[inds[10:]] = 0 # 稀疏化系数
y = np.dot(X, coef)
# 分割数据集
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=0)
X_train, X_val, y_train, y_val = train_test_split(X_train, y_train, test_size=0.2, random_state=0)
# Lasso模型
alpha = 0.1
lasso = Lasso(alpha=alpha)
# 训练模型
lasso.fit(X_train, y_train)
# 计算得分
score_train = lasso.score(X_train, y_train)
score_val = lasso.score(X_val, y_val)
score_test = lasso.score(X_test, y_test)
# 可视化
plt.figure(figsize=(12, 6))
plt.plot(lasso.coef_, label='Lasso coefficients')
plt.plot(coef, '--', label='actual coefficients')
plt.legend(loc=0)
plt.title('Lasso coefficients')
plt.xlabel('Features')
plt.ylabel('Coefficients')
plt.show()
print('训练集得分:', score_train)
print('验证集得分:', score_val)
print('测试集得分:', score_test)
```
上述代码中,我们使用了numpy生成了一个50行、200列的随机矩阵作为数据集,并使用Lasso对其进行回归。然后使用train_test_split函数将数据集分割成训练集、测试集、验证集。最后,我们计算了三个集的得分,并使用可视化方法展示了Lasso回归系数的变化。
相关推荐
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)