Total members 11806 |It is currently Thu Nov 21, 2019 4:55 am Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





This example is a good one to start learning applying machine learning in python. If you are new to python and machine learning this example will guide you through simple steps to run your first Supervised Learning model. As a dataset, we use the publicly available Diabetes dataset in sklearn library. The Diabetes dataset has records for 442 patients and 10 features. The features are Age, Gender, BMI, Blood Pressure, 6x Blood Serum Measurements. For simplicity, we pick the second feature which is the Gender. The target class is a continuous value for Diabetes Disease. The trained model is validated by splitting the dataset into training and testing. The linear regression model try to find a linear relationship between the feature X and target class Y. The linear equation is Y=aX+c where 'a' is the coefficient and 'c' is the intersection. We train the model using the training split and then measure the model performance using the testing split.

Code:
#Demo1
#M. S. Rakha, Ph.D.
#Post-Doctoral - Queen's University
#Supervised Learning - LinearRegression
%matplotlib inline
print(__doc__)



import matplotlib.pyplot as plt
import numpy as np
from sklearn import datasets, linear_model
from sklearn.metrics import mean_squared_error, r2_score
import pandas as pd

# Load the diabetes dataset
diabetes = datasets.load_diabetes()
diabetesDF=pd.DataFrame(diabetes.data)


# Use only one feature
diabetes_X = diabetes.data[:, np.newaxis, 2]

# Split the data into training/testing sets
diabetes_X_train = diabetes_X[:-20]
diabetes_X_test = diabetes_X[-20:]

# Split the targets into training/testing sets
diabetes_y_train = diabetes.target[:-20]
diabetes_y_test = diabetes.target[-20:]

# Create linear regression object
regr = linear_model.LinearRegression()

# Train the model using the training sets
regr.fit(diabetes_X_train, diabetes_y_train)

# Make predictions using the testing set
diabetes_y_pred = regr.predict(diabetes_X_test)

# The coefficients
print('Coefficients: \n',regr.intercept_)
print('Coefficients: \n', regr.coef_)
# The mean squared error
print("Mean squared error: %.2f"
      % mean_squared_error(diabetes_y_test, diabetes_y_pred))
# Explained variance score: 1 is perfect prediction
print('Variance score: %.2f' % r2_score(diabetes_y_test, diabetes_y_pred))

# Plot outputs
plt.scatter(diabetes_X_test, diabetes_y_test,  color='black')
plt.plot(diabetes_X_test, diabetes_y_pred, color='blue', linewidth=3)

plt.xticks(())
plt.yticks(())

plt.show()


Below is the output of running the script on Jupyter notebook:
Code:
Automatically created module for IPython interactive environment
Coefficients:
152.91886182616167
Coefficients:
[938.23786125]
Mean squared error: 2548.07
Variance score: 0.47






Attachments:
LinearResults.png
LinearResults.png [ 13.67 KiB | Viewed 238 times ]

_________________
M. S. Rakha, Ph.D.
Queen's University
Canada
Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Build Linear Regression in Python - Supervised Learning
 Naive Bayes Classification (Binary )- Supervised Learning     -  
 Random Forest Classification (Binary )- Supervised Learning     -  
 Building Quantile regression in R     -  
 linear interpolation array c++     -  
 AES -S-BOX Linear Algorithm implementation-substitution box     -  
 hashing in python     -  
 how to use GeoIP with Python     -  
 Reading email in Python     -  
 Python Module for MySQL     -  
 How to read and write to CSV file from python     -  



Topic Tags

Artificial Intelligence, Python, Machine Learning






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com