多变量线性回归-代码实现
import numpy as np
from sklearn import datasets
dataset = datasets.load_boston()
print(dataset.get('feature_names'))
# 使用第6-8列feature,即AGE(1940年以前建成的自住单位的比例),
# DIS(距离五个波士顿就业中心的加权距离),RAD(距离高速公路的便利指数)
X = dataset.data[:, 5:8]
# 为X增加一列全为1,来求偏置项
X = np.column_stack((X, np.ones(len(X))))
y = dataset.target
# 划分训练集和测试集
X_train = X[:-20]
X_test = X[-20:]
y_train = y[:-20]
y_test = y[-20:]
X_train = np.mat(X_train)
y_train = np.mat(y_train).T
xTx = X_train.T * X_train
w = 0
if np.linalg.det(xTx) == 0.0:
print('xTx不可逆')
else:
w = np.ravel(xTx.I * (X_train.T * y_train))
coef_ = w[:-1]
intercept_ = w[-1]
# 去掉添加的那一列1
X_train = X_train[:, 0:3]
X_test = X_test[:, 0:3]
y_test_pred = coef_[0] * X_test[:, 0] + coef_[1] * X_test[:, 1] + coef_[2] * X_test[:, 2] + intercept_
# 矩阵转回数组
X_train = np.ravel(X_train).reshape(-1, 3)
y_train = np.ravel(y_train)
print('Coefficients: ', coef_)
print('Intercept:', intercept_)
print('the model is: y = ', coef_, '* X + ', intercept_)
# 均方误差
print("Mean squared error: %.2f" % np.average((y_test - y_test_pred) ** 2))
[‘CRIM’ ‘ZN’ ‘INDUS’ ‘CHAS’ ‘NOX’ ‘RM’ ‘AGE’ ‘DIS’ ‘RAD’ ‘TAX’ ‘PTRATIO’
‘B’ ‘LSTAT’]
Coefficients: [ 8.46020754 -0.09742681 -0.48029222]
Intercept: -22.08394419276602
the model is: y = [ 8.46020754 -0.09742681 -0.48029222] * X + -22.08394419276602
Mean squared error: 10.33
数据标准化
from sklearn.datasets import load_iris
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=0)
print("Size of training set: %d size of test set: %d" % (X_train.shape[0], X_test.shape[0])
house_dataset = datasets.load_boston()
house_data = house_dataset.data;
house_price = house_dataset.target;
x_train,x_test,y_train,y_test=train_test_split(house_data,house_price,test_size=0.2);
#StandardScaler是数据放缩的其中一种方案,即f(x)=(x-平均值)/标准差
scaler = StandardScaler();
#在数据模型建立时,只能使用训练组数据,因此使用训练组数据的标准差进行标准化
scaler.fit(x_train);
#对训练组和测试组的输入数据进行标准化,即f(训练组或测试组数据)=(训练组或测试组数据-训练组或测试组平均值)/训练组标准差
x_train = scaler.transform(x_train);
x_test = scaler.transform(x_test);
网格搜索调参
from sklearn.datasets import load_iris
from sklearn.svm import SVC
from sklearn.model_selection import train_test_split
iris = load_iris()
X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, random_state=0)
print("Size of training set: %d size of test set: %d" % (X_train.shape[0], X_test.shape[0]))
best_score = 0
for gamma in [0.001, 0.01, 0.1, 1, 10, 100]:
for C in [0.001, 0.01, 0.1, 1, 10, 100]:
# for each combination of parameters
# train an SVC
svm = SVC(gamma=gamma, C=C)
svm.fit(X_train, y_train)
# evaluate the SVC on the test set
score = svm.score(X_test, y_test)
# if we got a better score, store the score and parameters
if score > best_score:
best_score = score
best_parameters = {'C': C, 'gamma': gamma}
print("best score: ", best_score)
print("best parameters: ", best_parameters)