Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
61bd3d4
Atomic Prox/Model
PhilipDeegan Jun 15, 2018
2685f9d
Merge pull request #249 from Dekken/Atomic_model_prox
PhilipDeegan Jun 16, 2018
ad80df9
Addition of examples for yapf - and include cpp-tests in clang format…
PhilipDeegan Jun 19, 2018
9de5268
Merge pull request #256 from Dekken/format_all_the_things
PhilipDeegan Jun 22, 2018
5c63ce0
quicker mac builds on travis
PhilipDeegan Jun 25, 2018
35ad9f2
Merge pull request #259 from Dekken/travis_doctest_timeout
PhilipDeegan Jun 25, 2018
d86b41d
Fix reference counting for deserialized sparsearrays
PhilipDeegan Jun 26, 2018
72767c1
Merge pull request #258 from Dekken/fix_sparse_typemap
PhilipDeegan Jun 26, 2018
1c5b7d3
testing docker and python 3.7 on travis
PhilipDeegan Jul 19, 2018
b769855
Merge pull request #264 from Dekken/python3.7
PhilipDeegan Jul 19, 2018
04bdb8b
Added a new example for 2d linear regression
yerdua1 Jun 14, 2018
28d1061
Merge pull request #265 from Dekken/add-2d-linear-regression-example
PhilipDeegan Jul 19, 2018
586bb6e
Quicker builds on appveyor - stops 1 hour timeout
PhilipDeegan Aug 31, 2018
bd13ed2
Merge pull request #275 from Dekken/appvery
PhilipDeegan Aug 31, 2018
2c775bd
Make ASVRG converge more properly with several threads
Mbompr Aug 30, 2018
88c71a8
Merge pull request #274 from X-DataInitiative/better-asynchronous-svr…
PhilipDeegan Aug 31, 2018
5bb6561
Allow debug pyenv
PhilipDeegan Aug 2, 2018
f838afc
Merge pull request #271 from Dekken/dbg_pyenv
PhilipDeegan Aug 31, 2018
ca3a12e
allow MKL in source build if available
PhilipDeegan Sep 11, 2018
46ea3f8
Merge pull request #276 from Dekken/setmkl
PhilipDeegan Sep 11, 2018
e21a51b
Disable doctest on 3.7 because google_analytics fails in sphynx
PhilipDeegan Sep 16, 2018
c8795f3
Merge pull request #279 from Dekken/sphynx
PhilipDeegan Sep 16, 2018
0024f34
Atomic solvers
PhilipDeegan Sep 16, 2018
4f588d7
Merge pull request #254 from Dekken/atomic_solvers
Mbompr Sep 17, 2018
3e956ce
Record history in C++ to allow sto solvers to be quicker
Mbompr Sep 12, 2018
43508e1
Merge pull request #281 from X-DataInitiative/record-history-in-cpp
Mbompr Sep 20, 2018
3d96d22
SAGA uses AtomicSAGA C++ if threads > 1
PhilipDeegan Sep 20, 2018
1297fac
Merge pull request #286 from Dekken/asaga.py
Mbompr Sep 24, 2018
8606b78
Allow serialization of ModelLabelsFeatureswith sparse features
PhilipDeegan Sep 13, 2018
394b6f2
Finalize serializing sparse features on models with test
PhilipDeegan Sep 24, 2018
2d3fb19
Merge pull request #284 from Dekken/sparsemod
PhilipDeegan Sep 25, 2018
b2a6e90
drop gtests from appveyor - times out - investigate cmake
PhilipDeegan Oct 3, 2018
cd5b3b3
Merge pull request #292 from X-DataInitiative/quickyor
PhilipDeegan Oct 3, 2018
5f6d444
corrections for deserializing third party CSR matrices
PhilipDeegan Oct 8, 2018
9e3b35e
Merge pull request #297 from Dekken/sparsefix
PhilipDeegan Oct 8, 2018
11f3322
Remove requirement for cereal JSON archives in Python/etc
PhilipDeegan Oct 9, 2018
ed11dbc
Merge pull request #300 from Dekken/jsless
PhilipDeegan Oct 9, 2018
c762fa4
Set swig to use relative paths and just use a single -Ilib/swig for s…
PhilipDeegan Oct 2, 2018
513e788
Merge pull request #291 from Dekken/swig
PhilipDeegan Oct 9, 2018
ff3253e
C++ google tests working on appveyor
PhilipDeegan Oct 4, 2018
9e73a82
Merge pull request #293 from Dekken/quickyor
PhilipDeegan Oct 9, 2018
a624425
Added ability to choose custom node names in plot_point_process
timleathart Oct 10, 2018
9e80ca1
Merge pull request #302 from timleathart/master
Mbompr Oct 10, 2018
be90ec7
Sparse/Dense column major arrays
PhilipDeegan Oct 3, 2018
98005d5
Merge pull request #261 from Dekken/col_maj
PhilipDeegan Oct 17, 2018
2ac7de3
GCC 4.8 has problems with lambdas, switching to function version
PhilipDeegan Oct 12, 2018
23cea11
Merge pull request #304 from Dekken/asaga_lambda
PhilipDeegan Oct 18, 2018
d2adc73
Rearrange vector operations for simple updates
PhilipDeegan Oct 12, 2018
493a357
Merge pull request #295 from Dekken/gpu
PhilipDeegan Oct 18, 2018
715463b
Add linear system solver to array
Mbompr Oct 3, 2018
4363049
Merge pull request #294 from X-DataInitiative/add-linear-system-to-array
Mbompr Oct 19, 2018
87fecd3
Added optional rotation of x_ticklabels for plot_hawkes_kernel_norms
Oct 11, 2018
0ca995b
Merge pull request #310 from Dekken/x_ticklabels
Mbompr Oct 23, 2018
da66623
v0.5.0 release
PhilipDeegan Nov 8, 2018
9b04948
Merge pull request #312 from X-DataInitiative/v0.5.0
PhilipDeegan Nov 9, 2018
b8e5a90
ModelHawkes.__init__ must be called last in init to set default dtype…
PhilipDeegan Nov 21, 2018
c11243e
Merge pull request #315 from Dekken/hawkes_dtype_order
PhilipDeegan Nov 21, 2018
09a15a9
Test GPG fix for Travis/OSX
PhilipDeegan Jan 4, 2019
9888bc0
Merge pull request #319 from Dekken/travis_rvm
PhilipDeegan Jan 4, 2019
1b24fe9
Three in one for CI
PhilipDeegan Jan 23, 2019
2e0ef60
Merge pull request #331 from Dekken/CI_Fixing
PhilipDeegan Jan 28, 2019
21e14d0
Check for MKL, don't use if not found
PhilipDeegan Jan 29, 2019
94a1b6c
Merge pull request #317 from X-DataInitiative/mkl_header_check
PhilipDeegan Jan 30, 2019
bbecb69
Add fetch url dataset helper
Mbompr Feb 15, 2019
c6356c3
Merge pull request #336 from X-DataInitiative/fetch-url-dataset
PhilipDeegan Feb 15, 2019
c08d2a7
Re-organize benchmarks
Mbompr Feb 15, 2019
e7d10e2
Merge pull request #337 from X-DataInitiative/reorganize-benchmarks
PhilipDeegan Feb 17, 2019
e8bbedb
-Werror signed/unsigned
PhilipDeegan Jan 30, 2019
55178bd
Merge pull request #333 from Dekken/Werror
PhilipDeegan Feb 18, 2019
f62fff0
Serialization and comparaison for hawkes models
andro2157 Jun 11, 2018
6ad84c5
Merge pull request #250 from andro2157/hawkes_serialization
PhilipDeegan Mar 5, 2019
6ab8494
Main thread hangs when interrupted, exit is required
PhilipDeegan Mar 11, 2019
8969881
Merge pull request #342 from X-DataInitiative/exit_after_interrupt
PhilipDeegan Mar 16, 2019
90d64a9
Update SimuSCCS doc (fix typos and fix naming for exposure_type arg) …
MaryanMorel Mar 11, 2019
eae49b1
Merge pull request #343 from X-DataInitiative/SimuSCCS-doc-fix
PhilipDeegan Mar 16, 2019
1416c93
add/fix serialization for proxs
PhilipDeegan Mar 20, 2019
cd284cd
Merge pull request #348 from Dekken/serial
PhilipDeegan Mar 21, 2019
0ad0621
swig v4
PhilipDeegan May 4, 2019
3f27be1
Merge pull request #356 from X-DataInitiative/swig4
PhilipDeegan May 15, 2019
b266727
change comb import to new non deprecated version
PhilipDeegan May 27, 2019
b2052c8
Merge pull request #366 from Dekken/scipy_comb
PhilipDeegan May 27, 2019
2883b33
shared_ptr vibilitiy required for pointer tracking
PhilipDeegan May 29, 2019
eec1ac5
Merge pull request #367 from Dekken/leak
PhilipDeegan May 29, 2019
3ed7ba1
adding features relatively to the binacox model
SimonBussy Apr 16, 2019
ea27359
Merge pull request #369 from Dekken/binacox_squash
stephanegaiffas Jun 4, 2019
3b072fd
Python 3.6.8/3.7.3 - library order loading rejiggered. Sphinx updated
PhilipDeegan Jul 2, 2019
0517d1d
Merge pull request #375 from Dekken/python37
PhilipDeegan Jul 2, 2019
d5657ed
set Swig version required to 4 - provide simple install script
PhilipDeegan Jun 28, 2019
dcfe2d9
Merge pull request #374 from Dekken/swig4
PhilipDeegan Jul 6, 2019
15e95af
GCC8 Werror changes + SWIG new version required!
PhilipDeegan Dec 18, 2018
3b7b400
Merge pull request #379 from Dekken/gcc8
PhilipDeegan Jul 13, 2019
a7c9d08
Replace 'SO' with 'EXT_SUFFIX', 'SO' is deprecated
PhilipDeegan Jul 30, 2019
15b8fb0
Merge pull request #385 from Dekken/pydeprecated
PhilipDeegan Sep 2, 2019
57f1b84
load dep libs if not loaded
PhilipDeegan Jul 23, 2019
f845c25
Merge pull request #384 from Dekken/survival_import
PhilipDeegan Sep 3, 2019
1175859
Longitudinal preprocessor multithreading
andro2157 Jun 20, 2019
1d4f1a9
allow benchmarking without gtest
PhilipDeegan Mar 21, 2019
be79950
Block the command "pytest" from setup.py on Windows as it has issues (it
PhilipDeegan Jul 12, 2019
7964114
batch and stream parallel kfold and bootstrap
PhilipDeegan Jun 4, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -50,3 +50,6 @@ XCode/
# Build directory for C++ tests
cpp_test_build/
lib/cmake-build-debug

# benchmark data
tools/benchmark/data
19 changes: 8 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,25 @@ matrix:
- os: linux
services: docker
env:
- DOCKER_IMAGE=xdatainitiative/tick_ubuntu:1.4
- PYVER=3.5.3
- DOCKER_IMAGE=xdatainitiative/tick_debian:0.5.1
- PYVER=3.6.8
- os: linux
services: docker
env:
- DOCKER_IMAGE=xdatainitiative/tick_ubuntu:1.4
- PYVER=3.6.1
- DOCKER_IMAGE=xdatainitiative/tick_debian:0.5.1
- PYVER=3.7.3
- os: osx
language: generic
sudo: required
env:
- PYVER=3.5.3
osx_image: xcode9.1
- PYVER=3.6.8
osx_image: xcode10.2
- os: osx
language: generic
sudo: required
env:
- PYVER=3.6.1
osx_image: xcode9.1

before_install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then rvm get stable; fi
- PYVER=3.7.3
osx_image: xcode10.2

install:
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then source tools/travis/osx_install.sh; fi
Expand Down
38 changes: 34 additions & 4 deletions INSTALL.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

### Requirements

_tick_ currently works on Linux/OSX (Windows is experimental) systems and requires Python 3.5 or newer. Please have the required Python dependencies in your Python environment:
_tick_ currently works on Linux/OSX (Windows is experimental) systems and requires Python 3.6 or newer. Please have the required Python dependencies in your Python environment:

- numpy
- scipy
Expand All @@ -17,11 +17,21 @@ If you build and install _tick_ via _pip_ these dependencies will automatically

pip install -r requirements.txt

[Swig](http://www.swig.org/Doc3.0/SWIGDocumentation.html) might also be necessary if precompiled binaries are not available for your distribution.
[Swig](http://www.swig.org/Doc4.0/SWIGDocumentation.html) might also be necessary if precompiled binaries are not available for your distribution.

### Source installations

For source installations, a C++ compiler capable of compiling C++11 source code (such as recent versions of [gcc](https://gcc.gnu.org/) or [clang](https://clang.llvm.org/)) must be available. In addition, SWIG version 3.07 or newer must also be available.
For source installations, a C++ compiler capable of compiling C++11 source code (such as recent versions of [gcc](https://gcc.gnu.org/) or [clang](https://clang.llvm.org/)) must be available. In addition, SWIG version 4.0 or newer must also be available. It is also recommended to build swig if necessary without PCRE

The following script is provided as is with not assurance for working, and may require "sudo" permissions:

git clone https://github.com/swig/swig -b rel-4.0.0 swig && \
cd swig && ./autogen.sh && ./configure --without-pcre && \
make && make install

It also may require the following (styled for a Debian type system)

apt-get install -y autotools-dev automake gawk bison flex

### Install using _pip_

Expand Down Expand Up @@ -56,7 +66,7 @@ This will build all required C++ extensions, and install the extensions in your
Download python and swig, and add their respective installation directories to the PATH environment variable.

python 3.6 - https://www.python.org/downloads/windows/
swigwin 3.0.* - http://www.swig.org/download.html
swigwin 4.0.* - http://www.swig.org/download.html

Download wheel files from http://www.lfd.uci.edu/~gohlke/pythonlibs - this is not required for Anaconda

Expand Down Expand Up @@ -113,6 +123,19 @@ This will build all extensions, and install them directly in the source director
Note also that special scripts, intended for developers, are available. `./clean_build_test.sh` removes all compiled binaries and runs the full compilation process, with all `C++` and `Python` unit-tests. This can take some time.
Similarly, `./build_test.sh` runs the full compilation process, without removing first binaries, while `full_clean.sh` only removes them.

It is possible to build with support for Intel MKL in the tick C++ libraries, to do so requires having the intel MKL libraries installed and running the build command like so (example provided is for linux with MKL installed to /opt/intel)

MKLROOT=/opt/intel/mkl python setup.py build_ext --inplace

On MacOS it's possible an error may occur when trying to find a "intel_thread.dyld"
To fix this try:

export DYLD_LIBRARY_PATH="/opt/intel/lib:$DYLD_LIBRARY_PATH"

Replacing "/opt/intel/lib" to "$MKLROOT/../lib" if necessary.

Upgrading to the latest version of MKL may also resolve this.

### Alternative installation method

Also supported is the build tool [maiken](https://github.com/dekken/maiken) which works with various compilers on major platforms.
Expand All @@ -138,3 +161,10 @@ Logging from mkn can be enabled with the "KLOG" environment variable
KLOG=3 # DEBUG

If there is a compile error it should be displayed without KLOG being set.


### Python 3.5

Python 3.5 is not officially supported any more, but it should still work.
To build with it, edit "python_min_ver = (3, 6, 0)" in setup.py.

2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Copyright (c) 2015-2017, the tick developers
Copyright (c) 2015-2018, the tick developers
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
Expand Down
40 changes: 5 additions & 35 deletions appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,50 +29,20 @@ cache:

install:
- git submodule update --init
- curl -o mkn.exe -L https://ci.appveyor.com/api/projects/dekken/maiken/artifacts/bin/build/mkn.exe
- IF NOT EXIST C:\ProgramData\chocolatey\bin\swig.exe choco install tools\windows\swig.3.0.12.nupkg --yes --limit-output #> $null
- curl -o mkn.exe -L https://github.com/Dekken/maiken/raw/binaries/win10_x64/mkn.exe
- IF NOT EXIST C:\ProgramData\chocolatey\bin\swig.exe choco install tools\windows\swig.4.0.0.nupkg --yes --limit-output #> $null
- cat C:\ProgramData\chocolatey\logs\chocolatey.log
- git clone https://github.com/X-DataInitiative/tick_appveyor -b master --depth 1 appveyor

build_script:
- cp "C:\Program Files (x86)\Windows Kits\10\bin\x64\rc.exe" %CD%
- cp %CD%\rc.exe %CD%\lib
- SET MKN_CL_PREFERRED=1 # forces mkn to use cl even if gcc/clang are found
- SET MKN_COMPILE_THREADS=1 # mkn use 1 thread heap space issue
- SET SWIG=0 # disables swig for mkn
- call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvars64.bat"

- cl # print compiler version
# doesnt exist for somereason?
- mkdir -p "C:\Program Files (x86)\Windows Kits\10\include\10.0.16299.0\cppwinrt"

# Python 3.7 TODO

# Python 3.6
- cat appveyor/pip/numpy/numpy-1.14.0+mkl-cp36* > appveyor/pip/numpy-1.14.0+mkl-cp36-cp36m-win_amd64.whl
- C:\Python36-x64\Scripts\pip install appveyor\pip\numpy-1.14.0+mkl-cp36-cp36m-win_amd64.whl
- C:\Python36-x64\Scripts\pip install appveyor\pip\numpydoc-0.7.0-py2.py3-none-any.whl
- C:\Python36-x64\Scripts\pip install appveyor\pip\scipy-1.0.0-cp36-cp36m-win_amd64.whl
- C:\Python36-x64\Scripts\pip install -r requirements.txt
- C:\Python36-x64\Scripts\pip install tensorflow
- C:\Python36-x64\python setup.py build_ext --inplace
- C:\Python36-x64\python -m unittest discover -v . "*_test.py"
- bash -c "PATH=\"$PWD:/c/Python36-x64/:/c/Program Files/Git/usr/bin:$PATH\"; sh/mkn.sh"
- bash -c "PATH=\"$PWD:/c/Python36-x64/:/c/Program Files/Git/usr/bin:$PATH\"; sh/gtest.sh"
- C:\Python36-x64\python setup.py bdist_wheel
- rm -rf build
- rm -rf lib\bin

# # Python 3.5 # with addition of gtests appveyor is taking more than an hour and timing out
# - cat appveyor/pip/numpy/numpy-1.14.0+mkl-cp35* > appveyor/pip/numpy-1.14.0+mkl-cp35-cp35m-win_amd64.whl
# - C:\Python35-x64\Scripts\pip install appveyor\pip\numpy-1.14.0+mkl-cp35-cp35m-win_amd64.whl
# - C:\Python35-x64\Scripts\pip install appveyor\pip\numpydoc-0.7.0-py2.py3-none-any.whl
# - C:\Python35-x64\Scripts\pip install appveyor\pip\scipy-1.0.0-cp35-cp35m-win_amd64.whl
# - C:\Python35-x64\Scripts\pip install -r requirements.txt
# - C:\Python35-x64\Scripts\pip install tensorflow
# - C:\Python35-x64\python setup.py build_ext --inplace
# - C:\Python35-x64\python -m unittest discover -v . "*_test.py"
# - C:\Python35-x64\python setup.py bdist_wheel
# - rm -rf build
# - rm -rf lib\bin
- bash -c "PATH=\"$PWD:/c/Python37-x64/:/c/Program Files/Git/usr/bin:$PATH\"; tools/appveyor/run.sh"

artifacts:
- path: "dist\\*.whl"
Expand Down
6 changes: 3 additions & 3 deletions doc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

import sys
import os

import sphinxext.google_analytics
try:
import sphinx_bootstrap_theme
use_bootstrap_theme = True
Expand Down Expand Up @@ -92,9 +92,9 @@
# built documents.
#
# The short X.Y version.
version = '0.4'
version = '0.5'
# The full version, including alpha/beta/rc tags.
release = '0.4.0'
release = '0.5.0'

# The language for content autogenerated by Sphinx. Refer to documentation
# for a list of supported languages.
Expand Down
5 changes: 4 additions & 1 deletion doc/modules/dev.rst
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ Create SWIG file
~~~~~~~~~~~~~~~~

Now that our proximal operator is defined in C++ we need to make it available
in Python. We do it thanks to `SWIG <http://www.swig.org/Doc3.0/>`_.
in Python. We do it thanks to `SWIG <http://www.swig.org/Doc4.0/>`_.
Hence we have to create a .i file. In tick we store them in the lib/swig folder.

This .i file looks a lot like our .h file.
Expand Down Expand Up @@ -515,6 +515,9 @@ serialized copy of the object, and a `__setstate__` method to reconstruct the
object from a string value (this is where the initialization/constructor
parameters play in).

Similarly, `TICK_MAKE_TEMPLATED_PICKLABLE` is provided if needed to specify
a type with templated parameters.

It's important to consider the initialization of the Python object. In some
cases it might be convenient to add a parameter-less C++ constructor that
initializes an empty object. Otherwise existing constructors should be used.
Expand Down
2 changes: 0 additions & 2 deletions doc/modules/solver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ which outputs

Launching the solver SVRG...
n_iter | obj | rel_obj
0 | ... | ...
1 | ... | ...
2 | ... | ...
3 | ... | ...
Expand All @@ -73,7 +72,6 @@ which outputs

Launching the solver SVRG...
n_iter | obj | rel_obj
0 | 5.29e-01 | 2.37e-01
1 | 5.01e-01 | 5.15e-02
2 | 4.97e-01 | 8.44e-03
3 | 4.97e-01 | 5.00e-04
Expand Down
2 changes: 2 additions & 0 deletions doc/modules/survival.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Kaplan-Meier estimators.
nelson_aalen
kaplan_meier
ConvSCCS
sccs.BatchConvSCCS
sccs.StreamConvSCCS

2. Models
=========
Expand Down
2 changes: 1 addition & 1 deletion doc/sphinxext/google_analytics.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
https://bitbucket.org/birkenfeld/sphinx-contrib/src/e758073384efd1ed5ed1e6286301b7bef71b27cf/googleanalytics/
"""

from sphinx.application import ExtensionError
from sphinx.errors import ExtensionError


def add_ga_javascript(app, pagename, templatename, context, doctree):
Expand Down
107 changes: 107 additions & 0 deletions examples/plot_2d_linear_regression.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
"""
===================================
Linear regression in two dimensions
===================================

In this example, we try to predict the median price of houses in Boston's
neighbors by looking at two features: the average of the number of rooms per
dwelling and the pencentage of low status in the population.
The linear regression is done using the `Boston Housing Dataset`_ which
contains 13 features used to predict the median price of houses.
The two features selected are the most efficent on the test set.
The 3D representation allows a better understanding of the prediction
mechanism with two features.

This example is inspired by linear regression example from
`scikit-learn documentation`_.

.. _Boston Housing Dataset: https://www.kaggle.com/c/boston-housing
.. _scikit-learn documentation: http://scikit-learn.org/stable/auto_examples/linear_model/plot_ols.html#sphx-glr-auto-examples-linear-model-plot-ols-py
"""

import matplotlib.pyplot as plt
import numpy as np
from tick import linear_model
from sklearn.metrics import mean_squared_error, r2_score
from sklearn.utils import shuffle
from sklearn.datasets import load_boston
from mpl_toolkits.mplot3d import axes3d
from matplotlib import cm

# Load the Boston Housing Dataset
features, label = load_boston(return_X_y=True)
features, label = shuffle(features, label, random_state=0)

# Use two features: the average of the number of rooms per dwelling and
# the pencentage of low status of the population
X = features[:, [5, 12]]

# Split the data into training/testing sets
n_train_data = int(0.8 * X.shape[0])

X_train = X[:n_train_data]
X_test = X[n_train_data:]

y_train = label[:n_train_data]
y_test = label[n_train_data:]

# Create linear regression and fit it on the training set
regr = linear_model.LinearRegression()
regr.fit(X_train, y_train)

# Make predictions using the testing set
y_pred = regr.predict(X_test)

print('Coefficients:')
print(' intercept: {:.2f}'.format(regr.intercept))
print(' average room per dwelling: {:.2f}'.format(regr.weights[0]))
print(' percentage of low status in population: {:.2f}'
.format(regr.weights[1]))

# The mean squared error
print('Mean squared error on test set: {:.2f}'.format(
mean_squared_error(y_test, y_pred)))

# Explained variance score: 1 is perfect prediction
print('Variance score on test set: {:.2f}'.format(r2_score(y_test, y_pred)))
# To work in 3D

# We first generate a mesh grid
resolution = 10
x = X_test[:, 0]
y = X_test[:, 1]
z = y_test

x_surf = np.linspace(min(x), max(x), resolution)
y_surf = np.linspace(min(y), max(y), resolution)
x_surf, y_surf = np.meshgrid(x_surf, y_surf)

# and then predict the label for all values in the grid
z_surf = np.zeros_like(x_surf)
mesh_points = np.vstack((x_surf.ravel(), y_surf.ravel())).T
z_surf.ravel()[:] = regr.predict(mesh_points)

fig = plt.figure(figsize=(20, 5))

# 3D representation under different rotated angles for a better visualazion
xy_angles = [10, 35, 60, 85]
z_angle = 20

for i, angle in enumerate(xy_angles):
n_columns = len(xy_angles)
position = i + 1

ax = fig.add_subplot(1, n_columns, position, projection='3d')

ax.view_init(z_angle, angle)

ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot, rstride=1, cstride=1,
alpha=0.3, linewidth=0.2, edgecolors='black')
ax.scatter(x, y, z)

ax.set_title('angle: {}°'.format(angle))
ax.set_zlabel('median house pricing')
ax.set_xlabel('avg room per dwelling')
ax.set_ylabel('% low status population')

plt.show()
Loading