首页 > 其他分享 >实验1-波士顿房价预测

实验1-波士顿房价预测

时间:2024-04-25 23:55:45浏览次数:16  
标签:score r2 房价 train 实验 test import 波士顿 sklearn

实验1-波士顿房价预测

1

from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, LogisticRegression
# from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error
# from sklearn.externals import joblib
import joblib
from sklearn.metrics import r2_score
from sklearn.neural_network import MLPRegressor

import pandas as pd
import numpy as np

# lb = load_boston()
# x_train, x_test, y_train, y_test = train_test_split(lb.data, lb.target, test_size=0.2)

data_url = "http://lib.stat.cmu.edu/datasets/boston"
raw_df = pd.read_csv(data_url, sep="\s+", skiprows=22, header=None)
data = np.hstack([raw_df.values[::2, :], raw_df.values[1::2, :2]])
target = raw_df.values[1::2, 2]

x_train, x_test, y_train, y_test = train_test_split(data, target, test_size=0.2)


# 为数据增加一个维度,相当于把[1, 5, 10] 变成 [[1, 5, 10],]
y_train = y_train.reshape(-1, 1)
y_test = y_test.reshape(-1, 1)

# 进行标准化
std_x = StandardScaler()
x_train = std_x.fit_transform(x_train)
x_test = std_x.transform(x_test)

std_y = StandardScaler()
y_train = std_y.fit_transform(y_train)
y_test = std_y.transform(y_test)

# 正规方程预测
lr = LinearRegression()
lr.fit(x_train, y_train)
print("r2 score of Linear regression is",r2_score(y_test,lr.predict(x_test)))

2

#回归
from sklearn.linear_model import RidgeCV

cv = RidgeCV(alphas=np.logspace(-3, 2, 100))
cv.fit (x_train , y_train)
print("r2 score of Linear regression is",r2_score(y_test,cv.predict(x_test)))

3

#梯度下降
sgd = SGDRegressor()
sgd.fit(x_train, y_train)
print("r2 score of Linear regression is",r2_score(y_test,sgd.predict(x_test)))

4

from keras.models import Sequential
from keras.layers import Dense

#基准NN
#使用标准化后的数据
seq = Sequential()
#构建神经网络模型
#input_dim来隐含的指定输入数据shape
seq.add(Dense(64, activation='relu'))
seq.add(Dense(64, activation='relu'))
seq.add(Dense(1, activation='relu'))
seq.compile(optimizer='rmsprop', loss='mse', metrics=['mae'])
seq.fit(x_train, y_train,  epochs=300, batch_size = 16, shuffle = False)
score = seq.evaluate(x_test, y_test,batch_size=16) #loss value & metrics values
print("score:",score)
print('r2 score:',r2_score(y_test, seq.predict(x_test)))

标签:score,r2,房价,train,实验,test,import,波士顿,sklearn
From: https://www.cnblogs.com/hmy22466/p/18146752

相关文章

  • 20211317 李卓桐 Exp5 信息搜集与漏洞扫描 实验报告
    Exp5信息搜集与漏洞扫描实验报告1、实践目标掌握信息搜集的最基础技能与常用工具的使用方法。2、实践内容(1)各种搜索技巧的应用(2)DNSIP注册信息的查询(3)基本的扫描技术:主机发现、端口扫描、OS及服务版本探测、具体服务的查点(以自己主机为目标)(4)漏洞扫描:会扫,会看报告,会查漏......
  • 实验三
    TASK1点击查看代码#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn)......
  • 实验4 信号量(Semaphores)
    要使用信号量,请先包含头文件<semaphore.h>sem_t:信号量的数据类型intsem_init(sem_t*sem,intpshared,unsignedintval);该函数第一个参数为信号量指针,第二个参数为信号量类型(一般设置为0),第三个为信号量初始值,第二个参数pshared为0时,该进程内所有线程可用,不为0时不同进......
  • 进度跟踪和成本跟踪实验
      接着实验    ......
  • 软件工程基础-实验一-原型设计-作家助手
    实验要求一:对比分析对比分析墨刀、Axure、Mockplus等原型设计工具的各自的适用领域及优缺点。一丶墨刀墨刀是一款在线的产品设计协作软件,可以解决产设研团队中存在的项目管理权限不明、版本管理混乱、协作低效等诸多问题。优点:功能强大:可满足产品经理、设计师、开发在产品设......
  • 实验3
    #include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);//函数声明voidprint_spaces(intn);//函数声明voidprint_blank_lines(intn);//函数声明int......
  • 软件开发与创新第二次实验———结对编程:计算出题系统
    一.结对信息2252418盛宇伟2252436董朝二.题目要求小学老师要每周给同学出300道四则运算练习题。这个程序有很多种实现方式:C/C++C#/VB.net/JavaExcelUnixShellEmacs/Powershell/VbscriptPerlPython两个运算符,100以内的数字,不需要写答案。需要检查答案是否正确,并......
  • 实验三
    实验任务一#include<stdio.h>#include<stdlib.h>#include<time.h>#include<windows.h>#defineN80voidprint_text(intline,intcol,chartext[]);voidprint_spaces(intn);voidprint_blank_lines(intn);intmain(){ intlin......
  • 互斥锁(实验)(不严谨)
    进程与进程的锁和线程与线程的锁是不一样的。abc三部曲pthread_mutex_tlock=PTHREAD_MUTEX_INITIALIZER;//创建一个锁pthread_mutex_lock(&lock);//上锁pthread_mutex_unlock(&lock);//开锁对于全局变量,共享数据就是临界区,需要使用锁。intticketAmount=2;//这里......
  • XMU《UNIX 系统程序设计》第二次实验报告
    一、实验内容描述实验目的掌握与文件和目录树有关的系统调用和库函数。实验要求编写程序myfind命令语法myfind<pathname>[-comp<filename>|-name<str>...]命令语义(1)myfind<pathname>的功能除了具有与程序4-7相同的功能外,还要输出在<pathname>目录子树之下,文......