线性回归模型表示:
![](https://img.haomeiwen.com/i1780773/f01ecd4c5db4ee30.png)
数据集data.txt内容如下:
![](https://img.haomeiwen.com/i1780773/2062e5e0d304d9f2.png)
第1、2列是输入x1, x2,第3列是输出y, y=w1x1+w2x2+b
线性回归模型 - sklearn实现
'''
线性回归模型 - sklearn实现
'''
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error, r2_score
# 载入数据
data = np.loadtxt('data.txt',delimiter=',',dtype=np.float64)
# 分离训练集和测试集
X=data[:,0:-1]
y=data[:,-1]
X_train, X_test, y_train, y_test = train_test_split (X,y,test_size=0.2, random_state=100)
#模型拟合
model = LinearRegression()
model.fit(X_train,y_train)
#模型预测
y_pred = model.predict(X_test)
#输出结果
print('Intercept: ',model.intercept_) #截距
print('Coefficients: ',model.coef_) #权重
print('Mean Squared Error:',mean_squared_error(y_test, y_pred))
print('Variance Score ',r2_score(y_test, y_pred))
# 可视化结果
fig=plt.figure()
ax = fig.add_subplot(111,projection='3d')
ax.scatter(X_train[:,0],X_train[:,1],y_train,color='black')
ax.scatter(X_test[:,0],X_test[:,1],y_test,color='blue')
ax.scatter(X_test[:,0],X_test[:,1],y_pred,color='red')
x1_fit, x2_fit = np.meshgrid(np.arange(X[:,0].min(), X[:,0].max(), (X[:,0].max()-X[:,0].min())/100),np.arange(X[:,1].min(), X[:,1].max(), (X[:,1].max()-X[:,1].min())/100) )
x_fit= np.column_stack((x1_fit.flatten(),x2_fit.flatten()))
ax.plot_surface(x1_fit,
x2_fit,
model.predict(x_fit).reshape(100,100),
alpha=.5)
plt.show()
输出结果:
![](https://img.haomeiwen.com/i1780773/e630b202e99e11b0.png)
线性回归模型 - Tensorflow实现
'''
线性回归模型 - tensorflow实现
'''
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
# 载入数据
data = np.loadtxt('data.txt',delimiter=',')
# 分离训练集和测试集
X=data[:,0:-1].astype(np.float32)
y=data[:,-1].astype(np.float32)
scaler = preprocessing.StandardScaler().fit(X)
print(scaler.mean_, scaler.scale_)
X= scaler.transform(X)
print(X)
X_train, X_test, y_train, y_test = train_test_split (X,y,test_size=0.2, random_state=100)
#模型拟合
with tf.name_scope('Weight'):
W=tf.Variable(tf.random_normal([2,1]),name='W')
tf.summary.histogram('weights',W)
with tf.name_scope('biase'):
b=tf.Variable(tf.zeros([1,1])+0.1,name='b')
tf.summary.histogram('biase',b)
with tf.name_scope('Wx_plus_b'):
Wx_plus_b=tf.matmul(X_train,W)+b
tf.summary.histogram('Wx_plus_b',Wx_plus_b)
with tf.name_scope('loss'):
loss=tf.reduce_mean(tf.square(y_train.reshape(-1,1)-Wx_plus_b))
tf.summary.scalar('loss',loss)
with tf.name_scope('train'):
train=tf.train.GradientDescentOptimizer(0.5).minimize(loss)
sess=tf.Session()
merged = tf.summary.merge_all() # pack summary
writer = tf.summary.FileWriter('logs/',sess.graph)
init=tf.global_variables_initializer()
sess.run(init)
for i in range(1000):
sess.run(train)
print(sess.run(loss))
#print(sess.run(Wx_plus_b))
#print(y_train.reshape(-1,1))
print(sess.run(W))
print(sess.run(b))
result = sess.run(merged)
writer.add_summary(result,i)
print ("Coefficients of tensorflow (raw input): K=%s, b=%s"% (sess.run(W).flatten() / scaler.scale_, sess.run(b).flatten() - np.dot(scaler.mean_ / scaler.scale_, sess.run(W))))
输出结果:
![](https://img.haomeiwen.com/i1780773/7dc8db45e74c75e3.png)
线性回归模型 - keras实现
'''
线性回归模型 - keras实现
'''
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from keras.models import Sequential
from keras.layers import Dense
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
# 载入数据
data = np.loadtxt('data.txt',delimiter=',')
X=data[:,0:-1].astype(np.float32)
y=data[:,-1].astype(np.float32)
#归一化
scaler = preprocessing.StandardScaler().fit(X)
print(scaler.mean_, scaler.scale_)
X= scaler.transform(X)
# 分离训练集和测试集
X_train, X_test, y_train, y_test = train_test_split (X,y,test_size=0.2, random_state=100)
#可视化
fig=plt.figure()
ax=fig.add_subplot(111,projection='3d')
ax.scatter(X_train[:,0],X_train[:,1],y_train,color='black')
ax.scatter(X_test[:,0],X_test[:,1],y_test,color='blue')
model=Sequential()
model.add(Dense( input_dim=2,output_dim=1))
model.compile(loss='mse', optimizer='sgd')
print('Training...')
for step in range(1000):
cost=model.train_on_batch(X_train, y_train)
if step%100==0:
print('train cost:',cost)
print('Testing...')
cost=model.evaluate(X_test, y_test,batch_size=10)
print('test cost:', cost)
W,b=model.layers[0].get_weights()
print('Weights=',W.flatten() / scaler.scale_,'\nbiases=',b.flatten()/ scaler.scale_)
x1_fit, x2_fit=np.meshgrid(np.arange(X[:,0].min(),X[:,0].max(),(X[:,0].max()-X[:,0].min())/100),np.arange(X[:,1].min(),X[:,1].max(),(X[:,1].max()-X[:,1].min())/100))
x_fit=np.column_stack((x1_fit.flatten(),x2_fit.flatten()))
ax.plot_surface(x1_fit,x2_fit,model.predict(x_fit).reshape(100,100),alpha=.5)
plt.show()
输出结果:
![](https://img.haomeiwen.com/i1780773/3ab15f8f426a191f.png)
网友评论