怎么更改上面的代码
时间: 2024-02-28 10:54:42 浏览: 59
如果你想只使用特征变量进行建模,可以参考如下代码:
```python
import numpy as np
import pylab as pl
import pandas as pd
from sklearn.linear_model import Ridge
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split
X2=[]
X3=[]
X4=[]
X5=[]
X6=[]
X7=[]
df=pd.read_excel('C:/Users/86147/OneDrive/文档/777.xlsx',header=0,usecols=(3,))
X2=df.values.tolist()
x2=[]
for i in X2:
if X2.index(i)<=2927: #两个单元楼的分隔数
x2.append(i)
df=pd.read_excel('C:/Users/86147/OneDrive/文档/777.xlsx',header=0,usecols=(4,))
X3=df.values.tolist()
x3=[]
for i in X3:
if X3.index(i)<=2927:
x3.append(i)
df=pd.read_excel('C:/Users/86147/OneDrive/文档/777.xlsx',header=0,usecols=(5,))
X4=df.values.tolist()
x4=[]
for i in X4:
if X4.index(i)<=2927:
x4.append(i)
df=pd.read_excel('C:/Users/86147/OneDrive/文档/777.xlsx',header=0,usecols=(6,))
X5=df.values.tolist()
x5=[]
for i in X5:
if X5.index(i)<=2927:
x5.append(i)
df=pd.read_excel('C:/Users/86147/OneDrive/文档/777.xlsx',header=0,usecols=(7,))
X6=df.values.tolist()
x6=[]
for i in X6:
if X6.index(i)<=2927:
x6.append(i)
df=pd.read_excel('C:/Users/86147/OneDrive/文档/777.xlsx',header=0,usecols=(8,))
X7=df.values.tolist()
x7=[]
for i in X7:
if X7.index(i)<=2927:
x7.append(i)
np.random.seed(42)
q=np.array(X2[:2922])
w=np.array(x3[:2922])
e=np.array(x4[:2922])
r=np.array(x5[:2922])
t=np.array(x6[:2922])
p=np.array(x7[:2922])
eps=np.random.normal(0,0.05,152)
X=np.c_[q,w,e,r,t,p]
beta=[0.1,0.15,0.2,0.5,0.33,0.45]
y=np.dot(X,beta)
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
alpha = 0.1 # 设置岭回归的惩罚参数
ridge = Ridge(alpha=alpha)
ridge.fit(X_train, y_train)
y_pred = ridge.predict(X_test)
mse = mean_squared_error(y_test, y_pred)
print('MSE:', mse)
coef = ridge.coef_ # 计算岭回归的系数
intercept = ridge.intercept_ # 计算岭回归的截距
print('Coefficients:', coef)
print('Intercept:', intercept)
```
这个代码的主要区别在于,只使用了x2到x7这6个特征变量,而没有使用x1变量。在读取Excel文件时也只读取了这6个变量的数据,并且在合并X的时候只使用了这6个变量的数据。
阅读全文