Python Tutorial : Data transforms, features, and targets

DataCamp · Beginner ·⚡ Algorithms & Data Structures ·6y ago

Key Takeaways

This video tutorial covers data preparation for machine learning using Python, focusing on creating features and targets from historical stock price data using libraries like pandas, ta-lib, and seaborn.

Full Transcript

now that we're familiar with our data we need to get it prepared for machine learning to prepare our data will need features and targets our features our inputs we predict future price changes with the 10-day price change and volume our targets are the future price changes generally we can use pandas dataframes and series in our machine learning algorithms it's useful to incorporate historical data as features for example the price changes in the last 200 days instead of including all previous two hundred days price changes we can concentrate past data into a single point using technical indicators like the moving average shown here a moving average is the average of a value in the past n days classic moving average periods are 1450 and 200 days for stocks here's a plot showing the AMD price and a 200-day simple moving average or SMA you can see moving averages smooth the data the other indicator will use is relative strength index or RSI this oscillates between 0 and 100 when it's close to zero this may mean the price is due to rebound from recent lows when RSI is close to 100 this may mean the price of the stock is due to decline the equation for RSI is 100 minus 100 over 1 plus relative strength relative strength is the average gain of price increases divided by the average loss of price decreases during the time period n both RSI and moving averages can be calculated with the ta Lib package this Python library is a wrapper for C code meaning we can run C code using Python to use ta libs functions for RSI and moving averages we provide an umpire array of prices and the argument time period this is the value of n mentioned in the previous slides we're using 200 for time period and adding the new feature to our data frame as ma 200 for the 200-day moving average and RSI 200 for the 200-day RSI 40 a Lib functions we must provide numpy arrays not pandas objects the values property of pandas series and data frames yields numpy arrays we can now make our features and targets we choose the ten-day close percent change and the 200-day moving average and RSI from our data frame these feature names go into a lists which selects the columns from the data frame then we select the ten-day future close percent change as our target finally we create a data frame with both the features and targets so we can check for correlations before we do any machine learning it's good to check features and targets for correlations we use the pandas function core to calculate Pearson correlations and the Seabourn library has a handy heat map function for plotting the correlations the a notes option shows the numeric values for each correlation in the plot the plot looks like this with colors ranging from black for negative correlations to white for positive correlations the numeric values are also shown in each square to examine a correlation between two variables we look for the intersection of the two variables in the plot for example the RSI and tend a future price percent change intersect in the bottom left corner these variables have little to no correlation since the value is close to zero usually a value of 0.2 or greater means there is some linear correlation present the diagonal line with all ones shows the correlation of each variable with itself which is 1 this means each variable is perfectly linearly correlated with itself as we expect ok I think you're ready to create your own features and targets

Original Description

Want to learn more? Take the full course at https://learn.datacamp.com/courses/machine-learning-for-finance-in-python at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work. --- Now that we're familiar with our data, we need to get it prepared for machine learning. To prepare our data, we'll need features and targets. Our features are inputs we predict future price changes with -- the 10-day price change and volume. Our targets are the future price changes. Generally, we can use pandas DataFrames and Series in our machine learning algorithms. It's useful to incorporate historical data as features; for example, the price changes in the last 200 days. Instead of including all previous 200 days' price changes, we can concentrate past data into a single point using technical indicators like the moving average shown here. A moving average is the average of value in the past n days. Classic moving average periods are 14, 50, and 200 days for stocks. Here's a plot showing the AMD price and a 200-day simple moving average, or SMA. You can see moving averages smooth the data. The other indicator we'll use is relative strength index, or RSI. This oscillates between 0 and 100. When it's close to 0, this may mean the price is due to rebound from recent lows. When RSI is close to 100, this may mean the price of the stock is due to decline. The equation for RSI is 100 minus 100 over 1 + relative strength. Relative strength is the average gain of price increases divided by the average loss of price decreases during the time period, n. Both RSI and moving averages can be calculated with the TA-lib package. This Python library is a wrapper for C code, meaning we can run C code using Python. To use TA-lib's functions for RSI and moving averages, we provide a numpy array of prices and the argument time period. This is the value of n mentioned in the previous slides. We're using 200 for time period, and adding the new f
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from DataCamp · DataCamp · 0 of 60

← Previous Next →
1 SQL Server Tutorial: Date manipulation
SQL Server Tutorial: Date manipulation
DataCamp
2 R Tutorial: Intermediate Interactive Data Visualization with plotly in R
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
3 R Tutorial: Adding aesthetics to represent a variable
R Tutorial: Adding aesthetics to represent a variable
DataCamp
4 R Tutorial: Moving Beyond Simple Interactivity
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
5 Python Tutorial: Why use ML for marketing? Strategies and use cases
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
6 Python Tutorial: Preparation for modeling
Python Tutorial: Preparation for modeling
DataCamp
7 Python Tutorial: Machine Learning modeling steps
Python Tutorial: Machine Learning modeling steps
DataCamp
8 R Tutorial: The prior model
R Tutorial: The prior model
DataCamp
9 R Tutorial: Data & the likelihood
R Tutorial: Data & the likelihood
DataCamp
10 R Tutorial: The posterior model
R Tutorial: The posterior model
DataCamp
11 R Tutorial: An Introduction to plotly
R Tutorial: An Introduction to plotly
DataCamp
12 R Tutorial: Plotting a single variable
R Tutorial: Plotting a single variable
DataCamp
13 R Tutorial: Bivariate graphics
R Tutorial: Bivariate graphics
DataCamp
14 Python Tutorial: Customer Segmentation in Python
Python Tutorial: Customer Segmentation in Python
DataCamp
15 Python Tutorial: Time cohorts
Python Tutorial: Time cohorts
DataCamp
16 Python Tutorial: Calculate cohort metrics
Python Tutorial: Calculate cohort metrics
DataCamp
17 Python Tutorial: Cohort analysis visualization
Python Tutorial: Cohort analysis visualization
DataCamp
18 R Tutorial: Building Dashboards with flexdashboard
R Tutorial: Building Dashboards with flexdashboard
DataCamp
19 R Tutorial: Anatomy of a flexdashboard
R Tutorial: Anatomy of a flexdashboard
DataCamp
20 R Tutorial: Layout basics
R Tutorial: Layout basics
DataCamp
21 R Tutorial: Advanced layouts
R Tutorial: Advanced layouts
DataCamp
22 Python Tutorial: Time Series Analysis in Python
Python Tutorial: Time Series Analysis in Python
DataCamp
23 Python Tutorial: Correlation of Two Time Series
Python Tutorial: Correlation of Two Time Series
DataCamp
24 Python Tutorial: Simple Linear Regressions
Python Tutorial: Simple Linear Regressions
DataCamp
25 Python Tutorial: Autocorrelation
Python Tutorial: Autocorrelation
DataCamp
26 R Tutorial: The gapminder dataset
R Tutorial: The gapminder dataset
DataCamp
27 R Tutorial: The filter verb
R Tutorial: The filter verb
DataCamp
28 R Tutorial: The arrange verb
R Tutorial: The arrange verb
DataCamp
29 R Tutorial: The mutate verb
R Tutorial: The mutate verb
DataCamp
30 R Tutorial: What is cluster analysis?
R Tutorial: What is cluster analysis?
DataCamp
31 R Tutorial: Distance between two observations
R Tutorial: Distance between two observations
DataCamp
32 R Tutorial: The importance of scale
R Tutorial: The importance of scale
DataCamp
33 R Tutorial: Measuring distance for categorical data
R Tutorial: Measuring distance for categorical data
DataCamp
34 Python Tutorial: Plotting multiple graphs
Python Tutorial: Plotting multiple graphs
DataCamp
35 Python Tutorial: Customizing axes
Python Tutorial: Customizing axes
DataCamp
36 Python Tutorial: Legends, annotations, & styles
Python Tutorial: Legends, annotations, & styles
DataCamp
37 Python Tutorial: Introduction to iterators
Python Tutorial: Introduction to iterators
DataCamp
38 Python Tutorial: Playing with iterators
Python Tutorial: Playing with iterators
DataCamp
39 Python Tutorial: Using iterators to load large files into memory
Python Tutorial: Using iterators to load large files into memory
DataCamp
40 SQL Tutorial: Introduction to Relational Databases in SQL
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
41 SQL Tutorial: Tables: At the core of every database
SQL Tutorial: Tables: At the core of every database
DataCamp
42 SQL Tutorial: Update your database as the structure changes
SQL Tutorial: Update your database as the structure changes
DataCamp
43 Python Tutorial: Classification-Tree Learning
Python Tutorial: Classification-Tree Learning
DataCamp
44 Python Tutorial: Decision-Tree for Classification
Python Tutorial: Decision-Tree for Classification
DataCamp
45 Python Tutorial: Decision-Tree for Regression
Python Tutorial: Decision-Tree for Regression
DataCamp
46 Python Tutorial: Census Subject Tables
Python Tutorial: Census Subject Tables
DataCamp
47 Python Tutorial: Census Geography
Python Tutorial: Census Geography
DataCamp
48 Python Tutorial: Using the Census API
Python Tutorial: Using the Census API
DataCamp
49 R Tutorial: A/B Testing in R
R Tutorial: A/B Testing in R
DataCamp
50 R Tutorial: Baseline Conversion Rates
R Tutorial: Baseline Conversion Rates
DataCamp
51 R Tutorial: Designing an Experiment - Power Analysis
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
52 R Tutorial: Introduction to qualitative data
R Tutorial: Introduction to qualitative data
DataCamp
53 R Tutorial: Understanding your qualitative variables
R Tutorial: Understanding your qualitative variables
DataCamp
54 R Tutorial: Making Better Plots
R Tutorial: Making Better Plots
DataCamp
55 SQL Tutorial: OLTP and OLAP
SQL Tutorial: OLTP and OLAP
DataCamp
56 SQL Tutorial: Storing data
SQL Tutorial: Storing data
DataCamp
57 SQL Tutorial: Database design
SQL Tutorial: Database design
DataCamp
58 Python Tutorial: Introduction to spaCy
Python Tutorial: Introduction to spaCy
DataCamp
59 Python Tutorial: Statistical Models
Python Tutorial: Statistical Models
DataCamp
60 Python Tutorial: Rule-based Matching
Python Tutorial: Rule-based Matching
DataCamp

This video tutorial teaches how to prepare data for machine learning by creating features and targets from historical stock price data using Python libraries like pandas and ta-lib. It covers technical indicators like moving averages and RSI, and how to calculate Pearson correlations and interpret correlation plots.

Key Takeaways
  1. Import necessary libraries like pandas, ta-lib, and seaborn
  2. Load and prepare historical stock price data
  3. Calculate technical indicators like moving averages and RSI
  4. Create features and targets from the data
  5. Calculate Pearson correlations and interpret correlation plots
💡 Using technical indicators like moving averages and RSI can help concentrate past data into a single point, making it easier to use in machine learning algorithms.

Related Reads

Up next
Stump Grinder Carbide Wheel Grinds Hardwood To Chips
Innoforge Studio
Watch →