Coursera Machine Learning Challenges in Python: ex1 (Linear Regression)

policy

Machine Learning at Stanford University, the most popular course on the online learning platform Coursera. The instructor is Dr. Andrew Ng. Classes consist of lectures and programming tasks, but Octave or Matlab is specified as the language used for the programming tasks.

We will quietly implement this programming task using Python. However,

The policy.

Ex1 at once

In ex1, which is the first task, we will do Linear Regression. In restaurant chain management, the profit amount when opening a new store is predicted based on the data set that pairs the population of the city that opened in the past and the profit amount of the restaurant.

Click here for the code.

ex1.py


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn import linear_model

#Data read
data = pd.read_csv("ex1data1.txt", header=None)

plt.scatter(data[0], data[1], marker='x', c='r')
plt.xlabel("Population of city in 10,000s")
plt.ylabel("Profit in $10,000s")

X = np.array([data[0]]).T
y = np.array(data[1])

model = linear_model.LinearRegression()
model.fit(X, y)

px = np.arange(X.min(),X.max(),.01)[:,np.newaxis]
py = model.predict(px)

plt.plot(px, py, color="blue", linewidth=3)
plt.show()

The resulting plot is output like this: ex1.png

This time's point

The linear regression model uses scikit-learn's sklearn.linear_model.LinearRegression () class. First, create an instance and train with model.fit (X, y). The intercept and slope that are the learning results can be extracted as model.intercept_ and model.coef_, respectively. To make a prediction for a new X value using a model, usemodel.predict (X).

About one-dimensional vector of numpy

Unlike Matlab / Octave, Python does not distinguish between one-dimensional vertical and horizontal vectors. To explicitly create a vertical vector np.array([[1,2,3,4,5]]).T Or np.array([1,2,3,4,5])[:,np.newaxis)] And. -> Reference article

Recommended Posts

Coursera Machine Learning Challenges in Python: ex1 (Linear Regression)
Coursera Machine Learning Challenges in Python: ex2 (Logistic Regression)
Coursera Machine Learning Challenges in Python: ex7-2 (Principal Component Analysis)
Coursera Machine Learning Challenges in Python: ex3 (Handwritten Number Recognition with Logistic Regression)
Coursera Machine Learning Challenges in Python: ex5 (Adjustment of Regularization Parameters)
Coursera Machine Learning Challenges in Python: ex6 (How to Adjust SVM Parameters)
Coursera Machine Learning Challenges in Python: ex7-1 (Image compression with K-means clustering)
Machine learning linear regression
EV3 x Python Machine Learning Part 2 Linear Regression
Machine Learning: Supervised --Linear Regression
Online linear regression in Python
Python Scikit-learn Linear Regression Analysis Nonlinear Simple Regression Analysis Machine Learning
Machine learning beginners try linear regression
Classification and regression in machine learning
Python: Preprocessing in Machine Learning: Overview
Machine learning algorithm (generalization of linear regression)
Linear regression in Python (statmodels, scikit-learn, PyMC3)
Machine learning with python (2) Simple regression analysis
[python] Frequently used techniques in machine learning
Python: Preprocessing in machine learning: Data acquisition
[Python] Saving learning results (models) in machine learning
Python: Preprocessing in machine learning: Data conversion
Machine learning algorithm (linear regression summary & regularization)
Implemented in Python PRML Chapter 3 Bayesian Linear Regression
[Python3] Let's analyze data using machine learning! (Regression)
Get a glimpse of machine learning in Python
Machine learning logistic regression
Linear search in Python
Python: Supervised Learning (Regression)
Regression analysis in Python
"Linear regression" and "Probabilistic version of linear regression" in Python "Bayesian linear regression"
Build an interactive environment for machine learning in Python
Tool MALSS (application) that supports machine learning in Python
Tool MALSS (basic) that supports machine learning in Python
Attempt to include machine learning model in python package
Cross-entropy to review in Coursera Machine Learning week 2 assignments
MALSS, a tool that supports machine learning in Python
Machine learning with Python! Preparation
Used in machine learning EDA
Simple regression analysis in Python
[Python] Linear regression with scikit-learn
Supervised machine learning (classification / regression)
Beginning with Python machine learning
Machine learning stacking template (regression)
Machine learning algorithm (logistic regression)
The result of Java engineers learning machine learning in Python www
Implement stacking learning in Python [Kaggle]
First simple regression analysis in Python
Python: Application of supervised learning (regression)
How about Anaconda for building a machine learning environment in Python?
Machine learning with python (1) Overall classification
Machine learning summary by Python beginners
Automate routine tasks in machine learning
Machine learning algorithm (simple regression analysis)
<For beginners> python library <For machine learning>
Machine learning in Delemas (data acquisition)
Enjoy Coursera / Machine Learning materials twice
Implemented Perceptron learning rules in Python
[Machine learning] Understanding linear multiple regression from both scikit-learn and mathematics
Preprocessing in machine learning 2 Data acquisition
"Gaussian process and machine learning" Gaussian process regression implemented only with Python numpy