进阶任务四

【Task4 Linear Regression(多变量)】(2天)

  1. 波士顿房产数据(完整数据)
    在这里插入图片描述
  2. 实现多变量(手写代码)
    import numpy as np
    x=[[1,10,1],[1,20,1],[1,30,1],[1,30,2],[1,70,3],[1,70,2]]
    x=np.mat(x)
    print(x)
    print(x[1])
    print(x.T[1])#取单个x特征
    y=[0.8,1.2,2.2,2.5,5.5,5.2]
    h=lambda th0,th1,th2,x:th0+th1x[1]+th2x[2]
    print(h(1,1,2,[1,10,1]))
    y=np.mat(y).T#y转换为列向量
    th=np.mat([0.1,0.2,0.3]).T#thta转为列向量
    y_th0=lambda th,x,y:np.sum(xth-y)
    print(y_th0(th,x,y))
    y_th1=lambda th,x,y:np.sum(x.T[1]
    (xth-y))
    print(y_th1(th,x,y))
    y_th2=lambda th,x,y:np.sum(x.T[2]
    (xth-y))
    y_th2(th,x,y)
    lr=0.0001
    for _ in range(100000):
    th0=float(th[0])-lr
    y_th0(th,x,y)
    th1=float(th[1])-lry_th1(th,x,y)
    th2=float(th[2])-lr
    y_th2(th,x,y)
    th=np.mat([[th0],[th1],[th2]])
    print(th)
    th=(x.Tx).Ix.T*y
    th
  3. 数据标准化(手写代码)
    ss=StandardScaler()
    ss.fit(x_train.y_train)
  4. 网格搜索调参
    5 from sklearn.linear_model import LinearRegression对比
    import numpy as np
    import matplotlib as mpl
    import matplotlib.pyplot as plt
    import pandas as pd
    import warnings
    import sklearn
    from sklearn.linear_model import LinearRegression, LassoCV, RidgeCV, ElasticNetCV
    from sklearn.preprocessing import PolynomialFeatures
    from sklearn.pipeline import Pipeline
    from sklearn.linear_model.coordinate_descent import ConvergenceWarning
    from sklearn.model_selection import train_test_split
    from sklearn.preprocessing import StandardScaler
    from sklearn.model_selection import GridSearchCV
    from sklearn import metrics
    path = ‘E:\AI10\housing.data’
    fd = pd.read_csv(path,header=None,names=names)
    fd.head()
    x, y = np.split(data, (13,), axis=1)
    print (x[0:5])
    y = y.ravel()
    models = [
    Pipeline([
    (‘ss’, StandardScaler()),
    (‘poly’, PolynomialFeatures()),
    (‘linear’, RidgeCV(alphas=np.logspace(-3,1,20)))
    ])
    ]
    parameters = {
    “linear__fit_intercept”: [True, False]
    }
    model = GridSearchCV(models[t], param_grid=parameters,cv=5, n_jobs=1)
    model.fit(x_train, y_train)
    print ("%s算法:最优参数:" % titles[t],model.best_params_)
    print ("%s算法:R值=%.3f" % (titles[t], model.best_score_))
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值