<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>python Archives - AI Betting Edge</title>
	<atom:link href="https://aibettingedge.com/tag/python/feed/" rel="self" type="application/rss+xml" />
	<link>https://aibettingedge.com/tag/python/</link>
	<description>MLB and NBA Prop Bets</description>
	<lastBuildDate>Sun, 21 Aug 2022 10:58:40 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.1.5</generator>
	<item>
		<title>Helper Functions for Calculating Fantasy Points</title>
		<link>https://aibettingedge.com/helper-functions-for-calculating-fantasy-points/</link>
					<comments>https://aibettingedge.com/helper-functions-for-calculating-fantasy-points/#respond</comments>
		
		<dc:creator><![CDATA[Shon Butani]]></dc:creator>
		<pubDate>Fri, 15 May 2020 14:17:33 +0000</pubDate>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tutorial]]></category>
		<guid isPermaLink="false">https://demo.creativethemes.com/blocksy/app/?p=287</guid>

					<description><![CDATA[<p>This is going to be one of our shorter blog posts, but if you&#8217;re into daily fantasy basketball betting, it&#8217;s a must read! One of the most basic but overlooked areas in building a fantasy points model is &#8211; how exactly are we even defining fantasy points? Does it vary from website to website? Does [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://aibettingedge.com/helper-functions-for-calculating-fantasy-points/">Helper Functions for Calculating Fantasy Points</a> appeared first on <a rel="nofollow" href="https://aibettingedge.com">AI Betting Edge</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">This is going to be one of our shorter blog posts, but if you&#8217;re into daily fantasy basketball betting, it&#8217;s a must read! One of the most basic but overlooked areas in building a fantasy points model is &#8211; how exactly are we even defining fantasy points? Does it vary from website to website? Does a point count more than a rebound? Wouldn&#8217;t it be nice to have a configurable function that takes in weights and outputs the&nbsp;<em>correct</em>&nbsp;fantasy points scored by a player, as measured by a particular website&#8217;s (draftkings, fanduel) statistical weighted combination used to create fantasy points? That&#8217;s exactly what we&#8217;re going to outline below.</p>



<p class="wp-block-paragraph">Below is a simple function we wrote that takes in your major stat category <em>weights</em>, and implements a vectorized calculation of fantasy points for all the rows in your dataframe. (If you aren&#8217;t aware of what dataframe means in this context, see our first two blog posts <a href="https://aibettingedge.com/2020/05/19/nba-com-scraper/" data-type="post" data-id="345">here </a>and <a href="https://aibettingedge.com/2020/05/19/tristique-nulla-aliquet-enim-tortor/" data-type="post" data-id="338">here</a>.</p>



<pre class="wp-block-code"><code>def calculate_fpts(PTS_weight, P3M_weight,REB_weight, AST_weight, STL_weight, BLK_weight,TOV_weight):
    
    full_df&#91;"FPTS"] = (PTS_weight * full_df&#91;"PTS"]) + (P3M_weight * full_df&#91;"3PM"]) + (REB_weight * full_df&#91;"REB"]) + (AST_weight *  full_df&#91;"AST"]) + (STL_weight * full_df&#91;"STL"]) + (BLK_weight * full_df&#91;"BLK"]) - (TOV_weight * full_df&#91;"TOV"])

    return full_df&#91;"FPTS"]

# Using draftkings weights to calculate fpts
full_df&#91;"FPTS"] = full_df.apply(lambda x : calculate_fpts(1,0.5,1.25,1.5,2,2,0.5), axis=1)</code></pre>



<p class="wp-block-paragraph">The only thing you have make sure of is that your column names match up with ours above. We&#8217;re using standard basketball abbreviations so there shouldn&#8217;t be any confusion (Points -&gt; PTS, Rebounds -&gt; REB), and so on.</p>



<p class="wp-block-paragraph">Next is a calculation that is almost always overlooked, and could be leading to incorrect predictions in your sports betting model. That calculation is the double-double and/or triple-double. The assumption is the readers of this article knows what a double-double and triple-double are, but how exactly could we go about adding this as a factor in our overall fantasy points calculation above? DraftKings adds 1.5 fantasy points if a player gets a double-double, and 3 fantasy points for a triple-double. So for someone like Russell Westbrook who is (used to be) a triple-double machine, if you simply ignore this calculation, you&#8217;ll consistently underpredict his fantasy points. There are more efficient ways of doing the calculation that what we&#8217;ve listed below, but the good part about our function is that it&#8217;s extremely intuitive and easy to interpret. </p>



<pre class="wp-block-code"><code>def calculate_triple_double(PTS, REB, AST, STL, BLK):
    count = 0
    
    if PTS >= 10:
        count += 1

    if REB >= 10:
        count += 1
    
    if AST >= 10:
        count += 1
    
    if STL >= 10:
        count += 1
    
    if BLK >= 10:
        count += 1
    
    if count == 2:
        return 1.5
    elif count >= 3:
        return 3
    else:
        return 0
    
full_df&#91;"TRIPLE_DOUBLE"] = full_df.apply(lambda x : calculate_triple_double(x&#91;"PTS"], x&#91;"REB"], x&#91;"AST"], x&#91;"STL"], x&#91;"BLK"]), axis=1)

full_df&#91;"FPTS"]+=full_df&#91;"TRIPLE_DOUBLE"]</code></pre>



<p class="wp-block-paragraph">In summary, if you&#8217;re serious about building a basketball betting model, keep these two functions handy &#8211; they&#8217;ll come up over and over again and there&#8217;s no need to constantly recreate the logic.</p>
<p>The post <a rel="nofollow" href="https://aibettingedge.com/helper-functions-for-calculating-fantasy-points/">Helper Functions for Calculating Fantasy Points</a> appeared first on <a rel="nofollow" href="https://aibettingedge.com">AI Betting Edge</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://aibettingedge.com/helper-functions-for-calculating-fantasy-points/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Scraping Potential Assists</title>
		<link>https://aibettingedge.com/scraping-potential-assists/</link>
					<comments>https://aibettingedge.com/scraping-potential-assists/#respond</comments>
		
		<dc:creator><![CDATA[Shon Butani]]></dc:creator>
		<pubDate>Fri, 15 May 2020 14:06:43 +0000</pubDate>
				<category><![CDATA[Apps]]></category>
		<category><![CDATA[Learning]]></category>
		<category><![CDATA[nba]]></category>
		<category><![CDATA[pandas]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">https://demo.creativethemes.com/blocksy/app/?p=273</guid>

					<description><![CDATA[<p>In this post, we&#8217;re going to discuss how to scrape one of the most important features for building a successful NBA prop bets model. That feature is potential assists. And it&#8217;s specifically helpful for building an over/under assists model.&#160; First, let&#8217;s discuss what a potential assist actually is. A potential assist is a pass that [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://aibettingedge.com/scraping-potential-assists/">Scraping Potential Assists</a> appeared first on <a rel="nofollow" href="https://aibettingedge.com">AI Betting Edge</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">In this post, we&#8217;re going to discuss how to scrape one of the most important features for building a successful NBA prop bets model. That feature is potential assists. And it&#8217;s specifically helpful for building an over/under assists model.&nbsp;</p>



<p class="wp-block-paragraph">First, let&#8217;s discuss what a potential assist actually is. A potential assist is a pass that leads directly to a shot, foul or turnover. If you watch basketball, you&#8217;ve probably noticed that the number of assists a player gets is&nbsp;<em>largely</em>&nbsp;dependent on how good his teammates are shooting that night. Let&#8217;s put it this way, consider two scenarios: In scenario one, player 1 makes three passes to player 2, and all three times player 2 shoots and makes a three pointer. That goes for both three potential assists and three assists for player 1. Each potential assist was actually realized as an actual assist, because player 2 made all his three 3-point attempts on passes from player 1. Now let&#8217;s consider scenario 2 &#8211; player 1 passes ten times to player 2, and player 2 shoots ten threes, making only two. This counts for TEN potential assists for player 1, while only two actual assists.</p>



<p class="wp-block-paragraph">Now comes the important question, which scenario do you prefer? The answer is, it&#8217;s complicated, but in the long run, scenario 2 is definitely preferable to scenario 1. No one in the history of basketball is a 100% three point shooter, so we can&#8217;t expect 3 potential assists to translate to 3 assists too often. On the contrary, most NBA players don&#8217;t shoot 20% from the field, so we can&#8217;t expect 10 potential assists to translate to 2 assists often &#8211; usually more like 3 or 4 assists. This is why just knowing the number of assists a player had in a game is a misleading statistic. By using potential assists as a feature, you can take into account how repeatable his previous assist totals are going forward.</p>



<p class="wp-block-paragraph">With that being said, let&#8217;s dig into the code. You&#8217;ll need the NBA API for this, and if you don&#8217;t have in installed yet, you can check out our previous blog post on working with the NBA API.</p>



<pre class="wp-block-code"><code>import pandas as pd
from nba_api.stats.endpoints import leaguedashptstats</code></pre>



<p class="wp-block-paragraph">Below we&#8217;ve created an easy to use function for you to scrape potential assists in addition to two other useful columns, passes made and passes received. The function itself takes in season, season_type, and game_date as arguments, and returns the three aformentioned features for all the players that played on that game date.</p>



<p class="wp-block-paragraph">The parameters that the leaguedashptstats endpoint takes in as arguments are predefined, so just copy and paste the function exactly as is below.</p>



<pre class="wp-block-code"><code>def get_passing_stats(season, season_type, game_date):

    player_dash_info = leaguedashptstats.LeagueDashPtStats(
        last_n_games = 0,
        pt_measure_type = "Passing",
        player_or_team = "Player",
        per_mode_simple = "PerGame",
        season = season,
        season_type_all_star = season_type,
        team_id_nullable = 0,
        opponent_team_id = 0,
        date_from_nullable = game_date,
        date_to_nullable = game_date
    )

    player_logs = player_dash_info.get_data_frames()&#91;0]
    player_logs&#91;"GAME_DATE"] = game_date
    
    player_logs&#91;"GAME_DATE"] = pd.to_datetime(player_logs&#91;"GAME_DATE"])
    
    return player_logs</code></pre>



<p class="wp-block-paragraph">And there you have it! Below is a dataframe the tells you how many passes made, passes received, and potential assists a player had for any particular game date. </p>



<figure class="wp-block-image size-full"><img fetchpriority="high" decoding="async" width="621" height="325" src="https://aibettingedge.com/wp-content/uploads/2022/05/Scraping-Potential-Assists.png" alt="Scraping Potential Assists" class="wp-image-434" srcset="https://aibettingedge.com/wp-content/uploads/2022/05/Scraping-Potential-Assists.png 621w, https://aibettingedge.com/wp-content/uploads/2022/05/Scraping-Potential-Assists-300x157.png 300w" sizes="(max-width: 621px) 100vw, 621px" /></figure>
<p>The post <a rel="nofollow" href="https://aibettingedge.com/scraping-potential-assists/">Scraping Potential Assists</a> appeared first on <a rel="nofollow" href="https://aibettingedge.com">AI Betting Edge</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://aibettingedge.com/scraping-potential-assists/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Calculating Season Averages for NBA Betting Features</title>
		<link>https://aibettingedge.com/calculating-season-averages-for-nba-betting-features/</link>
					<comments>https://aibettingedge.com/calculating-season-averages-for-nba-betting-features/#respond</comments>
		
		<dc:creator><![CDATA[Shon Butani]]></dc:creator>
		<pubDate>Fri, 15 May 2020 14:04:47 +0000</pubDate>
				<category><![CDATA[Learning]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[Trends]]></category>
		<category><![CDATA[betting]]></category>
		<category><![CDATA[learning]]></category>
		<category><![CDATA[nba]]></category>
		<category><![CDATA[python]]></category>
		<guid isPermaLink="false">https://demo.creativethemes.com/blocksy/app/?p=270</guid>

					<description><![CDATA[<p>In this post, we&#8217;re going to dig into some feature engineering. At it&#8217;s core, any NBA betting model is a time series analysis problem. You&#8217;re trying to predict what a player will do tonight, given what he&#8217;s done in the past. The &#8220;given what he&#8217;s done in the past&#8221; is what we call feature engineering. [&#8230;]</p>
<p>The post <a rel="nofollow" href="https://aibettingedge.com/calculating-season-averages-for-nba-betting-features/">Calculating Season Averages for NBA Betting Features</a> appeared first on <a rel="nofollow" href="https://aibettingedge.com">AI Betting Edge</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p class="wp-block-paragraph">In this post, we&#8217;re going to dig into some feature engineering. At it&#8217;s core, any NBA betting model is a time series analysis problem. You&#8217;re trying to predict what a player will do tonight, given what he&#8217;s done in the past. The &#8220;given what he&#8217;s done in the past&#8221; is what we call feature engineering. In this post, we&#8217;re going to walk you through how to take some basic stats and turn them into season average features, while preventing the dreaded data leakage problem.</p>



<p class="wp-block-paragraph">First, as always (most of the time), we import pandas. That&#8217;s surprisingly the only package we&#8217;ll need for this blog post.</p>



<pre class="wp-block-code language-python"><code>import pandas as pd</code></pre>



<p class="wp-block-paragraph">Next, let&#8217;s define the features we want to compute season averages for. For simplicity, we chose the basic box score stats.</p>



<pre class="wp-block-code"><code>stats = &#91;'PTS','REB','AST','TOV','3PM','STL','BLK']</code></pre>



<p class="wp-block-paragraph">Now let&#8217;s dig into the logic. There are a few relatively straightforward preprocessing steps that need to be performed before computing the season average. But before digging into the code, let&#8217;s talk a little about what we DON&#8217;T want to do. Imagine the NBA season is 5 games long. Monday, Tuesday, Wednesday, Thursday and Friday. LeBron James scored 20, 25, 30, 35 and 40 points on those 5 days. We pass in all 5 games as part of our training data (the data the algorithm learns from), and the only feature we use is his season average for points.&nbsp;<em>What is</em>&nbsp;his season average? You may quickly think, oh it&#8217;s just (20+25+30+35+40)/5 = 30 points. But is his season average really 30 points for all of those 5 game dates? To the algorithm, when it&#8217;s predicting Wednesday, the games on Thursday and Friday have not happened yet. So really for Monday, there is no average, because there&#8217;s no &#8220;yesterday&#8221;. For Tuesday, his average is 20 (Monday&#8217;s total). For Wednesday, it&#8217;s 22.5 (taken by averaging Monday and Tuesday&#8217;s outcomes) and so on and so forth.</p>



<p class="wp-block-paragraph">With that being said, let&#8217;s dig into the code. We first need to convert the &#8220;GAME_DATE&#8221; collumn to a pandas datetime object. Next, we need to sort based on three columns, first by &#8220;SEASON_ID&#8221;, second by &#8220;PLAYER_ID&#8221;, and third by &#8220;GAME_DATE&#8221;. The logic behind this is straightforward: We want each player&#8217;s per season stats, sorted in ascending order by game date (to prevent data leakage as mentioned above).</p>



<p class="wp-block-paragraph">After the sorting is done, we do a groupby on &#8220;SEASON_ID&#8221;, &#8220;PLAYER_ID&#8221;, and &#8220;TEAM_ID&#8221; to ensure that if a player gets traded midseason, we&#8217;re counting his stats only with the current team he&#8217;s on (as per the game date in the row). We apply a lambda function to the stats columns we defined above, and shift everything by 1 day to make sure that we&#8217;re not accidentally counting the player&#8217;s stats from that day towards his averages for predicting that day (again a data leakage problem). For each unique SEASON_ID, PLAYER_ID, GAME_DATE group, the first row should be null. That&#8217;s because of the shift(1) we performed above.</p>



<pre class="wp-block-code"><code># full_df is a dataframe with the player stats on each game_date

full_df&#91;"GAME_DATE"] = pd.to_datetime(full_df&#91;"GAME_DATE"])
full_df.sort_values(by=&#91;"SEASON_ID", "PLAYER_ID","GAME_DATE"], inplace=True)
full_df.reset_index(inplace=True,drop=False)

moving_average_dataframe = full_df.groupby(&#91;"SEASON_ID", "PLAYER_ID","TEAM_ID"])&#91;stats].apply(lambda x: x.expanding().mean().shift(1)).reset_index(drop=True)
moving_average_dataframe.columns = "1_day_lag_season_average"+moving_average_dataframe.columns
full_df = full_df.join(moving_average_dataframe)

full_df.drop(columns=&#91;'index'],inplace=True)</code></pre>



<figure class="wp-block-image size-full"><img decoding="async" width="752" height="419" src="https://aibettingedge.com/wp-content/uploads/2022/05/nba-season-average.png" alt="nba-season-average" class="wp-image-438" srcset="https://aibettingedge.com/wp-content/uploads/2022/05/nba-season-average.png 752w, https://aibettingedge.com/wp-content/uploads/2022/05/nba-season-average-300x167.png 300w" sizes="(max-width: 752px) 100vw, 752px" /></figure>
<p>The post <a rel="nofollow" href="https://aibettingedge.com/calculating-season-averages-for-nba-betting-features/">Calculating Season Averages for NBA Betting Features</a> appeared first on <a rel="nofollow" href="https://aibettingedge.com">AI Betting Edge</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://aibettingedge.com/calculating-season-averages-for-nba-betting-features/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
