pythonscikit教程(用Python做科学计算工具篇)
pythonscikit教程(用Python做科学计算工具篇)>>>>>> print(data.DESCR) 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 pro
所需基本库
- numpy
- scipy
- matplotlib
- 简介:问题设置
- 使用 scikit-learn 进行机器学习的基本原理
- 监督学习:手写数字的分类
- 监督学习:住房数据的回归
- 测量预测性能
- 无监督学习:降维和可视化
- 特征脸示例:链接 PCA 和 SVM
- 特征脸示例:链接 PCA 和 SVM
- 参数选择、验证和测试
6.4.监督学习:住房数据的回归
在这里,我们将做一个回归问题的简短示例:从一组特征中学习一个连续值。
6.4.1. 快速浏览数据我们将使用 scikit-learn 中提供的简单波士顿房价集。这记录了波士顿周围房地产市场的 13 个属性的测量值,以及中位数价格。问题是:你能根据新市场的属性预测其价格吗?
>>>
>>> from sklearn.datasets import load_boston
>>> data = load_boston()
>>> print(data.data.shape)
(506 13)
>>> print(data.target.shape)
(506 )
我们可以看到只有 500 多个数据点。
该DESCR变量对数据集有很长的描述:
>>>
>>> print(data.DESCR)
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
...
它通常有助于使用直方图、散点图或其他绘图类型快速可视化数据片段。使用 matplotlib,让我们显示目标值的直方图:每个邻域的中位数价格:
>>>
>>> plt.hist(data.target)
(array([...
让我们快速看一下某些特征是否比其他特征更适合我们的问题:
>>>
>>> for index feature_name in enumerate(data.feature_names):
... plt.figure()
... plt.scatter(data.data[: index] data.target)
<Figure size...
有时,在机器学习中,使用特征选择来决定哪些特征对特定问题最有用是很有用的。现有的自动化方法可以量化这种选择信息量最大的特征的练习。
6.4.2. 预测房价:一个简单的线性回归现在我们将使用scikit-learn对住房数据执行简单的线性回归。有许多使用回归量的可能性。一个特别简单的是LinearRegression:这基本上是一个普通最小二乘计算的包。
>>>
>>> from sklearn.model_selection import train_test_split
>>> X_train X_test y_train y_test = train_test_split(data.data data.target)
>>> from sklearn.linear_model import LinearRegression
>>> clf = LinearRegression()
>>> clf.fit(X_train y_train)
LinearRegression(copy_X=True fit_intercept=True n_jobs=1 normalize=False)
>>> predicted = clf.predict(X_test)
>>> expected = y_test
>>> print("RMS: %s" % np.sqrt(np.mean((predicted - expected) ** 2)))
RMS: 5.0059...
我们可以绘制误差:预期作为预测的函数:
>>>
>>> plt.scatter(expected predicted)
<matplotlib.collections.PathCollection object at ...>
尽管显然存在一些偏差,但该预测至少与真实价格相关。我们可以想象通过计算真实价格和预测价格之间的 RMS 残差来评估回归器的性能。然而,其中有一些微妙之处,我们将在后面的部分中介绍。
from sklearn.ensemble import GradientBoostingRegressor
# Instantiate the model fit the results and scatter in vs. out
相关文章:
- 用Python做科学计算(工具篇)——scikit-learn(机器学习)3
- 用Python做科学计算(工具篇)——scikit-learn(机器学习)2
- 用Python做科学计算(工具篇)——scikit-learn(机器学习)1
- 用Python做科学计算——matplotlib绘图实例
- 用Python做科学计算(工具篇)——1.1. NumPy 数组对象
- 用Python做科学计算(工具篇)——numpy1.2.数组的数值运算
- 用Python做科学计算(工具篇)——numpy1.3 更精细的数组
- 用Python做科学计算(工具篇)——numpy1.4 高级操作
- 用Python做科学计算(工具篇)——scipy 使用指南
- 用Python做科学计算(工具篇)——sympy使用指南(符号运算)