线性回归
import numpy as np
import pandas as pd
from sklearn.linear_model import LinearRegression
from matplotlib import pyplot as plt
def get_data():
x=np.random.rand(10,1)*100
z=np.random.randn(10,1)*10
y=x*2+z
print(x)
print(z)
print(y)
print(x.shape,y.shape)
return x,y
def linear_model_main(X_parameter, Y_parameter, predict_square_feet):
regr = LinearRegression()
regr.fit(X_parameter, Y_parameter)
predict_outcome = regr.predict(predict_square_feet)
predictions = {}
predictions['intercept'] = regr.intercept_
predictions['coefficient'] = regr.coef_
predictions['predict_value'] = predict_outcome
return predictions
def show_linear_line(X_parameter, Y_parameter):
regr = LinearRegression()
regr.fit(X_parameter, Y_parameter)
plt.scatter(X_parameter, Y_parameter, color='blue')
predicts=regr.predict(X_parameter)
plt.plot(X_parameter,predicts, color='red', linewidth=4)
print('========================')
print(predicts)
plt.title('Predict the house price')
plt.xlabel('square feet')
plt.ylabel('price')
plt.show()
if __name__ == '__main__':
x,y=get_data()
r=linear_model_main(x,y, 6)
show_linear_line(x,y)