RとPythonで医学統計

RとPython初心者医学生

現役医学生がPythonでNumpyやらPandasを使って線形回帰に挑戦してみた

 

www.medicalmed.press

今授業を受けているやつの一部だけJupyter Notebookからとってきました。

【世界で5万人が受講】実践 Python データサイエンス | Udemy

Udemyの授業はわかりやすいのでお勧めです。

Pythonの授業は日本語の講義がいくつかあるので便利です。

ボストンの住宅価格を線形回帰で予測するというのが、下の内容です。

 

In [1]:
import numpy as np
import pandas as pd
from pandas import Series,DataFrame

import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style('whitegrid')
%matplotlib inline
In [3]:
from sklearn.datasets import load_boston
In [4]:
boston = load_boston()
In [5]:
print(boston.DESCR)#Descriptionの略
 
Boston House Prices dataset
===========================

Notes
------
Data Set Characteristics:  

    :Number of Instances: 506 

    :Number of Attributes: 13 numeric/categorical predictive
    
    :Median Value (attribute 14) is usually the target

    :Attribute Information (in order):
        - CRIM     per capita crime rate by town
        - ZN       proportion of residential land zoned for lots over 25,000 sq.ft.
        - INDUS    proportion of non-retail business acres per town
        - CHAS     Charles River dummy variable (= 1 if tract bounds river; 0 otherwise)
        - NOX      nitric oxides concentration (parts per 10 million)
        - RM       average number of rooms per dwelling
        - AGE      proportion of owner-occupied units built prior to 1940
        - DIS      weighted distances to five Boston employment centres
        - RAD      index of accessibility to radial highways
        - TAX      full-value property-tax rate per $10,000
        - PTRATIO  pupil-teacher ratio by town
        - B        1000(Bk - 0.63)^2 where Bk is the proportion of blacks by town
        - LSTAT    % lower status of the population
        - MEDV     Median value of owner-occupied homes in $1000's

    :Missing Attribute Values: None

    :Creator: Harrison, D. and Rubinfeld, D.L.


This dataset was taken from the StatLib library which is maintained at Carnegie Mellon University.

The Boston house-price data of Harrison, D. and Rubinfeld, D.L. 'Hedonic
prices and the demand for clean air', J. Environ. Economics & Management,
vol.5, 81-102, 1978.   Used in Belsley, Kuh & Welsch, 'Regression diagnostics
...', Wiley, 1980.   N.B. Various transformations are used in the table on
pages 244-261 of the latter.

The Boston house-price data has been used in many machine learning papers that address regression
problems.   
     
**References**

   - Belsley, Kuh & Welsch, 'Regression diagnostics: Identifying Influential Data and Sources of Collinearity', Wiley, 1980. 244-261.
   - Quinlan,R. (1993). Combining Instance-Based and Model-Based Learning. In Proceedings on the Tenth International Conference of Machine Learning, 236-243, University of Massachusetts, Amherst. Morgan Kaufmann.


In [7]:
plt.hist(boston.target, bins=50)
plt.xlabel('Price($1,000)')
plt.ylabel('Number of houses')
Out[7]:
<matplotlib.text.Text at 0x1ca49750cf8>
 
In [8]:
plt.scatter(boston.data[:,5], boston.target)
plt.ylabel('Price($1,000)')
plt.xlabel('Number of rooms')
Out[8]:
<matplotlib.text.Text at 0x1ca4982fcc0>
 
In [9]:
boston_df = DataFrame(boston.data)
boston_df.columns = boston.feature_names
In [10]:
boston_df.head()
boston_df['Price']= boston.target
In [12]:
boston_df.head() #Priceを追加した
 
sns.lmplot('RM','Price',data=boston_df)
Out[13]:
<seaborn.axisgrid.FacetGrid at 0x1ca49901828>
 
In [20]:
x = boston_df.RM
x
 
 
In [25]:
x.shape
Out[25]:
(506, 1)
In [41]:
Y = boston_df.Price
In [42]:
x = np.array([[value,1]for value in x])
In [43]:
result= np.linalg.lstsq(x,Y)
In [32]:
a, b = np.linalg.lstsq(x,Y)[0]
In [33]:
plt.plot(boston_df.RM, boston_df.Price, 'o')

x = boston_df.RM
plt.plot(x,a*x+b,'r')
Out[33]:
[<matplotlib.lines.Line2D at 0x1ca499da6a0>]
 
 
今のところPythonよりRの方がやりやすいと感じます。
今月PythonずっとやればRよりも使いやすくなるのかな。