机器学习HelloWrold:从零开始到模型训练预测房价

编写不易,转载请注明( http://shihlei.iteye.com/blog/2386498)
一 概述
    基于python 从零开始,完成环境搭建,训练模型,预测房价。
    
    环境:
        1)运行环境
        Miniconda3:包管理器
        python :3.6     
        
        2)lib:
            jupyter:python 开发环境,deme 主要的集成开发环境
            numpy:科学计算库,用于高性能矩阵计算,(暂未使用)
            patplotlib:2D绘图库,demo 中绘图,方便查看模型近似
            pandas: 数据分析库,demo 中用于检测操作训练数据,未进行各种数值统计
            scikit-learn: 机器学习库,demo 进行模型训练
 
    其他:
        默认大家了解机器学习的本质,什么是回归。
 
二 环境搭建
    1) Miniconda3
    (a) 安装
     Miniconda3 是类似于pip,easy_install, 是 anaconda 的简化版,一般都是装anaconda ,不想装无用的lib,所以选择Miniconda ;
     网站:  https://conda.io/miniconda.html,我mac 环境,下载个shell ,bash即可,都是下一步,下一步
     装完,家目录下存在 miniconda3 目录
 
Do you approve the license terms? [yes|no]
>>> yes

Miniconda3 will now be installed into this location:
/Users/xxx/miniconda3

  - Press ENTER to confirm the location
  - Press CTRL-C to abort the installation
  - Or specify a different location below

[/Users/xxx/miniconda3] >>>
PREFIX=/Users/xxx/miniconda3

installing: python-3.6.1-2 ...
installing: asn1crypto-0.22.0-py36_0 ...
installing: cffi-1.10.0-py36_0 ...
installing: conda-env-2.6.0-0 ...
installing: cryptography-1.8.1-py36_0 ...
installing: idna-2.5-py36_0 ...
installing: openssl-1.0.2l-0 ...
installing: packaging-16.8-py36_0 ...
installing: pycosat-0.6.2-py36_0 ...
installing: pycparser-2.17-py36_0 ...
installing: pyopenssl-17.0.0-py36_0 ...
installing: pyparsing-2.1.4-py36_0 ...
installing: readline-6.2-2 ...
installing: requests-2.14.2-py36_0 ...
installing: ruamel_yaml-0.11.14-py36_1 ...
installing: setuptools-27.2.0-py36_0 ...
installing: six-1.10.0-py36_0 ...
installing: sqlite-3.13.0-0 ...
installing: tk-8.5.18-0 ...
installing: xz-5.2.2-1 ...
installing: yaml-0.1.6-0 ...
installing: zlib-1.2.8-3 ...
installing: conda-4.3.21-py36_0 ...
installing: pip-9.0.1-py36_1 ...
installing: wheel-0.29.0-py36_0 ...
Python 3.6.1 :: Continuum Analytics, Inc.
creating default environment...
installation finished.
Do you wish the installer to prepend the Miniconda3 install location
to PATH in your /Users/xxx/.bash_profile ? [yes|no]
[yes] >>>
Prepending PATH=/Users/xxx/miniconda3/bin to PATH in /Users/xxx/.bash_profile
A backup will be made to: /Users/xxx/.bash_profile-miniconda3.bak


For this change to become active, you have to open a new terminal.

Thank you for installing Miniconda3!

Share your notebooks and packages on Anaconda Cloud!
Sign up for free: https://anaconda.org
 
      (b)使用
# 更新
conda update conda

# 搜索软件
conda search -n python36 xxx

# 查看某个环境已经安装的软件
conda list -n python36 xxx
 
         2)安装python 环境
   
# 创建pyton36环境,版本3.6 , conda 自动寻找最新的3.6.x
conda create --name pytho36 python=3.6

# 激活某个环境,其实就是path 换一下指向的版本
source activate python36
 
注:
    (1)使用 conda 可以很方便的安装多个版本的python,和各个python 版本切换。我Python 2.7,3.6都装了
    (2)删除python 环境:conda remove --name python36 --all
 
    2)安装依赖lib

 

conda install -n python36 jupyter

conda install -n python36 matplotlib
conda install -n python36 numpy
conda install -n python36 pandas
conda install -n python36 scikit-learn

 

 
三 开发工具
    使用web 版的jupyter 进行开发,
 
   1) 启动方式:
jupyter notebook --port 8888
    2)使用
(1)新建编辑器

  
(2)编写脚本


     
 3)其他
 notebook的两种模式:
 (a)命令模式 (按键 Esc 开启)
L : 调出行号
 (b) 编辑模式 ( Enter 键启动)
Tab : 代码补全或缩进
Ctrl-Enter : 运行本单元
 
四 试试各个lib
1)matplotlib 画图
import numpy as np
import matplotlib.pyplot as plt

x = [1,2,3,4,5,6,7]
y = np.sin(x)

print(y)
#  设置执行x的值和y的值
plt.plot(x,y)
# 打印x,y 的曲线
plt.show()

 

 

 
2)pandas 
import numpy as np
import pandas as pd

#随机一个3x4 矩阵
table = np.random.randn(3,4)
print('table: ',table)

#设置title
title = ['a','b','c','d']

#构建pandas DataFrame
df = pd.DataFrame(table,columns=title)
print(df)

#试验把矩阵转置
tdf = df.T
print(tdf)


 
五 机器学习代码
import pandas as pd
import matplotlib.pyplot as plt
import sklearn.linear_model

#测试数据集,一个房屋大小,一个价格,随便写的
room_size_price_data = [[100,200],[105,211],[200,300],[111,250]]
room_df = pd.DataFrame(room_size_price_data,columns=['size','price'])
size_data = room_df.loc[:,['size']].values
price_data = room_df['price'].values

# 打印训练集数据
print(room_df)

# 训练模型,注意:size_data必须是二维数组,因为特征向量也是数组,一维数组这里会出错误
regr = linear_model.LinearRegression()
regr.fit(size_data,price_data)

# 查看对比图
plt.xlabel('Size')
plt.ylabel('Price')
plt.title('linear regression plot!')
plt.scatter(size_data,price_data,color='blue')
plt.plot(size_data,regr.predict(size_data),color="green")
plt.show()

# 预测
need_predict_size=[400,500]
for size in need_predict_size:    
    predict_price = regr.predict(size)
    print("size: %d, price : %d" % (size,predict_price))
 
结果:



 

猜你喜欢

转载自shihlei.iteye.com/blog/2386498