python LinearRegression线性回归

目录

1.什么是线性回归

2.线性回归思路

3.几种回归模型评估

3.1均方误差:mse = mean_squared_error(y真实值,y预测值)

3.2均方根误差:rmse = (mean_squared_error(y真实值,y预测值))**0.5

3.3平均绝对值误差:mae = mean_absolute_error(y真实值,y预测值)

3.4r2打分:r2 = r2_score(y真实值,y预测值) 或者r2=lr.score(x真实值,y真实值)

4.单(一)元线性回归

5.多元线性回归


1.什么是线性回归

        两个变量之间存在一次方函数关系,就称它们之间存在线性关系。更通俗一点讲,如果把这两个变量分别作为点的横坐标与纵坐标,其图象是平面上的一条直线,则这两个变量之间的关系就是线性关系。

2.线性回归思路


    
    
  1. """
  2. 1.获取数据集
  3. 2.指定特征值x、目标值y(想获得什么数据,预测什么数据就设为y)
  4. 3.数据集划分:训练集(默认75%)和测试集(默认25%)
  5. 4.建立模型、拟合、预测
  6. 5.模型评估
  7. 预测结果: 出太阳 出太阳 下雨
  8. 测试集的真实结果: 阴天 出太阳 下雨
  9. 正确率为66.66%
  10. """

3.几种回归模型评估

 

3.1均方误差:mse = mean_squared_error(y真实值,y预测值)

误差越接近0,误差越小 

3.2均方根误差:rmse = (mean_squared_error(y真实值,y预测值))**0.5

 

误差越接近0,误差越小 

3.3平均绝对值误差:mae = mean_absolute_error(y真实值,y预测值)

误差越接近0,误差越小 

3.4r2打分:r2 = r2_score(y真实值,y预测值) 或者r2=lr.score(x真实值,y真实值)

分数越高表示越好 

4.单(一)元线性回归


    
    
  1. # 安装pip install scikit-learn
  2. #线性回归类
  3. from sklearn.linear_model import LinearRegression
  4. # 数据集划分
  5. from sklearn.model_selection import train_test_split
  6. #数据集
  7. from sklearn.datasets import load_iris
  8. # 单(一)元线性回归
  9. # 获取数据集
  10. iris = load_iris()
  11. print( 'iris原始数据:',iris)
  12. print( '*'* 100)
  13. # 指定特征列x,y
  14. x,y = iris.data[:, 2].reshape(- 1, 1),iris.data[:, 3].reshape(- 1, 1)
  15. print( 'x数据:',x)
  16. print( '*'* 60)
  17. print( 'y数据:',y)
  18. print( '*'* 40)
  19. """
  20. # 随机种子
  21. import random
  22. # 5就是随机种子
  23. random.seed(5)
  24. num = [random.randint(1,100)for i in range(5)]
  25. print(num)
  26. # 划分训练集和测试集,把数据源给train_test_split,系统自行分割,
  27. 0.2是测试集的比例,random_state随机种子,数字是自定义,
  28. 数字不一样评估结果可能不一样,可以调
  29. """
  30. x_train,x_test,y_train,y_test = train_test_split(x,y,test_size= 0.2,random_state= 2)
  31. # 建立模型
  32. lr = LinearRegression()
  33. #拟合,计算方程参数w和b,y=w*x+b,系统自行计算
  34. lr.fit(x_train,y_train)
  35. print( '权重w:',lr.coef_)
  36. print( '*'* 30)
  37. print( '截距b:',lr.intercept_)
  38. print( '*'* 30)
  39. # 预测
  40. y_hat = lr.predict(x_test) #模型训练出来然后用测试值去测试出来的结果
  41. print( 'y_hat预测数据:',y_hat[: 5])
  42. print( '*'* 30)
  43. print( 'y_text真实数据:',y_test[: 5]) # 真实值
  44. print( '*'* 40)
  45. # 图形模型评估
  46. import matplotlib.pyplot as plt
  47. plt.rcParams[ 'font.family']=[ 'KaiTi']
  48. plt.figure(figsize=( 10, 4))
  49. plt.plot(y_test,label= '真实值',color= 'pink',marker= 'o')
  50. plt.plot(y_hat,label= '预测值',color= 'c',marker= 'D',ls= '--')
  51. plt.legend()
  52. plt.show()
  53. # 导入回归模型评估
  54. from sklearn.metrics import mean_squared_error,mean_absolute_error,r2_score
  55. #越接近于0,误差越小
  56. # 均方误差0.03810129178169931
  57. mse = mean_squared_error(y_test,y_hat)
  58. print( '均方误差mse:',mse)
  59. print( '*'* 40)
  60. # 均方根误差0.19519552193044623
  61. rmse = mse** 0.5
  62. print( '均方根误差rmse:',rmse)
  63. print( '*'* 40)
  64. #平均绝对值误差0.1430440743648432
  65. mae = mean_absolute_error(y_test,y_hat)
  66. print( '平均绝对值误差mae:',mae)
  67. print( '*'* 40)
  68. #r2分数,打分0.9368067916048772,93.68很好了
  69. r2 = r2_score(y_test,y_hat)
  70. print( 'r2打分:',r2)
  71. # 另一种r2打分方法
  72. print( '另一种r2打分方法:',lr.score(x_test, y_test))

运行结果:


    
    
  1. iris原始数据:
  2. { 'data': array([[ 5.1, 3.5, 1.4, 0.2],
  3. [ 4.9, 3. , 1.4, 0.2],
  4. [ 4.7, 3.2, 1.3, 0.2],
  5. [ 4.6, 3.1, 1.5, 0.2],
  6. [ 5. , 3.6, 1.4, 0.2],
  7. [ 5.4, 3.9, 1.7, 0.4],
  8. [ 4.6, 3.4, 1.4, 0.3],
  9. [ 5. , 3.4, 1.5, 0.2],
  10. [ 4.4, 2.9, 1.4, 0.2],
  11. [ 4.9, 3.1, 1.5, 0.1],
  12. [ 5.4, 3.7, 1.5, 0.2],
  13. [ 4.8, 3.4, 1.6, 0.2],
  14. [ 4.8, 3. , 1.4, 0.1],
  15. [ 4.3, 3. , 1.1, 0.1],
  16. [ 5.8, 4. , 1.2, 0.2],
  17. [ 5.7, 4.4, 1.5, 0.4],
  18. [ 5.4, 3.9, 1.3, 0.4],
  19. [ 5.1, 3.5, 1.4, 0.3],
  20. [ 5.7, 3.8, 1.7, 0.3],
  21. [ 5.1, 3.8, 1.5, 0.3],
  22. [ 5.4, 3.4, 1.7, 0.2],
  23. [ 5.1, 3.7, 1.5, 0.4],
  24. [ 4.6, 3.6, 1. , 0.2],
  25. [ 5.1, 3.3, 1.7, 0.5],
  26. [ 4.8, 3.4, 1.9, 0.2],
  27. [ 5. , 3. , 1.6, 0.2],
  28. [ 5. , 3.4, 1.6, 0.4],
  29. [ 5.2, 3.5, 1.5, 0.2],
  30. [ 5.2, 3.4, 1.4, 0.2],
  31. [ 4.7, 3.2, 1.6, 0.2],
  32. [ 4.8, 3.1, 1.6, 0.2],
  33. [ 5.4, 3.4, 1.5, 0.4],
  34. [ 5.2, 4.1, 1.5, 0.1],
  35. [ 5.5, 4.2, 1.4, 0.2],
  36. [ 4.9, 3.1, 1.5, 0.2],
  37. [ 5. , 3.2, 1.2, 0.2],
  38. [ 5.5, 3.5, 1.3, 0.2],
  39. [ 4.9, 3.6, 1.4, 0.1],
  40. [ 4.4, 3. , 1.3, 0.2],
  41. [ 5.1, 3.4, 1.5, 0.2],
  42. [ 5. , 3.5, 1.3, 0.3],
  43. [ 4.5, 2.3, 1.3, 0.3],
  44. [ 4.4, 3.2, 1.3, 0.2],
  45. [ 5. , 3.5, 1.6, 0.6],
  46. [ 5.1, 3.8, 1.9, 0.4],
  47. [ 4.8, 3. , 1.4, 0.3],
  48. [ 5.1, 3.8, 1.6, 0.2],
  49. [ 4.6, 3.2, 1.4, 0.2],
  50. [ 5.3, 3.7, 1.5, 0.2],
  51. [ 5. , 3.3, 1.4, 0.2],
  52. [ 7. , 3.2, 4.7, 1.4],
  53. [ 6.4, 3.2, 4.5, 1.5],
  54. [ 6.9, 3.1, 4.9, 1.5],
  55. [ 5.5, 2.3, 4. , 1.3],
  56. [ 6.5, 2.8, 4.6, 1.5],
  57. [ 5.7, 2.8, 4.5, 1.3],
  58. [ 6.3, 3.3, 4.7, 1.6],
  59. [ 4.9, 2.4, 3.3, 1. ],
  60. [ 6.6, 2.9, 4.6, 1.3],
  61. [ 5.2, 2.7, 3.9, 1.4],
  62. [ 5. , 2. , 3.5, 1. ],
  63. [ 5.9, 3. , 4.2, 1.5],
  64. [ 6. , 2.2, 4. , 1. ],
  65. [ 6.1, 2.9, 4.7, 1.4],
  66. [ 5.6, 2.9, 3.6, 1.3],
  67. [ 6.7, 3.1, 4.4, 1.4],
  68. [ 5.6, 3. , 4.5, 1.5],
  69. [ 5.8, 2.7, 4.1, 1. ],
  70. [ 6.2, 2.2, 4.5, 1.5],
  71. [ 5.6, 2.5, 3.9, 1.1],
  72. [ 5.9, 3.2, 4.8, 1.8],
  73. [ 6.1, 2.8, 4. , 1.3],
  74. [ 6.3, 2.5, 4.9, 1.5],
  75. [ 6.1, 2.8, 4.7, 1.2],
  76. [ 6.4, 2.9, 4.3, 1.3],
  77. [ 6.6, 3. , 4.4, 1.4],
  78. [ 6.8, 2.8, 4.8, 1.4],
  79. [ 6.7, 3. , 5. , 1.7],
  80. [ 6. , 2.9, 4.5, 1.5],
  81. [ 5.7, 2.6, 3.5, 1. ],
  82. [ 5.5, 2.4, 3.8, 1.1],
  83. [ 5.5, 2.4, 3.7, 1. ],
  84. [ 5.8, 2.7, 3.9, 1.2],
  85. [ 6. , 2.7, 5.1, 1.6],
  86. [ 5.4, 3. , 4.5, 1.5],
  87. [ 6. , 3.4, 4.5, 1.6],
  88. [ 6.7, 3.1, 4.7, 1.5],
  89. [ 6.3, 2.3, 4.4, 1.3],
  90. [ 5.6, 3. , 4.1, 1.3],
  91. [ 5.5, 2.5, 4. , 1.3],
  92. [ 5.5, 2.6, 4.4, 1.2],
  93. [ 6.1, 3. , 4.6, 1.4],
  94. [ 5.8, 2.6, 4. , 1.2],
  95. [ 5. , 2.3, 3.3, 1. ],
  96. [ 5.6, 2.7, 4.2, 1.3],
  97. [ 5.7, 3. , 4.2, 1.2],
  98. [ 5.7, 2.9, 4.2, 1.3],
  99. [ 6.2, 2.9, 4.3, 1.3],
  100. [ 5.1, 2.5, 3. , 1.1],
  101. [ 5.7, 2.8, 4.1, 1.3],
  102. [ 6.3, 3.3, 6. , 2.5],
  103. [ 5.8, 2.7, 5.1, 1.9],
  104. [ 7.1, 3. , 5.9, 2.1],
  105. [ 6.3, 2.9, 5.6, 1.8],
  106. [ 6.5, 3. , 5.8, 2.2],
  107. [ 7.6, 3. , 6.6, 2.1],
  108. [ 4.9, 2.5, 4.5, 1.7],
  109. [ 7.3, 2.9, 6.3, 1.8],
  110. [ 6.7, 2.5, 5.8, 1.8],
  111. [ 7.2, 3.6, 6.1, 2.5],
  112. [ 6.5, 3.2, 5.1, 2. ],
  113. [ 6.4, 2.7, 5.3, 1.9],
  114. [ 6.8, 3. , 5.5, 2.1],
  115. [ 5.7, 2.5, 5. , 2. ],
  116. [ 5.8, 2.8, 5.1, 2.4],
  117. [ 6.4, 3.2, 5.3, 2.3],
  118. [ 6.5, 3. , 5.5, 1.8],
  119. [ 7.7, 3.8, 6.7, 2.2],
  120. [ 7.7, 2.6, 6.9, 2.3],
  121. [ 6. , 2.2, 5. , 1.5],
  122. [ 6.9, 3.2, 5.7, 2.3],
  123. [ 5.6, 2.8, 4.9, 2. ],
  124. [ 7.7, 2.8, 6.7, 2. ],
  125. [ 6.3, 2.7, 4.9, 1.8],
  126. [ 6.7, 3.3, 5.7, 2.1],
  127. [ 7.2, 3.2, 6. , 1.8],
  128. [ 6.2, 2.8, 4.8, 1.8],
  129. [ 6.1, 3. , 4.9, 1.8],
  130. [ 6.4, 2.8, 5.6, 2.1],
  131. [ 7.2, 3. , 5.8, 1.6],
  132. [ 7.4, 2.8, 6.1, 1.9],
  133. [ 7.9, 3.8, 6.4, 2. ],
  134. [ 6.4, 2.8, 5.6, 2.2],
  135. [ 6.3, 2.8, 5.1, 1.5],
  136. [ 6.1, 2.6, 5.6, 1.4],
  137. [ 7.7, 3. , 6.1, 2.3],
  138. [ 6.3, 3.4, 5.6, 2.4],
  139. [ 6.4, 3.1, 5.5, 1.8],
  140. [ 6. , 3. , 4.8, 1.8],
  141. [ 6.9, 3.1, 5.4, 2.1],
  142. [ 6.7, 3.1, 5.6, 2.4],
  143. [ 6.9, 3.1, 5.1, 2.3],
  144. [ 5.8, 2.7, 5.1, 1.9],
  145. [ 6.8, 3.2, 5.9, 2.3],
  146. [ 6.7, 3.3, 5.7, 2.5],
  147. [ 6.7, 3. , 5.2, 2.3],
  148. [ 6.3, 2.5, 5. , 1.9],
  149. [ 6.5, 3. , 5.2, 2. ],
  150. [ 6.2, 3.4, 5.4, 2.3],
  151. [ 5.9, 3. , 5.1, 1.8]]),
  152. 'target': array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  153. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  154. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  155. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  156. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  157. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  158. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]), 'frame': None, 'target_names': array([ 'setosa', 'versicolor', 'virginica'], dtype= '<U10'), 'DESCR': '.. _iris_dataset:\n\nIris plants dataset\n--------------------\n\n**Data Set Characteristics:**\n\n:Number of Instances: 150 (50 in each of three classes)\n:Number of Attributes: 4 numeric, predictive attributes and the class\n:Attribute Information:\n - sepal length in cm\n - sepal width in cm\n - petal length in cm\n - petal width in cm\n - class:\n - Iris-Setosa\n - Iris-Versicolour\n - Iris-Virginica\n\n:Summary Statistics:\n\n============== ==== ==== ======= ===== ====================\n Min Max Mean SD Class Correlation\n============== ==== ==== ======= ===== ====================\nsepal length: 4.3 7.9 5.84 0.83 0.7826\nsepal width: 2.0 4.4 3.05 0.43 -0.4194\npetal length: 1.0 6.9 3.76 1.76 0.9490 (high!)\npetal width: 0.1 2.5 1.20 0.76 0.9565 (high!)\n============== ==== ==== ======= ===== ====================\n\n:Missing Attribute Values: None\n:Class Distribution: 33.3% for each of 3 classes.\n:Creator: R.A. Fisher\n:Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n:Date: July, 1988\n\nThe famous Iris database, first used by Sir R.A. Fisher. The dataset is taken\nfrom Fisher\'s paper. Note that it\'s the same as in R, but not as in the UCI\nMachine Learning Repository, which has two wrong data points.\n\nThis is perhaps the best known database to be found in the\npattern recognition literature. Fisher\'s paper is a classic in the field and\nis referenced frequently to this day. (See Duda & Hart, for example.) The\ndata set contains 3 classes of 50 instances each, where each class refers to a\ntype of iris plant. One class is linearly separable from the other 2; the\nlatter are NOT linearly separable from each other.\n\n|details-start|\n**References**\n|details-split|\n\n- Fisher, R.A. "The use of multiple measurements in taxonomic problems"\n Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to\n Mathematical Statistics" (John Wiley, NY, 1950).\n- Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.\n (Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.\n- Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System\n Structure and Classification Rule for Recognition in Partially Exposed\n Environments". IEEE Transactions on Pattern Analysis and Machine\n Intelligence, Vol. PAMI-2, No. 1, 67-71.\n- Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactions\n on Information Theory, May 1972, 431-433.\n- See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II\n conceptual clustering system finds 3 classes in the data.\n- Many, many more ...\n\n|details-end|\n', 'feature_names': [ 'sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'], 'filename': 'iris.csv', 'data_module': 'sklearn.datasets.data'}
  159. ****************************************************************************************************
  160. x数据:
  161. [[ 1.4]
  162. [ 1.4]
  163. [ 1.3]
  164. [ 1.5]
  165. [ 1.4]
  166. [ 1.7]
  167. [ 1.4]
  168. [ 1.5]
  169. [ 1.4]
  170. [ 1.5]
  171. [ 1.5]
  172. [ 1.6]
  173. [ 1.4]
  174. [ 1.1]
  175. [ 1.2]
  176. [ 1.5]
  177. [ 1.3]
  178. [ 1.4]
  179. [ 1.7]
  180. [ 1.5]
  181. [ 1.7]
  182. [ 1.5]
  183. [ 1. ]
  184. [ 1.7]
  185. [ 1.9]
  186. [ 1.6]
  187. [ 1.6]
  188. [ 1.5]
  189. [ 1.4]
  190. [ 1.6]
  191. [ 1.6]
  192. [ 1.5]
  193. [ 1.5]
  194. [ 1.4]
  195. [ 1.5]
  196. [ 1.2]
  197. [ 1.3]
  198. [ 1.4]
  199. [ 1.3]
  200. [ 1.5]
  201. [ 1.3]
  202. [ 1.3]
  203. [ 1.3]
  204. [ 1.6]
  205. [ 1.9]
  206. [ 1.4]
  207. [ 1.6]
  208. [ 1.4]
  209. [ 1.5]
  210. [ 1.4]
  211. [ 4.7]
  212. [ 4.5]
  213. [ 4.9]
  214. [ 4. ]
  215. [ 4.6]
  216. [ 4.5]
  217. [ 4.7]
  218. [ 3.3]
  219. [ 4.6]
  220. [ 3.9]
  221. [ 3.5]
  222. [ 4.2]
  223. [ 4. ]
  224. [ 4.7]
  225. [ 3.6]
  226. [ 4.4]
  227. [ 4.5]
  228. [ 4.1]
  229. [ 4.5]
  230. [ 3.9]
  231. [ 4.8]
  232. [ 4. ]
  233. [ 4.9]
  234. [ 4.7]
  235. [ 4.3]
  236. [ 4.4]
  237. [ 4.8]
  238. [ 5. ]
  239. [ 4.5]
  240. [ 3.5]
  241. [ 3.8]
  242. [ 3.7]
  243. [ 3.9]
  244. [ 5.1]
  245. [ 4.5]
  246. [ 4.5]
  247. [ 4.7]
  248. [ 4.4]
  249. [ 4.1]
  250. [ 4. ]
  251. [ 4.4]
  252. [ 4.6]
  253. [ 4. ]
  254. [ 3.3]
  255. [ 4.2]
  256. [ 4.2]
  257. [ 4.2]
  258. [ 4.3]
  259. [ 3. ]
  260. [ 4.1]
  261. [ 6. ]
  262. [ 5.1]
  263. [ 5.9]
  264. [ 5.6]
  265. [ 5.8]
  266. [ 6.6]
  267. [ 4.5]
  268. [ 6.3]
  269. [ 5.8]
  270. [ 6.1]
  271. [ 5.1]
  272. [ 5.3]
  273. [ 5.5]
  274. [ 5. ]
  275. [ 5.1]
  276. [ 5.3]
  277. [ 5.5]
  278. [ 6.7]
  279. [ 6.9]
  280. [ 5. ]
  281. [ 5.7]
  282. [ 4.9]
  283. [ 6.7]
  284. [ 4.9]
  285. [ 5.7]
  286. [ 6. ]
  287. [ 4.8]
  288. [ 4.9]
  289. [ 5.6]
  290. [ 5.8]
  291. [ 6.1]
  292. [ 6.4]
  293. [ 5.6]
  294. [ 5.1]
  295. [ 5.6]
  296. [ 6.1]
  297. [ 5.6]
  298. [ 5.5]
  299. [ 4.8]
  300. [ 5.4]
  301. [ 5.6]
  302. [ 5.1]
  303. [ 5.1]
  304. [ 5.9]
  305. [ 5.7]
  306. [ 5.2]
  307. [ 5. ]
  308. [ 5.2]
  309. [ 5.4]
  310. [ 5.1]]
  311. ************************************************************
  312. y数据:
  313. [[ 0.2]
  314. [ 0.2]
  315. [ 0.2]
  316. [ 0.2]
  317. [ 0.2]
  318. [ 0.4]
  319. [ 0.3]
  320. [ 0.2]
  321. [ 0.2]
  322. [ 0.1]
  323. [ 0.2]
  324. [ 0.2]
  325. [ 0.1]
  326. [ 0.1]
  327. [ 0.2]
  328. [ 0.4]
  329. [ 0.4]
  330. [ 0.3]
  331. [ 0.3]
  332. [ 0.3]
  333. [ 0.2]
  334. [ 0.4]
  335. [ 0.2]
  336. [ 0.5]
  337. [ 0.2]
  338. [ 0.2]
  339. [ 0.4]
  340. [ 0.2]
  341. [ 0.2]
  342. [ 0.2]
  343. [ 0.2]
  344. [ 0.4]
  345. [ 0.1]
  346. [ 0.2]
  347. [ 0.2]
  348. [ 0.2]
  349. [ 0.2]
  350. [ 0.1]
  351. [ 0.2]
  352. [ 0.2]
  353. [ 0.3]
  354. [ 0.3]
  355. [ 0.2]
  356. [ 0.6]
  357. [ 0.4]
  358. [ 0.3]
  359. [ 0.2]
  360. [ 0.2]
  361. [ 0.2]
  362. [ 0.2]
  363. [ 1.4]
  364. [ 1.5]
  365. [ 1.5]
  366. [ 1.3]
  367. [ 1.5]
  368. [ 1.3]
  369. [ 1.6]
  370. [ 1. ]
  371. [ 1.3]
  372. [ 1.4]
  373. [ 1. ]
  374. [ 1.5]
  375. [ 1. ]
  376. [ 1.4]
  377. [ 1.3]
  378. [ 1.4]
  379. [ 1.5]
  380. [ 1. ]
  381. [ 1.5]
  382. [ 1.1]
  383. [ 1.8]
  384. [ 1.3]
  385. [ 1.5]
  386. [ 1.2]
  387. [ 1.3]
  388. [ 1.4]
  389. [ 1.4]
  390. [ 1.7]
  391. [ 1.5]
  392. [ 1. ]
  393. [ 1.1]
  394. [ 1. ]
  395. [ 1.2]
  396. [ 1.6]
  397. [ 1.5]
  398. [ 1.6]
  399. [ 1.5]
  400. [ 1.3]
  401. [ 1.3]
  402. [ 1.3]
  403. [ 1.2]
  404. [ 1.4]
  405. [ 1.2]
  406. [ 1. ]
  407. [ 1.3]
  408. [ 1.2]
  409. [ 1.3]
  410. [ 1.3]
  411. [ 1.1]
  412. [ 1.3]
  413. [ 2.5]
  414. [ 1.9]
  415. [ 2.1]
  416. [ 1.8]
  417. [ 2.2]
  418. [ 2.1]
  419. [ 1.7]
  420. [ 1.8]
  421. [ 1.8]
  422. [ 2.5]
  423. [ 2. ]
  424. [ 1.9]
  425. [ 2.1]
  426. [ 2. ]
  427. [ 2.4]
  428. [ 2.3]
  429. [ 1.8]
  430. [ 2.2]
  431. [ 2.3]
  432. [ 1.5]
  433. [ 2.3]
  434. [ 2. ]
  435. [ 2. ]
  436. [ 1.8]
  437. [ 2.1]
  438. [ 1.8]
  439. [ 1.8]
  440. [ 1.8]
  441. [ 2.1]
  442. [ 1.6]
  443. [ 1.9]
  444. [ 2. ]
  445. [ 2.2]
  446. [ 1.5]
  447. [ 1.4]
  448. [ 2.3]
  449. [ 2.4]
  450. [ 1.8]
  451. [ 1.8]
  452. [ 2.1]
  453. [ 2.4]
  454. [ 2.3]
  455. [ 1.9]
  456. [ 2.3]
  457. [ 2.5]
  458. [ 2.3]
  459. [ 1.9]
  460. [ 2. ]
  461. [ 2.3]
  462. [ 1.8]]
  463. ****************************************
  464. 权重w: [[ 0.4145955]]
  465. ******************************
  466. 截距b: [- 0.35686296]
  467. ******************************
  468. y_hat预测数据:
  469. [[ 0.22357074]
  470. [ 0.26503029]
  471. [ 1.71611455]
  472. [ 0.22357074]
  473. [ 0.43086849]]
  474. ******************************
  475. y_text真实数据:
  476. [[ 0.3]
  477. [ 0.2]
  478. [ 2. ]
  479. [ 0.1]
  480. [ 0.2]]
  481. ****************************************
  482. 均方误差mse: 0.03810129178169931
  483. ****************************************
  484. 均方根误差rmse: 0.19519552193044623
  485. ****************************************
  486. 平均绝对值误差mae: 0.1430440743648432
  487. ****************************************
  488. r2打分: 0.9368067916048772
  489. 另一种r2打分方法: 0.9368067916048772

5.多元线性回归


    
    
  1. # 安装pip install scikit-learn
  2. #线性回归类
  3. from sklearn.linear_model import LinearRegression
  4. # 数据集划分
  5. from sklearn.model_selection import train_test_split
  6. #数据集
  7. from sklearn.datasets import load_iris
  8. # 多元线性回归
  9. #获取数据集
  10. iris = load_iris()
  11. print( 'iris原始数据:')
  12. print(iris)
  13. print( '*'* 100)
  14. #设定特征值,xy
  15. x,y = iris.data,iris.target.reshape(- 1, 1)
  16. #x为多元
  17. print( 'x数据为:',x)
  18. print( 'x长度为:', len(x))
  19. print( '*'* 50)
  20. print( 'y数据为:',y)
  21. print( 'y长度为:', len(y))
  22. print( '*'* 50)
  23. # 划分训练集和测试集
  24. x_train,x_test,y_train,y_test = train_test_split(x,y,test_size= 0.2,random_state= 2)
  25. # 建立模型
  26. lr = LinearRegression()
  27. #拟合,计算w和b
  28. lr.fit(x_train,y_train)
  29. w = lr.coef_
  30. print( '权重w的值:')
  31. print(w)
  32. b = lr.intercept_
  33. print( '截距b的值:')
  34. print(b)
  35. print( '*'* 50)
  36. #预测
  37. y_hat = lr.predict(x_test)
  38. print( 'y_hat预测值:')
  39. print(y_hat[: 10])
  40. print( 'y_test真实值:')
  41. print(y_test[: 10])
  42. print( '*'* 40)
  43. # 导入回归模型评估
  44. from sklearn.metrics import mean_squared_error,mean_absolute_error,r2_score
  45. mse = mean_squared_error(y_test,y_hat)
  46. print( '均方误差mse:')
  47. print(mse)
  48. print( '*'* 40)
  49. rmse = mse** 0.5
  50. print( '均方根误差rmse:')
  51. print(rmse)
  52. print( '*'* 40)
  53. mae = mean_absolute_error(y_test,y_hat)
  54. print( '平绝对值误差:')
  55. print(mae)
  56. print( '*'* 40)
  57. r2 = r2_score(y_test,y_hat)
  58. print( 'r2打分:')
  59. print(r2)
  60. print( '另一种r2打分:')
  61. print(lr.score(x_test,y_test))

运行结果:


    
    
  1. iris原始数据:
  2. { 'data': array([[ 5.1, 3.5, 1.4, 0.2],
  3. [ 4.9, 3. , 1.4, 0.2],
  4. [ 4.7, 3.2, 1.3, 0.2],
  5. [ 4.6, 3.1, 1.5, 0.2],
  6. [ 5. , 3.6, 1.4, 0.2],
  7. [ 5.4, 3.9, 1.7, 0.4],
  8. [ 4.6, 3.4, 1.4, 0.3],
  9. [ 5. , 3.4, 1.5, 0.2],
  10. [ 4.4, 2.9, 1.4, 0.2],
  11. [ 4.9, 3.1, 1.5, 0.1],
  12. [ 5.4, 3.7, 1.5, 0.2],
  13. [ 4.8, 3.4, 1.6, 0.2],
  14. [ 4.8, 3. , 1.4, 0.1],
  15. [ 4.3, 3. , 1.1, 0.1],
  16. [ 5.8, 4. , 1.2, 0.2],
  17. [ 5.7, 4.4, 1.5, 0.4],
  18. [ 5.4, 3.9, 1.3, 0.4],
  19. [ 5.1, 3.5, 1.4, 0.3],
  20. [ 5.7, 3.8, 1.7, 0.3],
  21. [ 5.1, 3.8, 1.5, 0.3],
  22. [ 5.4, 3.4, 1.7, 0.2],
  23. [ 5.1, 3.7, 1.5, 0.4],
  24. [ 4.6, 3.6, 1. , 0.2],
  25. [ 5.1, 3.3, 1.7, 0.5],
  26. [ 4.8, 3.4, 1.9, 0.2],
  27. [ 5. , 3. , 1.6, 0.2],
  28. [ 5. , 3.4, 1.6, 0.4],
  29. [ 5.2, 3.5, 1.5, 0.2],
  30. [ 5.2, 3.4, 1.4, 0.2],
  31. [ 4.7, 3.2, 1.6, 0.2],
  32. [ 4.8, 3.1, 1.6, 0.2],
  33. [ 5.4, 3.4, 1.5, 0.4],
  34. [ 5.2, 4.1, 1.5, 0.1],
  35. [ 5.5, 4.2, 1.4, 0.2],
  36. [ 4.9, 3.1, 1.5, 0.2],
  37. [ 5. , 3.2, 1.2, 0.2],
  38. [ 5.5, 3.5, 1.3, 0.2],
  39. [ 4.9, 3.6, 1.4, 0.1],
  40. [ 4.4, 3. , 1.3, 0.2],
  41. [ 5.1, 3.4, 1.5, 0.2],
  42. [ 5. , 3.5, 1.3, 0.3],
  43. [ 4.5, 2.3, 1.3, 0.3],
  44. [ 4.4, 3.2, 1.3, 0.2],
  45. [ 5. , 3.5, 1.6, 0.6],
  46. [ 5.1, 3.8, 1.9, 0.4],
  47. [ 4.8, 3. , 1.4, 0.3],
  48. [ 5.1, 3.8, 1.6, 0.2],
  49. [ 4.6, 3.2, 1.4, 0.2],
  50. [ 5.3, 3.7, 1.5, 0.2],
  51. [ 5. , 3.3, 1.4, 0.2],
  52. [ 7. , 3.2, 4.7, 1.4],
  53. [ 6.4, 3.2, 4.5, 1.5],
  54. [ 6.9, 3.1, 4.9, 1.5],
  55. [ 5.5, 2.3, 4. , 1.3],
  56. [ 6.5, 2.8, 4.6, 1.5],
  57. [ 5.7, 2.8, 4.5, 1.3],
  58. [ 6.3, 3.3, 4.7, 1.6],
  59. [ 4.9, 2.4, 3.3, 1. ],
  60. [ 6.6, 2.9, 4.6, 1.3],
  61. [ 5.2, 2.7, 3.9, 1.4],
  62. [ 5. , 2. , 3.5, 1. ],
  63. [ 5.9, 3. , 4.2, 1.5],
  64. [ 6. , 2.2, 4. , 1. ],
  65. [ 6.1, 2.9, 4.7, 1.4],
  66. [ 5.6, 2.9, 3.6, 1.3],
  67. [ 6.7, 3.1, 4.4, 1.4],
  68. [ 5.6, 3. , 4.5, 1.5],
  69. [ 5.8, 2.7, 4.1, 1. ],
  70. [ 6.2, 2.2, 4.5, 1.5],
  71. [ 5.6, 2.5, 3.9, 1.1],
  72. [ 5.9, 3.2, 4.8, 1.8],
  73. [ 6.1, 2.8, 4. , 1.3],
  74. [ 6.3, 2.5, 4.9, 1.5],
  75. [ 6.1, 2.8, 4.7, 1.2],
  76. [ 6.4, 2.9, 4.3, 1.3],
  77. [ 6.6, 3. , 4.4, 1.4],
  78. [ 6.8, 2.8, 4.8, 1.4],
  79. [ 6.7, 3. , 5. , 1.7],
  80. [ 6. , 2.9, 4.5, 1.5],
  81. [ 5.7, 2.6, 3.5, 1. ],
  82. [ 5.5, 2.4, 3.8, 1.1],
  83. [ 5.5, 2.4, 3.7, 1. ],
  84. [ 5.8, 2.7, 3.9, 1.2],
  85. [ 6. , 2.7, 5.1, 1.6],
  86. [ 5.4, 3. , 4.5, 1.5],
  87. [ 6. , 3.4, 4.5, 1.6],
  88. [ 6.7, 3.1, 4.7, 1.5],
  89. [ 6.3, 2.3, 4.4, 1.3],
  90. [ 5.6, 3. , 4.1, 1.3],
  91. [ 5.5, 2.5, 4. , 1.3],
  92. [ 5.5, 2.6, 4.4, 1.2],
  93. [ 6.1, 3. , 4.6, 1.4],
  94. [ 5.8, 2.6, 4. , 1.2],
  95. [ 5. , 2.3, 3.3, 1. ],
  96. [ 5.6, 2.7, 4.2, 1.3],
  97. [ 5.7, 3. , 4.2, 1.2],
  98. [ 5.7, 2.9, 4.2, 1.3],
  99. [ 6.2, 2.9, 4.3, 1.3],
  100. [ 5.1, 2.5, 3. , 1.1],
  101. [ 5.7, 2.8, 4.1, 1.3],
  102. [ 6.3, 3.3, 6. , 2.5],
  103. [ 5.8, 2.7, 5.1, 1.9],
  104. [ 7.1, 3. , 5.9, 2.1],
  105. [ 6.3, 2.9, 5.6, 1.8],
  106. [ 6.5, 3. , 5.8, 2.2],
  107. [ 7.6, 3. , 6.6, 2.1],
  108. [ 4.9, 2.5, 4.5, 1.7],
  109. [ 7.3, 2.9, 6.3, 1.8],
  110. [ 6.7, 2.5, 5.8, 1.8],
  111. [ 7.2, 3.6, 6.1, 2.5],
  112. [ 6.5, 3.2, 5.1, 2. ],
  113. [ 6.4, 2.7, 5.3, 1.9],
  114. [ 6.8, 3. , 5.5, 2.1],
  115. [ 5.7, 2.5, 5. , 2. ],
  116. [ 5.8, 2.8, 5.1, 2.4],
  117. [ 6.4, 3.2, 5.3, 2.3],
  118. [ 6.5, 3. , 5.5, 1.8],
  119. [ 7.7, 3.8, 6.7, 2.2],
  120. [ 7.7, 2.6, 6.9, 2.3],
  121. [ 6. , 2.2, 5. , 1.5],
  122. [ 6.9, 3.2, 5.7, 2.3],
  123. [ 5.6, 2.8, 4.9, 2. ],
  124. [ 7.7, 2.8, 6.7, 2. ],
  125. [ 6.3, 2.7, 4.9, 1.8],
  126. [ 6.7, 3.3, 5.7, 2.1],
  127. [ 7.2, 3.2, 6. , 1.8],
  128. [ 6.2, 2.8, 4.8, 1.8],
  129. [ 6.1, 3. , 4.9, 1.8],
  130. [ 6.4, 2.8, 5.6, 2.1],
  131. [ 7.2, 3. , 5.8, 1.6],
  132. [ 7.4, 2.8, 6.1, 1.9],
  133. [ 7.9, 3.8, 6.4, 2. ],
  134. [ 6.4, 2.8, 5.6, 2.2],
  135. [ 6.3, 2.8, 5.1, 1.5],
  136. [ 6.1, 2.6, 5.6, 1.4],
  137. [ 7.7, 3. , 6.1, 2.3],
  138. [ 6.3, 3.4, 5.6, 2.4],
  139. [ 6.4, 3.1, 5.5, 1.8],
  140. [ 6. , 3. , 4.8, 1.8],
  141. [ 6.9, 3.1, 5.4, 2.1],
  142. [ 6.7, 3.1, 5.6, 2.4],
  143. [ 6.9, 3.1, 5.1, 2.3],
  144. [ 5.8, 2.7, 5.1, 1.9],
  145. [ 6.8, 3.2, 5.9, 2.3],
  146. [ 6.7, 3.3, 5.7, 2.5],
  147. [ 6.7, 3. , 5.2, 2.3],
  148. [ 6.3, 2.5, 5. , 1.9],
  149. [ 6.5, 3. , 5.2, 2. ],
  150. [ 6.2, 3.4, 5.4, 2.3],
  151. [ 5.9, 3. , 5.1, 1.8]]),
  152. 'target': array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  153. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  154. 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  155. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
  156. 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  157. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
  158. 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2]), 'frame': None, 'target_names': array([ 'setosa', 'versicolor', 'virginica'], dtype= '<U10'), 'DESCR': '.. _iris_dataset:\n\nIris plants dataset\n--------------------\n\n**Data Set Characteristics:**\n\n:Number of Instances: 150 (50 in each of three classes)\n:Number of Attributes: 4 numeric, predictive attributes and the class\n:Attribute Information:\n - sepal length in cm\n - sepal width in cm\n - petal length in cm\n - petal width in cm\n - class:\n - Iris-Setosa\n - Iris-Versicolour\n - Iris-Virginica\n\n:Summary Statistics:\n\n============== ==== ==== ======= ===== ====================\n Min Max Mean SD Class Correlation\n============== ==== ==== ======= ===== ====================\nsepal length: 4.3 7.9 5.84 0.83 0.7826\nsepal width: 2.0 4.4 3.05 0.43 -0.4194\npetal length: 1.0 6.9 3.76 1.76 0.9490 (high!)\npetal width: 0.1 2.5 1.20 0.76 0.9565 (high!)\n============== ==== ==== ======= ===== ====================\n\n:Missing Attribute Values: None\n:Class Distribution: 33.3% for each of 3 classes.\n:Creator: R.A. Fisher\n:Donor: Michael Marshall (MARSHALL%PLU@io.arc.nasa.gov)\n:Date: July, 1988\n\nThe famous Iris database, first used by Sir R.A. Fisher. The dataset is taken\nfrom Fisher\'s paper. Note that it\'s the same as in R, but not as in the UCI\nMachine Learning Repository, which has two wrong data points.\n\nThis is perhaps the best known database to be found in the\npattern recognition literature. Fisher\'s paper is a classic in the field and\nis referenced frequently to this day. (See Duda & Hart, for example.) The\ndata set contains 3 classes of 50 instances each, where each class refers to a\ntype of iris plant. One class is linearly separable from the other 2; the\nlatter are NOT linearly separable from each other.\n\n|details-start|\n**References**\n|details-split|\n\n- Fisher, R.A. "The use of multiple measurements in taxonomic problems"\n Annual Eugenics, 7, Part II, 179-188 (1936); also in "Contributions to\n Mathematical Statistics" (John Wiley, NY, 1950).\n- Duda, R.O., & Hart, P.E. (1973) Pattern Classification and Scene Analysis.\n (Q327.D83) John Wiley & Sons. ISBN 0-471-22361-1. See page 218.\n- Dasarathy, B.V. (1980) "Nosing Around the Neighborhood: A New System\n Structure and Classification Rule for Recognition in Partially Exposed\n Environments". IEEE Transactions on Pattern Analysis and Machine\n Intelligence, Vol. PAMI-2, No. 1, 67-71.\n- Gates, G.W. (1972) "The Reduced Nearest Neighbor Rule". IEEE Transactions\n on Information Theory, May 1972, 431-433.\n- See also: 1988 MLC Proceedings, 54-64. Cheeseman et al"s AUTOCLASS II\n conceptual clustering system finds 3 classes in the data.\n- Many, many more ...\n\n|details-end|\n', 'feature_names': [ 'sepal length (cm)', 'sepal width (cm)', 'petal length (cm)', 'petal width (cm)'], 'filename': 'iris.csv', 'data_module': 'sklearn.datasets.data'}
  159. *****************************************************************************************
  160. x数据为:
  161. [[ 5.1 3.5 1.4 0.2]
  162. [ 4.9 3. 1.4 0.2]
  163. [ 4.7 3.2 1.3 0.2]
  164. [ 4.6 3.1 1.5 0.2]
  165. [ 5. 3.6 1.4 0.2]
  166. [ 5.4 3.9 1.7 0.4]
  167. [ 4.6 3.4 1.4 0.3]
  168. [ 5. 3.4 1.5 0.2]
  169. [ 4.4 2.9 1.4 0.2]
  170. [ 4.9 3.1 1.5 0.1]
  171. [ 5.4 3.7 1.5 0.2]
  172. [ 4.8 3.4 1.6 0.2]
  173. [ 4.8 3. 1.4 0.1]
  174. [ 4.3 3. 1.1 0.1]
  175. [ 5.8 4. 1.2 0.2]
  176. [ 5.7 4.4 1.5 0.4]
  177. [ 5.4 3.9 1.3 0.4]
  178. [ 5.1 3.5 1.4 0.3]
  179. [ 5.7 3.8 1.7 0.3]
  180. [ 5.1 3.8 1.5 0.3]
  181. [ 5.4 3.4 1.7 0.2]
  182. [ 5.1 3.7 1.5 0.4]
  183. [ 4.6 3.6 1. 0.2]
  184. [ 5.1 3.3 1.7 0.5]
  185. [ 4.8 3.4 1.9 0.2]
  186. [ 5. 3. 1.6 0.2]
  187. [ 5. 3.4 1.6 0.4]
  188. [ 5.2 3.5 1.5 0.2]
  189. [ 5.2 3.4 1.4 0.2]
  190. [ 4.7 3.2 1.6 0.2]
  191. [ 4.8 3.1 1.6 0.2]
  192. [ 5.4 3.4 1.5 0.4]
  193. [ 5.2 4.1 1.5 0.1]
  194. [ 5.5 4.2 1.4 0.2]
  195. [ 4.9 3.1 1.5 0.2]
  196. [ 5. 3.2 1.2 0.2]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 线性回归是一种统计学方法,可用于预测数值型变量之间的关系,如房价和房屋尺寸之间的关系。在这个场景下,我们考虑通过线性回归来建立一种模型,在已知的房屋尺寸的基础上,预测相应的房价。 假设我们有一个包含有房价和房屋尺寸的数据集,我们可以使用数据探索的工具,如散点图,来初步探索两个变量之间的关系。然后,我们可以使用线性回归模型来拟合这些数据点,并且预测新的房屋尺寸的房价。 接下来,我们将绘制一个图形来展示我们的线性回归模型如何拟合数据点和预测房价。在这张图中,我们将在横轴上表示房屋尺寸,纵轴上表示房价,并绘制出我们的线性回归模型所拟合的直线。这张图将使我们更容易地理解房价和房屋尺寸之间的关系,并且可以用于后续的数据分析以及预测。 在绘制完这张图后,我们可以检查线性回归模型的拟合精度。如果线性回归模型在数据集中存在显著的偏差,就需要重新考虑预测模型,或者增加更多的特征变量,这样可以使预测的结果更准确。此外,在应用线性回归模型之前,我们还应该注意一些其他的影响因素,如噪声或异常值,这样可以避免模型的偏差以及其他的预测错误。 ### 回答2: 线性回归是一种常用的机器学习算法,可以用于预测房价等连续值的问题。具体地说,线性回归就是通过找到一条直线(或者超平面,在高维空间中)来尽可能地拟合已知数据,然后利用这条直线进行预测。 在房价预测的问题中,我们可以使用线性回归算法来构建一个模型。首先,我们需要收集一些房价相关的数据,例如房屋面积、地理位置、年龄等等。然后,我们可以使用这些数据来训练线性回归模型,找到一个最优的线性函数,使得它最好地拟合已有的数据。 训练模型之后,我们就可以利用这个模型来进行预测。比如,我们输入某个房屋的面积、位置等信息,就可以利用模型预测这个房屋的价格了。 为了更加直观地理解线性回归算法,我们可以绘制出数据点和拟合直线的图像。在这个图像中,我们可以看到每一个数据点的位置,以及拟合直线的位置,这样可以更加方便地理解线性回归算法的表现。 总之,线性回归是一种非常实用的机器学习算法,它可以帮助我们解决很多连续值预测的问题,例如房价预测等。同时,在理解线性回归算法的时候,我们可以通过绘制图像来更好地理解模型的表现。 ### 回答3: 线性回归是一种广泛用于预测连续数值的统计学方法,常用于房价预测。我们可以通过已知的房屋面积、房龄等特征,拟合出一个数学函数,进而计算出未知房屋的价格。下面我将简单介绍如何使用Python中的scikit-learn库进行线性回归分析,以及如何绘制预测结果的图像。 首先,我们需要加载数据并探索数据的基本特征。数据可以从Kaggle等网站下载得到。以Boston House Price数据集为例,我们可以通过Pandas库读入数据并查看前几行数据的情况: ``` import pandas as pd df = pd.read_csv('train.csv') print(df.head()) ``` 接下来,我们需要针对数据的特征选择适当的模型进行拟合。这里我们选取最简单的线性回归模型。通过scikit-learn库中的LinearRegression模块,可以方便地进行模型训练。 ``` from sklearn.linear_model import LinearRegression X = df[['RM', 'LSTAT', 'PTRATIO']] # 我们选择房间数量、低收入人群比例以及学生与教师之比三个特征来预测房价 y = df['MEDV'] lr = LinearRegression() lr.fit(X, y) # 模型拟合 ``` 至此,我们已经拟合出了一个模型,可以使用测试数据集进行预测并计算模型的评估指标,例如均方误差(Mean Squared Error,MSE)等。同时,我们还可以通过matplotlib库绘制出预测结果的图像: ``` import matplotlib.pyplot as plt import numpy as np import seaborn as sns sns.set(style='whitegrid', context='notebook') pred_y = lr.predict(X) plt.scatter(y, pred_y) plt.plot([0, 50], [0, 50], '--k') plt.xlabel('True value') plt.ylabel('Predicted value') ``` 在图像中,横坐标代表真实房价,纵坐标代表预测房价。可以看到,预测结果与真实情况的差异较小,说明模型的拟合效果较好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值