Book Image

Artificial Intelligence with Python

Book Image

Artificial Intelligence with Python

Overview of this book

Artificial Intelligence is becoming increasingly relevant in the modern world. By harnessing the power of algorithms, you can create apps which intelligently interact with the world around you, building intelligent recommender systems, automatic speech recognition systems and more. Starting with AI basics you'll move on to learn how to develop building blocks using data mining techniques. Discover how to make informed decisions about which algorithms to use, and how to apply them to real-world scenarios. This practical book covers a range of topics including predictive analytics and deep learning.
Table of Contents (23 chapters)
Artificial Intelligence with Python
Credits
About the Author
About the Reviewer
www.PacktPub.com
Customer Feedback
Preface

Building a single variable regressor


Let's see how to build a single variable regression model. Create a new Python file and import the following packages:

import pickle 
 
import numpy as np 
from sklearn import linear_model 
import sklearn.metrics as sm 
import matplotlib.pyplot as plt 

We will use the file data_singlevar_regr.txt provided to you. This is our source of data:

# Input file containing data 
input_file = 'data_singlevar_regr.txt' 

It's a comma-separated file, so we can easily load it using a one-line function call:

# Read data 
data = np.loadtxt(input_file, delimiter=',') 
X, y = data[:, :-1], data[:, -1] 

Split it into training and testing:

# Train and test split 
num_training = int(0.8 * len(X)) 
num_test = len(X) - num_training 
 
# Training data 
X_train, y_train = X[:num_training], y[:num_training] 
 
# Test data 
X_test, y_test = X[num_training:], y[num_training:] 

Create...