标签:fun 选择 -o import imp html random param com
网址:https://www.cnblogs.com/pinard/p/6023000.html
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets,linear_model
data = pd.read_excel("F:\data\CCPP\Folds5x2_pp.xlsx");
x = data[[‘AT‘, ‘V‘, ‘AP‘, ‘RH‘]]
y = data[[‘PE‘]]
from sklearn.cross_validation import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x, y, random_state=1)#可以通过test_size来设置划分比列
from sklearn.linear_model import LinearRegression
linreg = LinearRegression()#线性回归函数
linreg.fit(x_train,y_train)
print("linreg.intercept_",linreg.intercept_,"linreg.coef_",linreg.coef_)
y_pred = linreg.predict(x_test)
from sklearn import metrics
print("MSE:",metrics.mean_squared_error(y_test, y_pred))
print("RMSE:",np.sqrt(metrics.mean_squared_error(y_test, y_pred)))
from sklearn.model_selection import cross_val_predict
predicted = cross_val_predict(linreg, x, y, cv=100)
print("predicted:",predicted.shape)
print("MSE:",metrics.mean_squared_error(y, predicted))
print ("RMSE:",np.sqrt(metrics.mean_squared_error(y, predicted)))
fig, ax = plt.subplots()
ax.scatter(y, predicted)
ax.plot([y.min(), y.max()], [y.min(), y.max()], ‘k--‘, lw=4)
ax.set_xlabel(‘Measured‘)
ax.set_ylabel(‘Predicted‘)
plt.show()
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
from sklearn import datasets,linear_model
from sklearn import metrics
data = pd.read_excel("F:\data\CCPP\Folds5x2_pp.xlsx");
x = data[[‘AT‘, ‘V‘, ‘AP‘, ‘RH‘]]
y = data[[‘PE‘]]
from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test = train_test_split(x, y, random_state=1)#可以通过test_size来设置划分比列
n_alphas = 200
alphas = np.logspace(-10,-2,n_alphas)
print("alphas:",alphas)
clf = linear_model.Ridge(fit_intercept=False)
coefs = []
for a in alphas:
#设置本次循环的超参数
clf.set_params(alpha=a)
#针对每个alpha做ridge回归
clf.fit(x_train, y_train)
y_predict = clf.predict(x_test)
error = metrics.mean_squared_error(y_predict,y_test)#计算方差
print("error:",error)
# 把每一个超参数alpha对应的theta存下来
coefs.append(clf.coef_)
print("coefs:",coefs)
from sklearn import metrics
标签:fun 选择 -o import imp html random param com
原文地址:https://www.cnblogs.com/131415-520/p/11741303.html