机器学习系统设计(1)——第一个机器学习应用

来源:互联网 发布:童装网络营销策划书 编辑:程序博客网 时间:2024/06/05 17:22

这里写图片描述

import scipy as spimport matplotlib.pyplot as plt'''precision 浮点数输出精度位数(默认值8位)suppress 是否 禁止 使用 科学记数法(默认为False)打印小浮点值'''sp.set_printoptions(precision=4, suppress=True)# 以 \t 为分隔符data = sp.genfromtxt("web_traffic.tsv", delimiter="\t")# print(data[:10])# print(type(data))# print(data.shape)# 小时信息x = data[:, 0]# 某个小时的 Web 访问数y = data[:, 1]def error(f, x, y):    return sp.sum((f(x)-y)**2)# sp.isnan(y) 返回一个布尔型的数组,用来表示数组项中的内容是否 合法# 统计缺失值的数量# print(sp.sum(sp.isnan(y)))# 使用 ~ 在 x 和 y 中值选择 y 值合法的项x = x[~sp.isnan(y)]y = y[~sp.isnan(y)]# print(x)# print(y)# print(x.shape)# 以 x 和 y 作为数据,初始化一个散点图plt.scatter(x, y)plt.title("Web traffic over the last month")plt.xlabel("Time")plt.ylabel("Hits/Hour")# 设置 x 轴的 刻度 及 与之对应 的 坐标plt.xticks([w*7*24 for w in range(10)],           ['week %i'%w for w in range(10)])# sp.polyfit() 前两个参数为 数组,第三个参数为 拟合多项式的程度# 返回值为 多项式系数,最小二乘拟合的残差,......fp1, residuals, rank, sv, rcond = sp.polyfit(x, y, 1, full=True)print('Model parameters: {}'.format(fp1))print('residuals is {}'.format(residuals))# 第一个训练模型f1 = sp.poly1d(fp1)print(type(f1))print(error(f1, x, y))# 在指定的间隔内返回均匀间隔的数字 sp.linspace(start, stop, 1000)fx = sp.linspace(0, x[-1], 1000)print(fx)# 画线形图 x y 线宽plt.plot(fx, f1(fx), linewidth=4)# 显示图例 参数 loc 设置图例显示的位置plt.legend(['d=%i'%f1.order], loc='upper right')# 自动缩放 tight(固定)plt.autoscale(tight=True)# 打开或关闭轴网格 grid(格)plt.grid()# 显示图像plt.show()f2p = sp.polyfit(x, y, 2)f2 = sp.poly1d(f2p)print(error(f2, x, y))# ---------------------------------# 拐点inflection = int(3.5*7*24)# 拐点之前的数据xa = x[:inflection]ya = y[:inflection]# 拐点之后的数据xb = x[inflection:]yb = y[inflection:]fa = sp.poly1d(sp.polyfit(xa, ya, 1))fb = sp.poly1d(sp.polyfit(xb, yb, 1))fa_error = error(fa, xa, ya)fb_error = error(fb, xb, yb)print('Error inflection=%f' % (fa_error + fb_error))# 使用拐點之後的數據進行2階擬合fb2 = sp.poly1d(sp.polyfit(xb, yb, 2))from scipy.optimize import fsolve# fsolve() 可以找到一个函数的根'''  给出一个起始估计值 800  返回由“func(x)= 0”定义的(非线性)方程的根 func(x)=fb2-100000'''reached_max = fsolve(fb2 - 100000, 800)/(7*24)print("100,000 hits/hour excepted at week {}".format(reached_max[0]))
C:\Users\rHotD\Anaconda3\python.exe C:/Users/rHotD/PycharmProjects/BuildingMachineLearningSystemWithPython/chapter-1/MLApp(little).pyModel parameters: [   2.5962  989.0249]residuals is [  3.1739e+08]<class 'numpy.lib.polynomial.poly1d'>317389767.34[   0.        0.7437    1.4875    2.2312    2.975     3.7187    4.4625    5.2062    5.9499    6.6937    7.4374    8.1812    8.9249    9.6687   10.4124   11.1562   11.8999   12.6436   13.3874   14.1311   14.8749   15.6186   16.3624   17.1061   17.8498   18.5936   19.3373   20.0811   20.8248   21.5686   22.3123   23.0561   23.7998   24.5435   25.2873   26.031    26.7748   27.5185   28.2623   29.006    29.7497   30.4935   31.2372   31.981    32.7247   33.4685   34.2122   34.956    35.6997   36.4434   37.1872   37.9309   38.6747   39.4184   40.1622   40.9059   41.6496   42.3934   43.1371   43.8809   44.6246   45.3684   46.1121   46.8559   47.5996   48.3433   49.0871   49.8308   50.5746   51.3183   52.0621   52.8058   53.5495   54.2933   55.037    55.7808   56.5245   57.2683   58.012    58.7558   59.4995   60.2432   60.987    61.7307   62.4745   63.2182   63.962    64.7057   65.4494   66.1932   66.9369   67.6807   68.4244   69.1682   69.9119   70.6557   71.3994   72.1431   72.8869   73.6306   74.3744   75.1181   75.8619   76.6056   77.3493   78.0931   78.8368   79.5806   80.3243   81.0681   81.8118   82.5556   83.2993   84.043    84.7868   85.5305   86.2743   87.018    87.7618   88.5055   89.2492   89.993    90.7367   91.4805   92.2242   92.968   93.7117   94.4555   95.1992   95.9429   96.6867   97.4304   98.1742   98.9179   99.6617  100.4054  101.1491  101.8929  102.6366  103.3804  104.1241  104.8679  105.6116  106.3554  107.0991  107.8428  108.5866  109.3303  110.0741  110.8178  111.5616  112.3053  113.049   113.7928  114.5365  115.2803  116.024   116.7678  117.5115  118.2553  118.999  119.7427  120.4865  121.2302  121.974   122.7177  123.4615  124.2052  124.9489  125.6927  126.4364  127.1802  127.9239  128.6677  129.4114  130.1552  130.8989  131.6426  132.3864  133.1301  133.8739  134.6176  135.3614  136.1051  136.8488  137.5926  138.3363  139.0801  139.8238  140.5676  141.3113  142.0551  142.7988  143.5425  144.2863  145.03  145.7738  146.5175  147.2613  148.005   148.7487  149.4925  150.2362  150.98    151.7237  152.4675  153.2112  153.955   154.6987  155.4424  156.1862  156.9299  157.6737  158.4174  159.1612  159.9049  160.6486  161.3924  162.1361  162.8799  163.6236  164.3674  165.1111  165.8549  166.5986  167.3423  168.0861  168.8298  169.5736  170.3173  171.0611  171.8048  172.5485  173.2923  174.036   174.7798  175.5235  176.2673  177.011   177.7548  178.4985  179.2422  179.986   180.7297  181.4735  182.2172  182.961   183.7047  184.4484  185.1922  185.9359  186.6797  187.4234  188.1672  188.9109  189.6547  190.3984  191.1421  191.8859  192.6296  193.3734  194.1171  194.8609  195.6046  196.3483  197.0921  197.8358  198.5796  199.3233  200.0671  200.8108  201.5546  202.2983  203.042   203.7858  204.5295  205.2733  206.017   206.7608  207.5045  208.2482  208.992   209.7357  210.4795  211.2232  211.967   212.7107  213.4545  214.1982  214.9419  215.6857  216.4294  217.1732  217.9169  218.6607  219.4044  220.1481  220.8919  221.6356  222.3794  223.1231  223.8669  224.6106  225.3544  226.0981  226.8418  227.5856  228.3293  229.0731  229.8168  230.5606  231.3043  232.048   232.7918  233.5355  234.2793  235.023   235.7668  236.5105  237.2543  237.998   238.7417  239.4855  240.2292  240.973   241.7167  242.4605  243.2042  243.9479  244.6917  245.4354  246.1792  246.9229  247.6667  248.4104  249.1542  249.8979  250.6416  251.3854  252.1291  252.8729  253.6166  254.3604  255.1041  255.8478  256.5916  257.3353  258.0791  258.8228  259.5666  260.3103  261.0541  261.7978  262.5415  263.2853  264.029   264.7728  265.5165  266.2603  267.004   267.7477  268.4915  269.2352  269.979  270.7227  271.4665  272.2102  272.954   273.6977  274.4414  275.1852  275.9289  276.6727  277.4164  278.1602  278.9039  279.6476  280.3914  281.1351  281.8789  282.6226  283.3664  284.1101  284.8539  285.5976  286.3413  287.0851  287.8288  288.5726  289.3163  290.0601  290.8038  291.5475  292.2913  293.035   293.7788  294.5225  295.2663  296.01  296.7538  297.4975  298.2412  298.985   299.7287  300.4725  301.2162  301.96    302.7037  303.4474  304.1912  304.9349  305.6787  306.4224  307.1662  307.9099  308.6537  309.3974  310.1411  310.8849  311.6286  312.3724  313.1161  313.8599  314.6036  315.3473  316.0911  316.8348  317.5786  318.3223  319.0661  319.8098  320.5536  321.2973  322.041  322.7848  323.5285  324.2723  325.016   325.7598  326.5035  327.2472  327.991   328.7347  329.4785  330.2222  330.966   331.7097  332.4535  333.1972  333.9409  334.6847  335.4284  336.1722  336.9159  337.6597  338.4034  339.1471  339.8909  340.6346  341.3784  342.1221  342.8659  343.6096  344.3534  345.0971  345.8408  346.5846  347.3283  348.0721  348.8158  349.5596  350.3033  351.047   351.7908  352.5345  353.2783  354.022   354.7658  355.5095  356.2533  356.997   357.7407  358.4845  359.2282  359.972   360.7157  361.4595  362.2032  362.9469  363.6907  364.4344  365.1782  365.9219  366.6657  367.4094  368.1532  368.8969  369.6406  370.3844  371.1281  371.8719  372.6156  373.3594  374.1031  374.8468  375.5906  376.3343  377.0781  377.8218  378.5656  379.3093  380.0531  380.7968  381.5405  382.2843  383.028   383.7718  384.5155  385.2593  386.003   386.7467  387.4905  388.2342  388.978   389.7217  390.4655  391.2092  391.953   392.6967  393.4404  394.1842  394.9279  395.6717  396.4154  397.1592  397.9029  398.6466  399.3904  400.1341  400.8779  401.6216  402.3654  403.1091  403.8529  404.5966  405.3403  406.0841  406.8278  407.5716  408.3153  409.0591  409.8028  410.5465  411.2903  412.034   412.7778  413.5215  414.2653  415.009   415.7528  416.4965  417.2402  417.984   418.7277  419.4715  420.2152  420.959  421.7027  422.4464  423.1902  423.9339  424.6777  425.4214  426.1652  426.9089  427.6527  428.3964  429.1401  429.8839  430.6276  431.3714  432.1151  432.8589  433.6026  434.3463  435.0901  435.8338  436.5776  437.3213  438.0651  438.8088  439.5526  440.2963  441.04    441.7838  442.5275  443.2713  444.015   444.7588  445.5025  446.2462  446.99  447.7337  448.4775  449.2212  449.965   450.7087  451.4525  452.1962  452.9399  453.6837  454.4274  455.1712  455.9149  456.6587  457.4024  458.1461  458.8899  459.6336  460.3774  461.1211  461.8649  462.6086  463.3524  464.0961  464.8398  465.5836  466.3273  467.0711  467.8148  468.5586  469.3023  470.046   470.7898  471.5335  472.2773  473.021  473.7648  474.5085  475.2523  475.996   476.7397  477.4835  478.2272  478.971   479.7147  480.4585  481.2022  481.9459  482.6897  483.4334  484.1772  484.9209  485.6647  486.4084  487.1522  487.8959  488.6396  489.3834  490.1271  490.8709  491.6146  492.3584  493.1021  493.8458  494.5896  495.3333  496.0771  496.8208  497.5646  498.3083  499.0521  499.7958  500.5395  501.2833  502.027   502.7708  503.5145  504.2583  505.002   505.7457  506.4895  507.2332  507.977   508.7207  509.4645  510.2082  510.952   511.6957  512.4394  513.1832  513.9269  514.6707  515.4144  516.1582  516.9019  517.6456  518.3894  519.1331  519.8769  520.6206  521.3644  522.1081  522.8519  523.5956  524.3393  525.0831  525.8268  526.5706  527.3143  528.0581  528.8018  529.5455  530.2893  531.033   531.7768  532.5205  533.2643  534.008   534.7518  535.4955  536.2392  536.983   537.7267  538.4705  539.2142  539.958   540.7017  541.4454  542.1892  542.9329  543.6767  544.4204  545.1642  545.9079  546.6517  547.3954  548.1391  548.8829  549.6266  550.3704  551.1141  551.8579  552.6016  553.3453  554.0891  554.8328  555.5766  556.3203  557.0641  557.8078  558.5516  559.2953  560.039   560.7828  561.5265  562.2703  563.014   563.7578  564.5015  565.2452  565.989   566.7327  567.4765  568.2202  568.964   569.7077  570.4515  571.1952  571.9389  572.6827  573.4264  574.1702  574.9139  575.6577  576.4014  577.1451  577.8889  578.6326  579.3764  580.1201  580.8639  581.6076  582.3514  583.0951  583.8388  584.5826  585.3263  586.0701  586.8138  587.5576  588.3013  589.045   589.7888  590.5325  591.2763  592.02    592.7638  593.5075  594.2513  594.995   595.7387  596.4825  597.2262  597.97  598.7137  599.4575  600.2012  600.9449  601.6887  602.4324  603.1762  603.9199  604.6637  605.4074  606.1512  606.8949  607.6386  608.3824  609.1261  609.8699  610.6136  611.3574  612.1011  612.8448  613.5886  614.3323  615.0761  615.8198  616.5636  617.3073  618.0511  618.7948  619.5385  620.2823  621.026   621.7698  622.5135  623.2573  624.001  624.7447  625.4885  626.2322  626.976   627.7197  628.4635  629.2072  629.951   630.6947  631.4384  632.1822  632.9259  633.6697  634.4134  635.1572  635.9009  636.6446  637.3884  638.1321  638.8759  639.6196  640.3634  641.1071  641.8509  642.5946  643.3383  644.0821  644.8258  645.5696  646.3133  647.0571  647.8008  648.5445  649.2883  650.032  650.7758  651.5195  652.2633  653.007   653.7508  654.4945  655.2382  655.982   656.7257  657.4695  658.2132  658.957   659.7007  660.4444  661.1882  661.9319  662.6757  663.4194  664.1632  664.9069  665.6507  666.3944  667.1381  667.8819  668.6256  669.3694  670.1131  670.8569  671.6006  672.3443  673.0881  673.8318  674.5756  675.3193  676.0631  676.8068  677.5506  678.2943  679.038   679.7818  680.5255  681.2693  682.013   682.7568  683.5005  684.2442  684.988   685.7317  686.4755  687.2192  687.963   688.7067  689.4505  690.1942  690.9379  691.6817  692.4254  693.1692  693.9129  694.6567  695.4004  696.1441  696.8879  697.6316  698.3754  699.1191  699.8629  700.6066  701.3504  702.0941  702.8378  703.5816  704.3253  705.0691  705.8128  706.5566  707.3003  708.044   708.7878  709.5315  710.2753  711.019   711.7628  712.5065  713.2503  713.994   714.7377  715.4815  716.2252  716.969   717.7127  718.4565  719.2002  719.9439  720.6877  721.4314  722.1752  722.9189  723.6627  724.4064  725.1502  725.8939  726.6376  727.3814  728.1251  728.8689  729.6126  730.3564  731.1001  731.8438  732.5876  733.3313  734.0751  734.8188  735.5626  736.3063  737.0501  737.7938  738.5375  739.2813  740.025   740.7688  741.5125  742.2563  743.    ]179983507.878Error inflection=132950348.197616100,000 hits/hour excepted at week 9.837964434790596Process finished with exit code 0
原创粉丝点击