目标:运用scikit-learn进行多元线性回归方程的构建,通过实际案例的训练集和测试集进行预测,最终通过预测结果和MSE来评估预测的精度。
一、首先安装scikit-learn:pip install scikit-learn
C:\Users\CMCC\PycharmProjects\AiProject> pip install scikit-learn
二、项目实战:糖尿病预测,你的健康守护者!
from sklearn import datasets
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import mean_squared_error
from sklearn.model_selection import train_test_split#加载糖尿病数据集
diabetes=datasets.load_diabetes()
x=diabetes.data
y=diabetes.targetprint("多元的参数集是:")
print(x)
print("结果集是:")
print(y)#将数据集拆分为训练集和测试集,测试集占20%,训练集占80%
x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.2)#创建一个多元线性回归算法对象
lr=LogisticRegression()#使用训练集训练模型
lr.fit(x_train,y_train)#使用测试集进行结果的预测
y_pred_test=lr.predict(x_test)
y_pred_train=lr.predict(x_train)print("测试集的预测结果是:")
print(y_pred_test)
print("训练集的预测结果是:")
print(y_pred_train)#打印模型的均方差,只保留两位的小数点,分别对于训练集和测试集的均方差进行对比,越小越好,证明越预测得准确
print("均方差:%.2f" % mean_squared_error(y_train ,y_pred_train))
print("均方差:%.2f" % mean_squared_error(y_test ,y_pred_test))多元的参数集是:
[[ 0.03807591 0.05068012 0.06169621 ... -0.00259226 0.01990842-0.01764613][-0.00188202 -0.04464164 -0.05147406 ... -0.03949338 -0.06832974-0.09220405][ 0.08529891 0.05068012 0.04445121 ... -0.00259226 0.00286377-0.02593034]...[ 0.04170844 0.05068012 -0.01590626 ... -0.01107952 -0.046879480.01549073][-0.04547248 -0.04464164 0.03906215 ... 0.02655962 0.04452837-0.02593034][-0.04547248 -0.04464164 -0.0730303 ... -0.03949338 -0.004219860.00306441]]
结果集是:
[151. 75. 141. 206. 135. 97. 138. 63. 110. 310. 101. 69. 179. 185.118. 171. 166. 144. 97. 168. 68. 49. 68. 245. 184. 202. 137. 85.131. 283. 129. 59. 341. 87. 65. 102. 265. 276. 252. 90. 100. 55.61. 92. 259. 53. 190. 142. 75. 142. 155. 225. 59. 104. 182. 128.52. 37. 170. 170. 61. 144. 52. 128. 71. 163. 150. 97. 160. 178.48. 270. 202. 111. 85. 42. 170. 200. 252. 113. 143. 51. 52. 210.65. 141. 55. 134. 42. 111. 98. 164. 48. 96. 90. 162. 150. 279.92. 83. 128. 102. 302. 198. 95. 53. 134. 144. 232. 81. 104. 59.246. 297. 258. 229. 275. 281. 179. 200. 200. 173. 180. 84. 121. 161.99. 109. 115. 268. 274. 158. 107. 83. 103. 272. 85. 280. 336. 281.118. 317. 235. 60. 174. 259. 178. 128. 96. 126. 288. 88. 292. 71.197. 186. 25. 84. 96. 195. 53. 217. 172. 131. 214. 59. 70. 220.268. 152. 47. 74. 295. 101. 151. 127. 237. 225. 81. 151. 107. 64.138. 185. 265. 101. 137. 143. 141. 79. 292. 178. 91. 116. 86. 122.72. 129. 142. 90. 158. 39. 196. 222. 277. 99. 196. 202. 155. 77.191. 70. 73. 49. 65. 263. 248. 296. 214. 185. 78. 93. 252. 150.77. 208. 77. 108. 160. 53. 220. 154. 259. 90. 246. 124. 67. 72.257. 262. 275. 177. 71. 47. 187. 125. 78. 51. 258. 215. 303. 243.91. 150. 310. 153. 346. 63. 89. 50. 39. 103. 308. 116. 145. 74.45. 115. 264. 87. 202. 127. 182. 241. 66. 94. 283. 64. 102. 200.265. 94. 230. 181. 156. 233. 60. 219. 80. 68. 332. 248. 84. 200.55. 85. 89. 31. 129. 83. 275. 65. 198. 236. 253. 124. 44. 172.114. 142. 109. 180. 144. 163. 147. 97. 220. 190. 109. 191. 122. 230.242. 248. 249. 192. 131. 237. 78. 135. 244. 199. 270. 164. 72. 96.306. 91. 214. 95. 216. 263. 178. 113. 200. 139. 139. 88. 148. 88.243. 71. 77. 109. 272. 60. 54. 221. 90. 311. 281. 182. 321. 58.262. 206. 233. 242. 123. 167. 63. 197. 71. 168. 140. 217. 121. 235.245. 40. 52. 104. 132. 88. 69. 219. 72. 201. 110. 51. 277. 63.118. 69. 273. 258. 43. 198. 242. 232. 175. 93. 168. 275. 293. 281.72. 140. 189. 181. 209. 136. 261. 113. 131. 174. 257. 55. 84. 42.146. 212. 233. 91. 111. 152. 120. 67. 310. 94. 183. 66. 173. 72.49. 64. 48. 178. 104. 132. 220. 57.]
测试集的预测结果是:
[72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.]
训练集的预测结果是:
[72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.72. 72. 72. 72. 72. 72. 72. 72. 72. 72. 72.]
均方差:12075.22
均方差:13446.11