Run an OLS regression with Pandas Data Frame | Statsmodels pandas

I think you can almost do exactly what you thought would be ideal, using the statsmodels package which was one of pandas ' optional dependencies before ...JoinStackOverflowtolearn,shareknowledge,andbuildyourcareer.SignupwithemailSignupSignupwithGoogleSignupwithGitHubSignupwithFacebookHomePublicQuestionsTagsUsersCollectivesExploreCollectivesFindaJobJobsCompaniesTeamsStackOverflowforTeams–Collaborateandshareknowledgewithaprivategroup.CreateafreeTeamWhatisTeams?TeamsCreatefreeTeamCollectivesonStackOverflowFindcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost.LearnmoreTeamsQ&AforworkConnectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch.LearnmoreRunanOLSregressionwithPandasDataFrameAskQuestionAsked7years,8monthsagoActive5monthsagoViewed221ktimes12257IhaveapandasdataframeandIwouldliketoabletopredictthevaluesofcolumnAfromthevaluesincolumnsBandC.Hereisatoyexample:importpandasaspddf=pd.DataFrame({"A":[10,20,30,40,50],"B":[20,30,10,40,50],"C":[32,234,23,23,42523]})Ideally,Iwouldhavesomethinglikeols(A~B+C,data=df)butwhenIlookattheexamplesfromalgorithmlibrarieslikescikit-learnitappearstofeedthedatatothemodelwithalistofrowsinsteadofcolumns.Thiswouldrequiremetoreformatthedataintolistsinsidelists,whichseemstodefeatthepurposeofusingpandasinthefirstplace.WhatisthemostpythonicwaytorunanOLSregression(oranymachinelearningalgorithmmoregenerally)ondatainapandasdataframe?pythonpandasscikit-learnregressionstatsmodelsShareImprovethisquestionFolloweditedApr4'16at18:33denfromufa5,0721111goldbadges6767silverbadges133133bronzebadgesaskedNov15'13at0:47MichaelMichael10.7k2020goldbadges5959silverbadges104104bronzebadges0Addacomment | 6Answers6ActiveOldestVotes168Ithinkyoucanalmostdoexactlywhatyouthoughtwouldbeideal,usingthestatsmodelspackagewhichwasoneofpandas'optionaldependenciesbeforepandas'version0.20.0(itwasusedforafewthingsinpandas.stats.)>>>importpandasaspd>>>importstatsmodels.formula.apiassm>>>df=pd.DataFrame({"A":[10,20,30,40,50],"B":[20,30,10,4


常見投資理財問答


延伸文章資訊