stardust-shadow

导航

pandas 常用函数

获取随机的1000行data数据。

import numpy as np
from sklearn.datasets import fetch_openml
from sklearn.linear_model import LinearRegression
data = fetch_openml(name="house_prices",as_frame=True, parser='auto')

 

X = data['data']

#筛选出数值, 填充空值
X=X.select_dtypes(exclude='object').fillna(0)
y = data['target']

index = np.arange(1460)
np.random.shuffle(index)
index

#数据拆分

test_index=index[1000:]
train_index=index[:1000]

X.loc[train_index,:]

y.loc[train_index]

X_test=X.loc[test_index,:]
y_test=y[test_index]
display(X_test,y_test)

#建模

model = LinearRegression(fit_intercept = True)
model.fit(X_train,y_train)
display(model.coef_,model.intercept_)

预测

y_ = model.predict(X_test).round(2)

 

#评估

model.score(X_test,y_test)

参考:https://www.jb51.net/article/276229.htm

posted on 2023-08-10 23:27  阡年  阅读(23)  评论(0)    收藏  举报