如何用Python进行曲线拟合?

时间:2017-08-27 20:30:12

标签: python curve-fitting

您好我有一些数据,我想用函数f(x)= ax ^ b + c来拟合我的数据。但是Python无法实现我的数据。我尝试使用Matlab,它的工作原理R平方是0.96所以它没关系但是用Python它不起作用我不明白为什么......这是我的代码:

import numpy as np
import matplotlib.pyplot as plt
import re
from scipy.optimize import *
from numpy import linalg as LA
from sympy.abc import t,p
from time import gmtime, strftime


def f(x,a,b,c):
    return a*np.power(x,b)+c

p0 = [50000,-1,0]
x = np.array([127,128,129,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179,180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199,200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219,220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239,240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255,256,257,258,259,260,261,262,263,264,266,267,268,269,270,271,272,273,274,275,276,277,278,279,280,281,282,283,284,285,286,287,288,289,290,291,292,293,294,295,296,297,298,299,300,301,302,303,304,305,306,307,308,309,310,311,312,313,314,315,316,317,318,319,320,321,322,323,324,325,326,327,328,329,330,331,332,333,334,335,336,337,338,339,340,341,342,343,344,345,346,347,348,349,350,351,352,353,354,355,360,361,362,363,364,365,366,367,368,369,370,371,372,373,374,375,376,377,378,379,380,381,382,383,384,385,386,387,388,389,390,391,392,393,394,395,396,397,398,399,400,401,402,403,404,405,406,407,408,409,410,411,412,413,414,416,417,418,419,420,421,422,423,424,425,426,427,428,429,430,431,432,433,434,435,436,437,438,439,440,441,442,443,444,445,446,447,448,449,450,451,452,453,454,455,456,457,458,459,460,461,462,463,464,465,466,467,468,469,470,471,472,473,474,475,476,477,478,479,480,481,482,483,484,485,486,487,488,489,490,491,492,493,494,495,496,497,498,499,500,501,502,503,504,505,506,507,508,509,510,511,512,513,514,515,516,517,518,519,520,521,522,523,524,525,526,527,528,529,530,531,532,533,534,535,536,537,538,539,540,541,542,543,544,545,546,547,548,549,550,551,552,553,554,555,556,557,558,559,560,561,562,563,564,565,566,567,568,569,570,571,572,573,574,575,576,577,578,579,580,581,582,583,584,585,586,587,588,589,590,591,592,593,594,595,596,597,598,599,600,601,602,603,604,605,606,607,608,609,610,611,612,613,614,615,616,617,618,619,620,621,622,623,624,625,626,627,628,629,630,631,632,633,634,635,636,637,638,639,640,641,642,643,644,645,646,647,648,649,650,651,652,653,654,655,656,657,658,659,660,661,662,663,664,665,666,667,668,670,671,672,673,674,675,676,677,678,679,680,681,682,683,684,685,686,687,688,689,690,691,692,693,694,695,696,697,698,699,700,701,702,703,704,705,706,707,708,709,710,712,713,714,715,716,717,718,719,720,721,722,723,724,725,726,727,728,729,730,731,732,733,734,735,736,737,738,739,740,741,742,743,744,745,746,747,748,749,750,751,752,753,754,755,756,757,758,759,760,761,762,763,764,765,766,767,768,769,770,771,772,773,774,775,776,777,778,779,780,781,782,783,784,785,786,787,788,789,790,791,792,793,794,795,796,797,798,799,800,801,802,803,804,805,806,807,808,809,810,811,812,813,814,815,816,817,818,819,820,821,822,823,824,825,826,827,828,829,830,831,832,833,834,835,836,837,838,839,840,841,842,843,844,845,846,847,848,849,850,851,852,853,854,855,856,857,858,859,860,861,862,863,864,865,866,867,868,869,870,871,872,873,874,875,876,877,878,879,880,881,882,883,884,886,887,888,889,890,891,892,893,894,895,896,897,898,899,900,901,902,903,904,905,906,907,908,909,910,911,912,913,914,915,916,917,918,919,920,921,922,923,924,925,926,927,928,929,930,931,932,933,934,935,936,937,938,939,940,941,942,943,944,945,946,947,948,949,950,951,952,953,954,955])
y = np.array([7186.28815965,7154.78265351,7117.45198193,7099.1728188,7083.95520592,7058.86362205,7058.38538758,7033.4243552,7017.44863198,6986.06622422,6987.9777083,6979.6630753,6977.16180162,6952.48776263,6939.80751078,6926.31732995,6915.06872784,6916.22001762,6904.8557658,6876.26428055,6872.84282573,6863.70165813,6862.93481312,6847.99484216,6843.71310766,6821.2417199,6818.47082288,6811.97265535,6807.96008056,6795.45220452,6777.43639859,6770.02551556,6765.0868828,6746.3142094,6743.29666971,6729.04474884,6719.00049143,6649.26932573,6652.31780701,6636.60455432,6637.27941273,6632.14402149,6689.24171528,6662.52078927,6641.62560426,6636.47494619,6622.07015958,6615.48840094,6605.00318143,6601.57433875,6592.87067031,6580.4065904,6575.38528388,6565.6764465,6552.17197384,6534.89902845,6502.06442288,6504.10625255,6481.95094639,6479.3123256,6477.43747537,6463.76962729,6452.74779229,6446.84950922,6435.09478481,6434.91138965,6418.0377104,6409.93671594,6402.15406995,6391.81601104,6386.70970781,6376.61685653,6362.41730044,6349.6749501,6345.83866709,6339.55654734,6326.63655392,6329.75321073,6312.77561055,6306.9492064,6300.49481469,6290.31342984,6282.6300932,6274.50738147,6266.05616906,6253.63346382,6242.79744966,6230.44723075,6234.37902932,6225.2170746,6211.53693456,6214.27792662,6216.22337481,6198.5678646,6189.56056879,6182.10925325,6174.22289838,6167.53436422,6153.42826464,6147.7379431,6143.1699557,6135.27748012,6126.53601682,6116.95959128,6093.48505582,6103.23759586,6098.19700506,6088.91546017,6080.34351597,6072.57431383,6052.74543528,6055.22931417,6053.22010518,6041.94716061,6031.25588183,6023.66965102,6017.95062001,6018.47847865,6012.89896723,5990.49395559,5974.99907908,5970.23761567,5966.63318284,5959.90224736,5956.21804813,5953.08591225,5915.20456204,5913.81909664,5925.55354002,5918.12528277,5911.82765366,5901.57344567,5893.29068082,5881.44153764,5874.77678441,5876.93243761,5877.32436533,5865.68563462,5888.24535741,5843.80280703,5826.56104767,5809.78849152,5819.46167637,5813.08798108,5807.56730674,5784.58125065,5794.17333875,5778.33328154,5774.84230133,5752.10663286,5752.55989811,5746.05733166,5743.28351636,5708.7168994,5726.77095964,5714.44300198,5699.96950588,5697.57952411,5708.72037591,5685.28182295,5673.8267214,5662.01384232,5672.99864475,5666.45482047,5647.15184727,5643.17737808,5627.27229693,5637.50945911,5609.54021693,5616.35652726,5623.33712557,5586.40831385,5578.79847959,5570.43490491,5580.29454478,5575.88658026,5544.33402558,5560.20445367,5525.61205519,5544.22317332,5527.9329834,5515.96687224,5493.91239814,5481.98453713,5470.90550409,5468.72952244,5485.3276454,5463.30721524,5458.05374669,5458.35652315,5473.32537333,5445.87716817,5449.97747436,5433.51511017,5420.79201587,5412.2684232,5421.22563343,5408.75860244,5411.10926288,5412.76537401,5393.71400508,5386.48561337,5354.33498003,5368.59663282,5363.66600885,5338.00291373,5345.40171289,5341.68044268,5326.82653569,5320.85786046,5321.71099421,5305.58391841,5308.60418955,5287.37181127,5298.48019573,5148.17702674,5051.31663321,5253.99083714,5254.85307096,5249.11643539,5245.41653956,5234.50191796,5227.10990525,5222.76079999,5214.62877333,5221.14569815,5193.03033547,5057.56812197,5171.07236742,5160.08725432,5153.00645537,5151.60277262,5132.3939813,5132.59403915,5075.27719389,5127.61940598,5106.67712043,5106.35352714,5086.83904332,4992.38119525,5092.13773483,5059.30718553,5066.97292388,5054.16357424,5040.44816139,5039.88954646,5031.37479547,5039.04651543,5026.08360051,5017.69382496,5011.84431225,5008.21904143,5001.61203703,4987.43855247,4992.4590303,4988.38945206,4977.59541582,4973.57273326,4955.67008723,4949.25922258,4956.40957259,4930.83641852,4805.10472968,4929.44543268,4919.82753519,4915.1242332,4902.11439305,4910.22042701,4894.73615902,4877.64905341,4878.42982569,4870.61035526,4869.89184534,4846.15509747,4858.20598546,4832.20040066,4832.81784181,4834.89095005,4825.28078283,4817.31541601,4821.14876054,4656.29584195,4800.67420849,4636.61254414,4703.52992612,4575.95972683,4812.80859881,4771.19454286,4714.52157875,4743.39897477,4743.80402997,4726.30832586,4728.15619816,4728.54921466,4719.1046177,4710.04567656,4701.31190522,4705.07050568,4695.50051803,4688.86661059,4691.08147367,4672.95415979,4660.05546646,4662.15072743,4642.88613076,4650.70720425,4646.14889066,4637.23761822,4636.72193751,4627.80444427,4613.27881484,4612.67969154,4609.69388463,4604.13505231,4590.03304202,4587.045826,4576.53714251,4585.4339942,4573.80203756,4570.17850777,4560.28913642,4555.1090548,4537.56111195,4534.82531005,4524.63149832,4526.37019973,4518.048489,4515.15179863,4505.43012663,4499.92226499,4491.51937321,4473.29183745,4462.31451468,4475.11104097,4469.475381,4459.31704031,4464.98908457,4443.18399672,4440.94009053,4439.48811523,4414.79859512,4402.77710434,4412.28022275,4395.96762735,4388.97026544,4378.16687581,4367.27515683,4364.60253104,4362.76577479,4353.54083899,4348.74621504,4342.66162376,4332.65576187,4329.67034691,4306.135431,4311.14492129,4309.19937609,4302.44608366,4281.10111919,4287.57964452,4285.59590624,4273.81351763,4266.88943466,4272.00757938,4276.77467929,4265.71122149,4261.65876225,4257.60644574,4246.09060266,4236.72354896,4211.10028686,4199.53650461,4219.81670357,4199.88506352,4188.26709521,4188.57639917,4178.61712409,4182.47966196,4178.87931634,4180.82055955,4195.82396392,4162.46889191,4140.92897508,4152.91163171,4145.74944947,4137.28192559,4135.51382192,4124.78190724,4113.05095684,4107.26865411,4088.30740697,4089.33370155,4086.24219839,4073.95490185,4073.65607385,4073.74162868,4069.13861378,4063.86364454,4053.85461345,4043.54007332,4038.51461857,4031.52226856,4020.04731182,4017.89746527,4013.48075237,4002.33850478,4001.95147835,3990.90971692,3990.44071212,3979.21939813,3971.34987014,3972.24819083,3958.07456738,3960.3705582,3955.9635094,3927.9589284,3910.75998486,3908.21238793,3908.88242032,3892.68568786,3902.60766876,3905.90690514,3885.4564197,3890.22510653,3883.68493032,3875.33512279,3861.32552378,3867.43230089,3856.06831211,3850.66643915,3844.80924414,3838.10903129,3831.13887999,3831.34979894,3833.89485625,3799.98141962,3788.85053663,3777.45764247,3771.33041638,3765.19614606,3774.06563343,3772.95444231,3771.84398173,3756.98985074,3742.13730496,3746.90689008,3737.10710191,3737.67260689,3719.51662328,3729.14000286,3699.9223425,3707.85978477,3698.97561143,3691.25156249,3680.17122763,3675.48259937,3672.4014837,3663.94990701,3649.47046103,3652.72118849,3646.38021405,3639.1188034,3628.35896678,3633.91561173,3625.27856936,3621.20692067,3609.8149343,3598.68442135,3592.72198996,3586.58608738,3600.74241382,3573.44576173,3596.16190914,3599.20352911,3601.79541464,3610.73867914,3550.38439126,3548.14394496,3542.96173259,3523.29683664,3523.38744072,3510.6411302,3505.31095645,3492.86894317,3492.36071128,3470.47214987,3473.33808473,3471.72287454,3474.18213262,3480.75407123,3467.53616987,3455.22459205,3453.32427731,3458.10309575,3436.18822023,3426.8315031,3428.19876443,3421.19309622,3415.50624324,3409.93107637,3403.36165485,3397.05112335,3390.49340816,3384.55177454,3384.91756828,3377.47188971,3377.15872803,3360.31659985,3349.31649482,3339.54343206,3325.89678163,3324.27280951,3330.7860754,3328.20854399,3316.80037546,3309.80752794,3308.02479947,3309.4370981,3301.46229405,3294.14041162,3287.43694455,3279.39471179,3269.8277426,3267.65771981,3264.17032676,3259.10718839,3244.06302893,3241.09389702,3240.07422418,3232.1096143,3225.10775212,3269.97006359,3231.91341002,3213.87438383,3205.16333094,3196.05419104,3402.92793325,3284.99455582,3240.02772281,3221.16594017,3201.72879765,3191.90245683,3170.37148564,3167.17514827,3161.38186767,3149.21527292,3137.9030212,3136.06741598,3115.90882371,3105.34171919,3105.99981897,3102.11708676,3095.01850843,3099.07948013,3083.47128813,3071.73446391,3062.81662471,3053.77353074,3044.95822253,3036.74387624,3033.62221771,3030.89192523,3026.90556108,3025.62486605,3015.88541154,3020.11214616,3011.01947566,3009.91190718,3001.13960666,2990.09300882,2987.69508553,2979.91493047,2975.5038606,2964.03297571,2961.24187721,2956.69719893,2947.59724454,2560.80861778,2523.22830776,2543.90230677,2550.40465103,2541.68871201,2533.62128802,2536.77457058,2520.04443928,2517.24990324,2508.2514329,2508.00955238,2504.00345911,2513.79417256,2510.51473836,2505.63586827,2499.63839806,2494.85171309,2489.74131018,2485.25236047,2480.37113559,2474.86031766,2469.81185283,2462.8252445,2457.59361855,2453.31383135,2448.72579706,2443.98274559,2438.97625983,2434.76675767,2432.19150956,2425.51268405,2420.3003231,2415.04877607,2411.71703733,2407.40643454,2400.260803,2396.11146606,2391.25486827,2387.00379032,2382.44617847,2377.53657594,2373.46594437,2368.41643801,2363.39004013,2358.97724125,2353.58311434,2349.58871203,2345.97831436,2338.15526155,2338.00356726,2333.95074432,2330.52866738,2324.55576687,2322.43891634,2317.54887542,2313.35439194,2308.13218063,2303.29915907,2299.54060251,2296.35235215,2291.03028093,2286.97249494,2283.80123543,2281.97476711,2276.474223,2273.282917,2268.17039265,2268.80080145,2264.32613925,2264.15704905,2259.90862055,2255.92776596,2253.87928928,2249.51570657,2246.42488629,2239.92463535,2236.71196251,2232.30213469,2227.95478738,2224.09227943,2218.77946831,2215.18066083,2204.19121143,2203.15420913,2200.65085495,2196.05167198,2191.84701886,2187.76528673,2180.5161262,2175.25391791,2170.51006796,2170.41297394,2171.03791928,2162.76916919,2158.22307756,2153.25811919,2147.95031431,2143.27903246,2140.50450263,2141.50626356,2143.55501026,2133.61616191,2128.98339068,2275.46195778,2327.03110455,2211.50175657,2168.19214706,2145.64666641,2131.79096174,2152.11851882,2132.18676133,2117.94796939,2116.15994134,2105.68801572,2099.58212652,2093.58045627,2064.40492162,2081.84936789,2075.51306501,2072.72861158,2065.15599435,2058.19654763,2051.64733002,2048.28641459,2045.70407184,2047.0305995,2044.65910872,2040.12828049,2035.34571387,2033.47199316,2030.83746854,2025.87265162,2019.62468273,2020.70482427,2016.94022174,2019.58908349,2012.22732886,2010.40198478,2000.55678336,1998.39506726,2000.70685011,1998.10792001,1994.30892857,1990.22864465,1986.85989109,1985.74748979,1982.20989486,1980.80326289,1979.80002961,1976.81623781,1969.53746464,1964.41703607,1960.22128914,1960.07757789,1956.59269655,1952.40679673,1950.88487208,1949.94954555,1947.07432152,1940.49774395,1949.28311344,1939.79840106,1936.11565583,1931.39024907,1929.57017134,1930.47858771,1929.06046371,1929.95517759,1926.75956523,1922.02020336,1917.5095819,1915.14917397,1911.04922758,1900.57105851,2942.21513255,2938.10552655,2929.70402465,2923.23943902,2924.84731203,2916.50427613,2912.42339911,2897.97388391,2890.07517815,2898.76262706,2883.70371258,2878.97319957,2839.24998631,2867.8586234,2863.71187814,2853.18356222,2851.2837037,2844.44359041,2842.77705724,2835.02971512,2823.92383159,2812.93403698,2818.1951303,2809.8472936,2805.66401711,2798.20491174,3010.31903168,2789.3540143,2784.06715693,2771.45649241,2770.735161,2767.72175039,2754.25623751,2757.83799024,2750.71233415,2747.71652216,2738.6794026,2730.79703204,2720.87707753,2722.23269382,2722.25350532,2711.18172163,2709.30210288,2702.83981108,2698.98735279,2685.37009922,2680.45108488,2678.09774253,2675.93461869,2669.92069148,2666.38049092,2656.04325512,2658.39710498,2647.69132096,2640.72194902,2642.39889036,2638.21547879,2624.16637709,2621.21317287,2625.91693115,2612.79603096,2606.46462447,2610.4152313,2589.97270498,2594.07360437,2584.88374126,2580.54581022,2573.94377369,2568.8068692])
params, cov = curve_fit(f, x, y, p0)

plt.plot(x,y, 'o')
plt.plot(x, f(x, params[0], params[1], params[2]))
plt.legend(['data','fit'],loc='best')
plt.show()

感谢您的帮助!

0 个答案:

没有答案