1) work with NumPy 2d arrays and data files 2) explore data set using ‘pandas’

python project and need the explanation and answer to help me learn.

it’s a lot of reading, because it walks you through the problem, not as much as it may seem
Requirements: as long as it it solves the problem
CS1112Fall2023Project4PartAdueTuesday10/31at11pmYoumustworkeitheronyourownorwithonepartner.IfyouworkwithapartneryoumustfirstregisterasagroupinCMSandthensubmityourworkasagroup.AdheretotheCodeofAcademicIntegrity.Agroupsubmissionrequiresthatbothpartnersarejointauthorsofallpartsoftheproject—splittingaprojectwitheachpartnerworkingontheirownpartisaviolationofacademicintegrity.Foragroup,“you”belowrefersto“yourgroup.”Youmaydiscussbackgroundissuesandgeneralstrategieswithothersandseekhelpfromthecoursestaff,buttheworkthatyousubmitmustbeyourown.Inparticular,youmaydiscussgeneralideaswithothersorlookupgeneralprogrammingtechniques,butyoumaynotworkoutthedetailedsolutionswithothers,andyoumaynotsearchfororusetoolstogeneratesolutionstospecificpartsoftheassignment.ItisnotOKforyoutoseeorhearanotherstudent’scodeanditiscertainlynotOKtocopycodefromanotherpersonorfrompublished/Internetsources,includinggenerativeartificialintelligencetools.Ifyoufeelthatyoucannotcompletetheassignmentonyouown,seekhelpfromthecoursestaff.Pleasereadthe“AcademicIntegrity”sectionintheSyllabusforfurtherdetailsandfortherationaleoftheCS1112AcademicIntegritypolicy.ObjectivesCompletingthisprojectwillsolidifyyourunderstandingof2-dimensionaldatastructures.InPartA,youwillwriteasetoffunctionstosimulateConway’sGameofLifeusingNumPy2-dimensionalarrays.Thecomputingthemesaresimulationandproblemdecomposition.ThecomputingthemeofPartBisdatascience.YouwillusethepopularandusefullibraryPandastoexploreasetofdata.The2-dimensionaldatastructurethatyouwillseeinPartBistheDataFrame.Youalsowillgettousesomenewmatplotlibfunctionstovisualize2-dimensionaland3-dimensionaldata!GroundRuleSincethisisanintroductorycomputingcourse,andinordertogiveyoupracticeonspecificprogrammingconcepts,somefunctions/featuresareforbiddeninassignments.Useonlytheconceptsandtoolsdiscussedinclassandinthisassignment.1Conway’sGameofLifeConway’s“Game”ofLifeisnotactuallyagamethatoneplays;ratheritisasimulationfromsomeinitialstatefollowingasetofrules.Thestateofthegameisagrid(matrix)ofelements,sometimescalledcells.Acellcanbeeitheraliveordead.Inonestepofthegame,thecellswilleitherliveordieaccordingtosomerules.TherulesstatedbelowaretakenfromtheWikipediaentryonConway’sGameofLife.Everycellinthegridinteractswithitseightneighbors,whicharethecellsthatarehorizontally,vertically,ordiagonallyadjacent.Ateachstepintime,thefollowingtransitionsoccur:❼Alivecellwithfewerthan2liveneighborsdies(under-population).❼Alivecellwith2or3liveneighborsliveson.❼Alivecellwithmorethan3liveneighborsdies(overcrowding).❼Adeadcellwithexactly3liveneighborsbecomeslive(reproduction).Inadditiontotheaboverules,wewilladdanextra“new-liferule”:❼Adeadcellwith4ormoreliveneighborsbecomeslivewithaprobabilityof0.4.Youwillcompletethefunctionsspecifiedingameoflife.pytosimulatethisgame.1
ProblemDecompositionLookovertheprovidedskeletoningameoflife.py.Wedecomposedtheproblemforyoubysplittinguptheworkintothreefunctions:onetocreatetheinitialstate,onetoupdatethestatebyonegeneration,andonetocarryoutthesimulationofmultiplegenerations.Decomposetheproblemfurtherbydesigningandimplementingyourownhelperfunction(s)!(Ahelperfunctionisjustafunction;itisonethatatleastoneofourspecifiedfunctionswillcallincompletingthesimulation.)Youarerequiredtodesignanduseatleastonehelperfunctioninthispartoftheproject.Youonlyneedtodesignandimplementonehelperfunction,butdecomposingaproblemintoindividualsubtasksisanexcellentdesignandprogramdevelopmentstrategy,sofeelfreetousemorehelperfunctionstofurthermodularizeyoursolution.Makeyourhelperfunctionsmeaningful—don’tjustdealwiththisrequirementsuperficiallybycreatingahardlynecessaryone-linerfunctionthatdoesjustatrivialcomputationortask.Designingandspecifyingauseful(helper)functiontakesrealeffort,anditisexactlytheproblemdecompositionpracticeandpreparationthatyouneedinordertodealwithlargerandmorecomplexprogramsandproblemsinthefuture.Besuretofullyspecifyafunctionbywritingadocstringthatsayswhatthefunctiondoesanddefinestheparameters.SuggestedWorkflowandTestingTherearemultiplepartstothissimulation:2differentwaystoinitializethestate,the4standardrulesandourextranew-life-rule,andanimation.Wesuggestaworkflowthatallowsyoutodevelopcodeandtestincrementally:1.Functioncreateworldincludestwodifferentinitializationschemes.Startwiththerandomschemeandleaveoutthefile-readingscheme.Perhapsaddacommentinthefunctiontoremindyourselftodealwiththefile-readinginitializationlater.Immediatelytestyourfunction.Youcanseeattheendofthefilethatwegaveyouanexamplecalltocreateworld.Makesurethatyourcodesofarworks.2.Goontofunctiononegenerationlater.TheoriginalrulesoftheGameofLifearedeterministic—norandomvariability—whileouraddednew-life-ruleinvolvesprobability.Solvethedeterministicpartfirstandimmediatelytestthefunction!(Addcodetothe“scriptcode”atthebottomofthefiletocallyourfunction.)Involvingprobabilitywhenyoustartthisfunctionwouldmakeitharderforyoutotellwhetheryourcodeiscorrect,sotestbeforeyouaddthenew-life-rule.Afteryouseethatit’scorrectthenaddthenew-liferuleandtestagain.3.Testingtheprobabilisticnew-liferule…Theprobabilityoftheeventhappeningislow(40%),whichmakesithappeninfrequentlyandthereforedifficulttotest.Whatshouldyoudo?Fortesting,changetheprobabilitytoveryhigh,like99%oreven100%sothatyournew-liferule(almost)always“kicksin”andyoucancheckthatithasthespecifiedeffectonthenewworldmatrix.Aftertesting,besuretochangetheprobabilitybackto40%.4.Goontofunctionsimulate.Skiptheanimation.Justcalltheappropriatefunctionstocreateaworldandwritealooptosimulatengenerations.Forthepurposeoftesting,addprintstatementsinthefunctionbodytoprinttheinitialworldmatrixandthematrixaftereachgeneration.Thenaddscriptcodetocallsimulate,specifyingthatyouwantrandominitializationandnottousethenewlife-rule.Here’sanexample:nr=5nc=8add_rule=Trueworld=simulate(1,nr,nc,”random”,notadd_rule,2)Thenincreasethenumberofgenerationsandtestagain.Youshouldalsocheckthat0generationswillnotcauseyourfunctiontothrowanexception(error).Gobacktoremovetheprintstatementsnow(ormakethemcomments).5.Nowyouhaveseenthroughthewholesimulationandit’stimetogobacktofinishthetwodetailsskippedearlier:readingtheinitialdatafromfileandaddingtheanimation.Choosewhicheveryouwanttoworkonfirst.(ProfessorFanchosetodographicsfirstbecauseit’spretty.)Wediscusssomenewgraphicsandfile-readingtoolsbelow.2
GraphicsinfunctionsimulateSpyderdoesnothandleinteractivegraphicsoranimations.Inordertoseeouranimations,youneedtodirectthefiguretoanexternalgraphicaluserinterface.Beforeyourunanimation,writethefol-lowingcommandintheSpyderPythonConsoleonce:%matplotlibqtIfyourestartthekernelorSpyder,youwillneedtoissuethecom-mandagain.matplotlib.pyplot.matshow(w)showsthevaluesinamatrixwusingcolors.Sinceourmatrixhasonlytwopossiblevalues,therearejusttwocolorsinthegraphic.Theexamplegraphicshownontherightisarandomlygeneratedinitialstatefora5rows8columnsworld.Belowisthecodethatyou’llneedtoshowtheinitialstate:plt.close()#Closeanycurrentlyopenedfigurewindowfig,ax=plt.subplots()#Getthefigurereference(fig)andaxesreference(ax)forasubplotax.matshow(w)#Showthevaluesinmatrixwusingdefaultcolorsontheaxes#Useyourworldmatrix,whichmaynotbecalledwax.set_title(______)#Youneedtowriteanf-stringtoreplacetheblank(seediagram)plt.pause(blink)#blinkisaparameterinfunctionsimulateThenattheendofeachiterationinthesimulation,youneedtoshowthematrixvaluesagainafterclearingtheaxes(dumpthepreviousplotbutkeepthefigure):ax.clear()#Clearaxesax.matshow(w)ax.set_title(______)plt.pause(blink)That’sit!Enjoytheanimation!ReaddatafileusingNumPyinfunctioncreateworldDuringLecture16,youlearnedhowtousenativePythonfunctionsandmethodstodealwithplaintextdatafilesingeneral.Whenyouareluckyenoughtohaveadatafilewhosecontentresemblesamatrix,youcanusetheveryfriendlynumpy.loadtxtfunctiontoreadthedata!Herearethecontentsoftheseedsglider.txtdatafilethatweprovided:#Thesmallest,mostcommon,andfirstdiscoveredspaceship.#www.conwaylife.com/wiki/index.php?title=Glider0100000000100000111000000000000000000000Besidesthefirsttwolines,therestofthefilecontentslookslikeamatrix.Inthiscase,youcanusethenumpy.loadtxtfunctiontoreturna2-darray:np.loadtxt(filename,’int’,’#’)wherefilenameisthenameofthefiletoberead,thesecondargumentspecifiesthattheNumPyarrayshoulduseanintegertypeforthedata,andthethirdargumentspecifiesthatanylinethatbeginswith”#”shouldbetreatedasacomment,notdata.Weprovidedaseconddatafile,seedsp48.txt,thatyoucanplaywith.TheseareknownGameofLifepatternsthatcreateinterestingeffects.Therearemanymorepatternsthatyoucanfindonline,andyoucanofcoursecreateyourown“seedsfile”usingthesameformatasourexamplefiles(anycommentlinesbeginwith”#”,datalineslooklikeamatrixof0sand1s).EnjoytheGameofLife!SubmitonCMS(afterregisteringyourgroup)yourfilegameoflife.py.PartBofProject4willappearinaseparatedocument.3

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *