program DragonCurve_51_Py_Turtle_JS_Sound; //https://rosettacode.org/wiki/Dragon_curve#Delphi //https://rosettacode.org/wiki/Brazilian_numbers#Pascal //https://docs.perplexity.ai/docs/getting-started //https://dev.to/max_kleiner_9d12e786b3ecc/dragon-curve-39fd //{$IFDEF Lazarus} // {$APPTYPE CONSOLE} ☮ ✞ π 📌 //{/$ENDIF} (* {$IFDEF FPC} {$MODE DELPHI}{$OPTIMIZATION ON,All} {$CODEALIGN proc=32,loop=4} {$ELSE} {$APPTYPE CONSOLE} {$ENDIF} uses SysUtils; *) //const var p: TColor; _sin: array of double; //TArray; _cos: array of double; //TArray; s: double; b: TBitmap; FAsBitmap: TBitmap; const Sep = 512; Depth = 11; //default:14 or 9 DRAGON_JS = ' '+LF+ ' '+LF+ ' '+LF+ ' '+LF+ ' '+LF+ ' '+LF+ '

Please input order, scale, x-shift, y-shift, color:

'+LF+ ' '+LF+ ' '+LF+ ' '+LF+ ' '+LF+ ' '+LF+ ' '+LF+ '

Dragon curve

'+LF+ ' '+LF+ ' '+LF+ ' '; procedure Dragon(n, a, t: Integer; d, x, y: Double; var b: TBitmap); forward; //public //constructor Create; //destructor Destroy; override; //property AsBitmap: TBitmap read b; //end; //Proc FillWord( var Dest, Count :Card; Filler : Word) procedure fillword2(var Dest, Count :Cardinal; Filler : Word ); begin fillword(dest, count, filler) end; procedure Bmp2Jpeg_(const BmpFileName, JpgFileName: string); var Bmp: TBitmap; Jpg: TJPEGImage; begin Bmp := TBitmap.Create; Jpg := TJPEGImage.Create; try Bmp.LoadFromFile(BmpFileName); Jpg.Assign(Bmp); Jpg.SaveToFile(JpgFileName); finally Jpg.Free; Bmp.Free; end; end; procedure Jpeg2Bmp(const BmpFileName, JpgFileName: string); var Bmp: TBitmap; Jpg: TJPEGImage; begin Bmp := TBitmap.Create; Jpg := TJPEGImage.Create; try Jpg.LoadFromFile(JpgFileName); Bmp.Assign(Jpg); Bmp.SaveToFile(BmpFileName); finally Jpg.Free; Bmp.Free; end; end; procedure Dragon(n, a, t: Integer; d, x, y: Double; var b: TBitmap); var a1, a2: integer; begin if n <= 1 then begin with b.Canvas do begin Pen.Color := cllime; //random(p); MoveTo(Trunc(x + 0.5), Trunc(y + 0.5)); LineTo(Trunc(x + d * _cos[a] + 0.5), Trunc(y + d * _sin[a] + 0.5)); exit; end; end; d := d * s; a1 := (a - t) and 7; a2 := (a + t) and 7; dragon(n - 1, a1, 1, d, x, y, b); dragon(n - 1, a2, -1, d, x + d * _cos[a1], y + d * _sin[a1], b); end; procedure CreateDragon; var width, height: integer; begin s := sqrt(2) / 2; setlength(_sin, 7) setlength(_cos, 7) _sin:= [0, s, 1, s, 0, -s, -1, -s]; _cos:= [1.0, s, 0.0, -s, -1.0, -s, 0.0, s]; p:= Rgb(64, 192, 96); b:= TBitmap.create; width:= Trunc(Sep * 11 / 6); height:= Trunc(Sep * 4 / 3); b.SetSize(width, height); with b.Canvas do begin Brush.Color:= (clBlack); Pen.Width:= 2; FillRect(Rect(0, 0, width, height)); end; dragon(Depth, 0, 1, sep, sep / 2, sep * 5 / 6, b); //b.savetofile(exepath+'examples\dragon51.bmp'); with TJPEGImage.Create do begin Assign(b); SaveToFile(exepath+'examples\dragon511.jpg'); free; end; openfile(exepath+'examples\dragon511.jpg'); b.free; end; Const DRAGFUNC = 'def dragon(level=4, size=200, direction=45): '+LF+ ' if level: '+LF+ ' right(direction) '+LF+ ' dragon(level-1, size/1.41421356237, 45) '+LF+ ' left(direction * 2) '+LF+ ' dragon(level-1, size/1.41421356237, -45) '+LF+ ' right(direction) '+LF+ ' else: '+LF+ ' forward(size) '; function PyCodeDragonTurtle(imgpath, aAPIKey: string): string; begin with TPythonEngine.Create(Nil) do begin //pythonhome:= 'C:\Users\User\AppData\Local\Programs\Python\Python312\'; try loadDLL; autofinalize:= false; ExecString('from turtle import right,left,forward,speed, exitonclick, hideturtle'); ExecStr(DRAGFUNC); ExecStr('speed(0)'); //ExecStr('hideturtle()'); ExecStr('dragon(6)'); ExecStr('exitonclick()'); //result:= (EvalStr('r.json()')); *) except raiseError; finally Free; end; end; end; var cFrm: TForm; procedure LoadTForm; begin cFrm:= TForm.create(self); try with cFrm do begin caption:= 'Dragon Turtle Sound ((((*))))'; icon.loadfromresourcename(hinstance, 'YINYANG'); height:= 800; width:= 900; color:= clblack; //clwhite Position:= poScreenCenter; show; //canvas.assign(myturtle.canvas) end; Application.processMessages; except Exit; end end; //https://rosettacode.org/wiki/Dragon_curve#Pascal var myturtle: TJvTurtle; procedure dcr_(step, adir:integer;leng:real); //var myturtle: TJvTurtle; begin; //myturtle:= TJvTurtle.create(self); step:=step -1; leng:= leng / sqrt(2); with myturtle do begin canvas:= cFrm.canvas; canvas.Pen.Color:= clgreen; penDown:= true; penWidth:= 10; setpos(300,300); //movebackward(100); 45-90-45 if adir > 0 then begin if step > 0 then begin //with myturtle do begin //turn(45); right(45*adir) dcr_(step,1,leng); //turn(90) left(90*adir); dcr_(step,0,leng); //turn(45) right(45*adir); end else begin //turn(45) right(45*adir); moveforward(leng); //turn(90) left(90*adir); moveforward(leng); //turn(45) right(45*adir); end; end else begin if step > 0 then begin //turn(45) left(45*adir); dcr_(step,1,leng); //turn(90) right(90*adir); dcr_(step,0,leng); //turn(45) left(45*adir); end else begin //turn(45) left(45*adir); moveforward(leng); //turn(90) right(90*adir); moveforward(leng); //turn(45) left(45*adir); end; end; end; //myturtle.free; end; procedure DrawDragon(step, adir: integer; leng:real); begin //myturtle:= TJvTurtle.create(self); //step:=step -1; with myturtle do begin if (step > -1) and (leng > 1) then begin leng:= leng/sqrt(2); Turn(45*adir); DrawDragon(step-1, +1, leng); Turn(-90*adir); DrawDragon(step-1, -1, leng); Turn(45*adir); //end end else //*) //Turtle.Draw(Laenge); moveforward(leng) end; end; var cn: integer; procedure DrawDragon2(step, adir: integer; len:real); begin //myturtle:= TJvTurtle.create(self); with myturtle do begin if (step >-1) and (len >1) then begin len:= len /sqrt(2); Turn(45*adir); //beep2(440+45, 150) //beep2(440-90, 550) DrawDragon2(step-1, +1, len); Turn(-90*adir); //beep2(440-90*adir, 450) //PlayResWav2( 'KAMMERTON', 'WAV'); //writeln(itoa(adir)); DrawDragon2(step-1, -1, len); Turn(45*adir); //canvas.Pen.Color:= random(clwhite); //beep2(440+45, 550) end else begin //*) moveforward(len ) //beep2(440, 100) //beep2(440-90*adir, 450) if (adir= -1) and (cn mod 210=1) then begin canvas.Pen.Color:= random(clwhite); if cn mod 1190=1 then //beep2(440-random(110), 950) } //PlaySound('INDIGO_WAV', hInstance, SND_RESOURCE or SND_ASYNC); playreswav('DRUMLOOP','WAV'); end; inc(cn) end; end; end; var T1, T0: TDateTime; i, n, cnt, lmt: NativeUInt; Begin //@main {backstr:= parseJsonvalue(GEO_to_text_API2_GEO(URL_APILAY_GEO, 'Vienna', L_APIKEY)); validtext:= StringReplace( backstr, '\n', CR+LF,[rfReplaceAll]); writeln(validtext); //} //writeln(formatJSON(GEO_to_text_API2_GEO(URL_APILAY_GEO, 'Athen', L_APIKEY))); { t_latlong:= TAddressGeoCodeOSM8(URL_APILAY_GEO, 'Athen', L_APIKEY); writ(jsonunescape(t_latlong.descript,#13#10)); // } { backstr:= parseJsonvalue(GEO_to_text_API2_emoji(URL_APILAY_EMO, 'slightly smiling face', N_APIKEY)); validtext:= StringReplace( backstr, '\n', CR+LF,[rfReplaceAll]); writeln(validtext); // U+1F642 } writeln(#$d83d#$de42); (* lmt := AMAX; setlength(isPrime, lmt + 1); MarkSmallestFactor; First20Brazilian; First33OddBrazilian; First20BrazilianPrimes; *) //CreateDragon(); //PyCodeDragonTurtle('',''); LoadTForm(); //dcr_(7,1,1500); myturtle:= TJvTurtle.create(self); //dcr_(7,1,1500); PlaySound('INDIGO_WAV', 0, SND_RESOURCE or SND_ASYNC); myturtle.canvas:= cFrm.canvas; myturtle.canvas.Pen.Color:= cllime; myturtle.penDown:= true; myturtle.penWidth:= 2; myturtle.setpos(220,400); cn:= 0; drawDragon2(15,1,500); //11 as default writeln('steps forward: '+itoa(cn)); myturtle.free; //Bmp2Jpeg (* with TEdgeViewForm.create(self) do begin width:= 1200; height:=1000; PageControl1.height:= 80; icon.loadfromresourcename(hinstance,'XJICON'); Caption:= 'maXbox5 EdgeView2Browser_DragonPlot' //edit1.text:= 'https://docwiki.embarcadero.com/Libraries/Sydney/en/System.RegularExpressions'; sleep(800); NavigatetoString(DRAGON_JS); //memoHTML.Text:= ARCHImedes; sleep(400); //btnSetSourceClick(self); showmodal; free; end; *) End. doc: https://dev.to/max_kleiner_9d12e786b3ecc/dragon-curve-39fd https://dev.to/max_kleiner_9d12e786b3ecc/dragon-curve-2-gfc https://maxbox5.wordpress.com/2024/08/07/dragon-curve/ Testing cases: Input parameters: ord scale x-shift y-shift color [File name to save] ------------------------------------------- 11 7. -265 -260 red DC11.png 15 2. -205 -230 brown DC15.png 17 1. -135 70 green DC17.png 19 0.6 380 440 navy DC19.png 21 0.22 1600 800 blue DC21.png 23 0.15 1100 800 violet DC23.png 25 0.07 2100 5400 darkgreen DC25.png ========================================== ref: debug: 615- 4294967295 err:0 🙂 high 110468 first 20 brazilian numbers 7 = 7 111 to base 2 8 = 2^3 22 to base 3 10 = 2*5 22 to base 4 12 = 2^2*3 22 to base 5 13 = 13 111 to base 3 14 = 2*7 22 to base 6 15 = 3*5 33 to base 4 16 = 2^4 22 to base 7 18 = 2*3^2 22 to base 8 20 = 2^2*5 22 to base 9 21 = 3*7 33 to base 6 22 = 2*11 22 to base 10 24 = 2^3*3 22 to base 11 26 = 2*13 22 to base 12 27 = 3^3 33 to base 8 28 = 2^2*7 22 to base 13 30 = 2*3*5 22 to base 14 31 = 31 11111 to base 2 32 = 2^5 22 to base 15 33 = 3*11 33 to base 10 first 33 odd brazilian numbers 7 = 7 111 to base 2 13 = 13 111 to base 3 15 = 3*5 33 to base 4 21 = 3*7 33 to base 6 27 = 3^3 33 to base 8 31 = 31 11111 to base 2 33 = 3*11 33 to base 10 35 = 5*7 55 to base 6 39 = 3*13 33 to base 12 43 = 43 111 to base 6 45 = 3^2*5 33 to base 14 51 = 3*17 33 to base 16 55 = 5*11 55 to base 10 57 = 3*19 33 to base 18 63 = 3^2*7 33 to base 20 65 = 5*13 55 to base 12 69 = 3*23 33 to base 22 73 = 73 111 to base 8 75 = 3*5^2 33 to base 24 77 = 7*11 77 to base 10 81 = 3^4 33 to base 26 85 = 5*17 55 to base 16 87 = 3*29 33 to base 28 91 = 7*13 77 to base 12 93 = 3*31 33 to base 30 95 = 5*19 55 to base 18 99 = 3^2*11 33 to base 32 105 = 3*5*7 33 to base 34 111 = 3*37 33 to base 36 115 = 5*23 55 to base 22 117 = 3^2*13 33 to base 38 119 = 7*17 77 to base 16 121 = 11^2 11111 to base 3 first 20 brazilian prime numbers 7 = 7 111 to base 2 13 = 13 111 to base 3 31 = 31 11111 to base 2 43 = 43 111 to base 6 73 = 73 111 to base 8 127 = 127 1111111 to base 2 157 = 157 111 to base 12 211 = 211 111 to base 14 241 = 241 111 to base 15 307 = 307 111 to base 17 421 = 421 111 to base 20 463 = 463 111 to base 21 601 = 601 111 to base 24 757 = 757 111 to base 27 1093 = 1093 1111111 to base 3 1123 = 1123 111 to base 33 1483 = 1483 111 to base 38 1723 = 1723 111 to base 41 2551 = 2551 111 to base 50 2801 = 2801 11111 to base 7  mX5🐞 executed: 05/08/2024 21:17:43 Runtime: 0:0:8.753 Memload: 72% use ref: https://www.blopig.com/blog/2017/07/using-random-forests-in-python-with-scikit-learn/ C:\Program Files\Git>cd cmd C:\Program Files\Git\cmd>py -3.12 -m pip install git+https://github.com/nathanrchn/perplexityai.git Collecting git+https://github.com/nathanrchn/perplexityai.git Cloning https://github.com/nathanrchn/perplexityai.git to c:\users\user\appdata\local\temp\pip-req-build-g92q2pi2 Running command git clone --filter=blob:none --quiet https://github.com/nathanrchn/perplexityai.git 'C:\Users\User\AppData\Local\Temp\pip-req-build-g92q2pi2' Resolved https://github.com/nathanrchn/perplexityai.git to commit d61c40b109a4614dea12ec91bed580cc64bc791c Installing build dependencies ... done Getting requirements to build wheel ... error error: subprocess-exited-with-error Exception: IndentationError: unexpected indent (line 1, offset 1): 'def automate_file_management(directory_path):' at 882.1918 × Getting requirements to build wheel did not run successfully. │ exit code: 1 ╰─> [34 lines of output] Traceback (most recent call last): File "C:\Users\User\AppData\Local\Programs\Python\Python312\Lib\site-packages\pip\_vendor\pyproject_hooks\_in_process\_in_process.py", line 353, in main() Ref: http://www.rolandmuseum.de/syn_ana.php https://www.coderstool.com/unicode-text-converter Let's explore some examples to understand how non-capturing groups can be used in regular expressions: Example 1: Matching URLs Suppose you want to match URLs in a text, but you don't want to capture the protocol (e.g., http or https). You can use a non-capturing group to accomplish this: /https?:\/\/(?:www\.)?example\.com/ In this example, the non-capturing group (?:www\.)? matches an optional 'www.' prefix, and the entire group is followed by 'example.com'. The result is a pattern that matches URLs like 'http://example.com' or 'https://www.example.com', without capturing the protocol. While maXbox5 appears to be a versatile scripting tool, there's no evidence in the given search results that it supports languages other than Object Pascal. The focus seems to be on providing a robust environment for Object Pascal scripting rather than offering multiple language options. In summary, maXbox5's bytecode interpretation system is a core component of its scripting engine, providing a balance between performance, cross-platform compatibility, and extensive library support. The bytecode interpreter allows maXbox5 to execute complex scripts efficiently while maintaining a high degree of flexibility and functionality across different operating systems and environments. Answer2similarity: 0.918471693992615 https://api.api-ninjas.com/v1/qrcode?format=png&data=maXbox5 [ fact": "Tobacco kills more Americans each year than alcohol, cocaine, crack, heroin, homicide, suicide, car accidents, fire and AIDS combined"}] True [ fact": "The size of a red blood cell is 708 microns. This is equivalent to one millionth of a meter"}] True [ fact": "A superstition in baseball is to never lend your bat to anyone or you will be jinxed"}] True fact": "In 1886, Coca-cola was first served at a pharmacy in Atlanta, Georgia for only five cents a glass. A pharmacist named John Pemberton created the formula for Coca-cola"}] fact": "The song with the longest title is \"I?m a Cranky Old Yank in a Clanky Old Tank on the Streets of Yokohama with my Honolulu Mama Doin? Those Beat-o, Beat-o Flat-On-My-Seat-o, Hirohito Blues\" written by Hoagy Carmichael in 1945. He later claimed the song title ended with ?Yank? and the rest was a joke"}] perpl res: debug: 119-The requested header could not be located. 854 err:20 Status3: debug: 120- 854 err:21 Null Pointer Exception at 854.1916 https://medium.com/@abhay.anand/day-3-linux-commands-you-should-know-65fb849561cc https://medium.com/100-days-of-linux/7-fundamental-use-cases-of-netcat-866364eb1742 https://medium.com/@saravra/how-to-study-cybersecurity-64ee0478e87e New in maXbox 5.1.6.98 (July 25, 2024) New in maXbox 5.1.4.98 (June 30, 2024) New in maXbox 5.1.4.95 (April 17, 2024) New in maXbox 5.1.4.90 (March 21, 2024) New in maXbox 4.7.6.50 (June 15, 2023) New in maXbox 4.7.6.20 (December 15, 2022) New in maXbox 4.7.6.10 (November 12, 2021) New in maXbox 4.7.5.80 (July 22, 2021) New in maXbox 4.7.4.64 (June 14, 2020) New in maXbox 4.7.4.62 (June 8, 2020) New in maXbox 4.7.4.60 (April 21, 2020) New in maXbox 4.7.3.60 (April 15, 2020) New in maXbox 4.7.2.82 (April 7, 2020) New in maXbox 4.7.1 (November 15, 2019) New in maXbox 4.6.3.10 (August 6, 2019) New in maXbox 4.5.8.10 (December 27, 2017) New in maXbox 4.2.8.10 (October 24, 2017) New in maXbox 4.2.6.10 (August 24, 2017) New in maXbox 4.2.5.10 (February 20, 2017) New in maXbox 4.2.4.80 (October 21, 2016) New in maXbox 4.2.4.60 (September 30, 2016) New in maXbox 4.2.4.25 (June 8, 2016) New in maXbox 4.2.2.90 (April 25, 2016) New in maXbox 4.2.0.80 (April 15, 2016) New in maXbox 4.0.2.60 (February 2, 2016) New in maXbox 3.9.9.195 (May 19, 2015) New in maXbox 3.9.9.190 (March 20, 2015) New in maXbox 3.9.9.180 (February 12, 2015) New in maXbox 3.9.9.160 (January 14, 2015) New in maXbox 3.9.9.120 (November 29, 2014) New in maXbox 3.9.9.110 (November 12, 2014) New in maXbox 3.9.9.101 (October 25, 2014) New in maXbox 3.9.9.100 (October 10, 2014) New in maXbox 3.9.9.98 (July 25, 2014) New in maXbox 3.9.9.96 (May 15, 2014) New in maXbox 3.9.9.95 (April 24, 2014) New in maXbox 3.9.9.94 (March 28, 2014) New in maXbox 3.9.9.92 (March 20, 2014) New in maXbox 3.9.9.91 (March 12, 2014) New in maXbox 3.9.9.88 (February 10, 2014) New in maXbox 3.9.9.86 (January 27, 2014) New in maXbox 3.9.9.85 (January 15, 2014) New in maXbox 3.9.9.84 (January 6, 2014) New in maXbox 3.9.9.82 (December 16, 2013) New in maXbox 3.9.9.81 (December 3, 2013) New in maXbox 3.9.9.80 (November 11, 2013) New in maXbox 3.9.9.60 (October 21, 2013) New in maXbox 3.9.9.20 (October 9, 2013) New in maXbox 3.9.9.18 (September 30, 2013) New in maXbox 3.9.9.16 (September 17, 2013) New in maXbox 3.9.9.8 (September 13, 2013) New in maXbox 3.9.9.7 (August 27, 2013) New in maXbox 3.9.9.6 (August 7, 2013) New in maXbox 3.9.9.5 (July 24, 2013) New in maXbox 3.9.9.1 (June 24, 2013) New in maXbox 3.9.8.9 (June 10, 2013) New in maXbox 3.9.8.8 (May 13, 2013) New in maXbox 3.9.8.6 (April 10, 2013) New in maXbox 3.9.8.3 (March 18, 2013) New in maXbox 3.9.8 (February 28, 2013) New in maXbox 3.9.7.4 (January 22, 2013) New in maXbox 3.9.7.1 (January 22, 2013) New in maXbox 3.9.7 (December 3, 2012) New in maXbox 3.9.6.3 (November 14, 2012) New in maXbox 3.9.4.4 (October 16, 2012) New in maXbox 3.9.3.5 (September 26, 2012) New in maXbox 3.9.2 (August 31, 2012) New in maXbox 3.9.1 (June 14, 2012) New in maXbox 3.9.0 (May 23, 2012) New in maXbox 3.8.6.4 (May 11, 2012) New in maXbox 3.8.6 (April 20, 2012) New in maXbox 3.8.5 (April 3, 2012) New in maXbox 3.8.4 (March 2, 2012) New in maXbox 3.8.2 (January 20, 2012) New in maXbox 3.8.1 (January 3, 2012) New in maXbox 3.8.0.9 (December 20, 2011) New in maXbox 3.7.1 (December 6, 2011) New in maXbox 3.7.0.2 (November 9, 2011) New in maXbox 3.6.3.0 (November 3, 2011) New in maXbox 3.6.2.0 (October 22, 2011) New in maXbox 3.6.1.2 (October 12, 2011) New in maXbox 3.6.0.2 (October 3, 2011) New in maXbox 3.5.0.6 (September 1, 2011) New in maXbox 3.3.0.4 (June 27, 2011) New in maXbox 3.2.0 (March 25, 2011) New in maXbox 3.1.0 (February 24, 2011) New in maXbox 3.0.0 (December 20, 2010) New in maXbox 2.9.2 (July 23, 2010) New in maXbox 2.9 (June 3, 2010) Zeus64 commented on 18 May 2020 C:\Users\Public\Documents\Embarcadero\Studio\21.0\Samples\Object Pascal\VCL\InetWinSockets 64bit build procedure TDynamicDll.DoOpenDll(const aDllName : string); {$IFDEF MSWINDOWS} const LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR = $00000100; LOAD_LIBRARY_DEFAULT_DIRS = $00001000; Var ExceptMask: TFPUExceptionMask; {$ENDIF} begin if not IsHandleValid then begin FDllName := aDllName; {$IFDEF MSWINDOWS} ExceptMask := GetExceptionMask; try {$IFDEF FPC} FDLLHandle := LoadLibraryExA(PAnsiChar(AnsiString(GetDllPath+DllName)), {$ELSE} /// [dcc64 Error] PythonEngine.pas(2965): E2010 Incompatible types: 'PWideChar' and 'PAnsiChar' FDLLHandle := LoadLibraryEx(PWidechar(GetDllPath+DllName), {$ENDIF} 0, LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR or LOAD_LIBRARY_DEFAULT_DIRS); finally SetExceptionMask(ExceptMask); end; {$ELSE} //Linux: need here RTLD_GLOBAL, so Python can do "import ctypes" FDLLHandle := THandle(dlopen(PAnsiChar(AnsiString(GetDllPath+DllName)), RTLD_LAZY+RTLD_GLOBAL)); {$ENDIF} end; end; function TMethodsContainer.AddMethod( AMethodName : PAnsiChar; AMethod : PyCFunction; ADocString : PAnsiChar ) : PPyMethodDef; begin if FMethodCount = FAllocatedMethodCount then ReallocMethods; Result := Methods[ MethodCount ]; Result^.ml_name := AMethodName; Result^.ml_meth := AMethod; Result^.ml_flags := METH_VARARGS; Result^.ml_doc := ADocString; Inc( FMethodCount ); end; from delphi_module import svg_image from io import StringIO import numpy as np import pandas as pd import matplotlib.pyplot as plt from sklearn.linear_model import BayesianRidge from sklearn.model_selection import RandomizedSearchCV, train_test_split from sklearn.preprocessing import PolynomialFeatures from sklearn.metrics import mean_squared_error, mean_absolute_error from sklearn.utils import parallel_backend parallel_backend('threading') import datetime import warnings warnings.filterwarnings("ignore") confirmed_df = pd.read_csv('https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv') parallel_backend('threading') cols = confirmed_df.keys() confirmed = confirmed_df.loc[:, cols[4]:cols[-1]] dates = confirmed.keys() world_cases = [] for i in dates: confirmed_sum = confirmed[i].sum() world_cases.append(confirmed_sum) # window size window = 7 days_since_1_22 = np.array([i for i in range(len(dates))]).reshape(-1, 1) world_cases = np.array(world_cases).reshape(-1, 1) days_in_future = 10 future_forcast = np.array([i for i in range(len(dates)+days_in_future)]).reshape(-1, 1) start = '1/22/2020' start_date = datetime.datetime.strptime(start, '%m/%d/%Y') future_forcast_dates = [] for i in range(len(future_forcast)): future_forcast_dates.append((start_date + datetime.timedelta(days=i)).strftime('%m/%d/%Y')) X_train_confirmed, X_test_confirmed, y_train_confirmed, y_test_confirmed = train_test_split(days_since_1_22[50:], world_cases[50:], test_size=0.05, shuffle=False) # transform our data for polynomial regression bayesian_poly = PolynomialFeatures(degree=5) bayesian_poly_X_train_confirmed = bayesian_poly.fit_transform(X_train_confirmed) bayesian_poly_X_test_confirmed = bayesian_poly.fit_transform(X_test_confirmed) bayesian_poly_future_forcast = bayesian_poly.fit_transform(future_forcast) # bayesian ridge polynomial regression tol = [1e-6, 1e-5, 1e-4, 1e-3, 1e-2] alpha_1 = [1e-7, 1e-6, 1e-5, 1e-4, 1e-3] alpha_2 = [1e-7, 1e-6, 1e-5, 1e-4, 1e-3] lambda_1 = [1e-7, 1e-6, 1e-5, 1e-4, 1e-3] lambda_2 = [1e-7, 1e-6, 1e-5, 1e-4, 1e-3] normalize = [True, False] bayesian_grid = {'tol': tol,'alpha_1': alpha_1,'alpha_2' : alpha_2,'lambda_1': lambda_1,'lambda_2' : lambda_2, 'normalize' : normalize} bayesian = BayesianRidge(fit_intercept=True) bayesian_search = RandomizedSearchCV(bayesian, bayesian_grid, scoring='neg_mean_squared_error', cv=3, return_train_score=True, n_jobs=-1, n_iter=40, verbose=1) bayesian_search.fit(bayesian_poly_X_train_confirmed, y_train_confirmed) print(bayesian_search.best_params_) bayesian_confirmed = bayesian_search.best_estimator_ test_bayesian_pred = bayesian_confirmed.predict(bayesian_poly_X_test_confirmed) bayesian_pred = bayesian_confirmed.predict(bayesian_poly_future_forcast) print('MAE:', mean_absolute_error(test_bayesian_pred, y_test_confirmed)) print('MSE:',mean_squared_error(test_bayesian_pred, y_test_confirmed)) plt.plot(y_test_confirmed) plt.plot(test_bayesian_pred) plt.legend(['Test Data', 'Bayesian Ridge Polynomial Predictions']) figfile = StringIO() plt.savefig(figfile, format='svg') figdata_svg = figfile.getvalue() svg_image.SvgText = figdata_svg #plt.show()