首页 > 其他分享 >day9

day9

时间:2024-09-16 20:14:44浏览次数:1  
标签:day9 print train test import reg sklearn

缺失值处理

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.font_manager import FontProperties
from sklearn import datasets

font = FontProperties(fname='/Library/Fonts/Heiti.ttf')

from io import StringIO

iris_data = """
4.7,,1.3,0.2
4.6,3.1,1.5,0.2
5.,3.6,1.4,0.2
5.4,3.9,1.7,0.4
4.6,3.4,,0.3
5.,3.4,1.5,0.2
4.4,2.9,1.4,0.2
4.9,3.1,1.5,0.1
5.4,3.7,1.5,
"""

iris = datasets.load_iris()
df = pd.read_csv(StringIO(iris_data),header=None)
df.columns=iris.feature_names
df=df.iloc[:,:4]
print(df)

from sklearn.impute import SimpleImputer
imputer=SimpleImputer(missing_values=np.nan,strategy='mean')
imputer=imputer.fit_transform(df.values)
df=pd.DataFrame(imputer,columns=iris.feature_names)
print(df)

标准化

最小最大标准化

from sklearn.preprocessing import MinMaxScaler
import numpy as np

test_data = np.array([1,2,3,4,5]).reshape(-1,1).astype(float)
min_max_scaler=MinMaxScaler()
min_max_scaler.fit(test_data)

波士顿房价训练回归

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from matplotlib.font_manager import FontProperties

字体

font = FontProperties(fname='/Library/Fonts/Heiti.ttf')

np小数点位数

np.set_printoptions(precision=3,suppress=True)

url = "https://raw.githubusercontent.com/scikit-learn/scikit-learn/main/sklearn/datasets/data/boston_house_prices.csv"
boston= pd.read_csv(url)
boston=boston.values
x=boston[1:,:-1]
y=boston[1:,-1]
print(x[:5])
print(y[:5])

切割和标准化

from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MinMaxScaler

x_train,x_test,y_train,y_test=train_test_split(x,y,test_size=0.3,random_state=1,shuffle=True)
print('训练集长度:{}'.format(len(y_train)),'测试集长度:{}'.format(len(y_test)))

scaler = MinMaxScaler()
scaler = scaler.fit(x_train)
x_train,x_test=scaler.transform(x_train),scaler.transform(x_test)
print('标准化后训练数据:\n{}'.format(x_train[:5]))
print('标准化后测试数据:\n{}'.format(x_test[:5]))

lasso回归

from sklearn.linear_model import Lasso

reg = Lasso()
reg = reg.fit(x_train,y_train)
y_pred =reg.predict(x_test)

print('lasso回归R2分数:{}'.format(reg.score(x_test,y_test)))

弹性网络回归

from sklearn.linear_model import ElasticNet

reg = ElasticNet()
reg = reg.fit(x_train,y_train)
y_pred =reg.predict(x_test)
print('弹性网络回归R2分数:{}'.format(reg.score(x_test,y_test)))

岭回归

from sklearn.linear_model import Ridge

reg = Ridge()
reg = reg.fit(x_train,y_train)
y_pred =reg.predict(x_test)
print('岭回归R2分数:{}'.format(reg.score(x_test,y_test)))

线性支持向量回归

from sklearn.svm import LinearSVR

reg = LinearSVR(C=100,max_iter=10000)
reg = reg.fit(x_train,y_train)
y_pred =reg.predict(x_test)
print('线性支持向量回归R2分数:{}'.format(reg.score(x_test,y_test)))

核支持向量回归

from sklearn.svm import SVR

reg = SVR(C=100,gamma='auto',max_iter=10000,kernel='rbf')
reg = reg.fit(x_train,y_train)
y_pred =reg.predict(x_test)
print('核支持向量回归R2分数:{}'.format(reg.score(x_test,y_test)))

决策树回归

from sklearn.tree import DecisionTreeRegressor

reg = DecisionTreeRegressor()
reg = reg.fit(x_train,y_train)
y_pred =reg.predict(x_test)
print('决策树回归R2分数:{}'.format(reg.score(x_test,y_test)))

随机森林回归

from sklearn.ensemble import RandomForestRegressor

reg = RandomForestRegressor()
reg = reg.fit(x_train,y_train)
y_pred =reg.predict(x_test)
print('随机森林回归R2分数:{}'.format(reg.score(x_test,y_test)))

标签:day9,print,train,test,import,reg,sklearn
From: https://www.cnblogs.com/dorakk/p/18416537

相关文章

  • 代码随想录day9-栈和队列1
    题目1232.用栈实现队列请你仅使用两个栈实现先入先出队列。队列应当支持一般队列支持的所有操作(push、pop、peek、empty):实现MyQueue类:voidpush(intx)将元素x推到队列的末尾intpop()从队列的开头移除并返回元素intpeek()返回队列开头的元素booleanempty()如......
  • 网安实习Day9@^@
    一、阿里云服务器安装CobaltStrike4.8(一)下载cs4.8压缩包解压在本地(二)使用xftp上传到阿里云服务器(三)xshell连接服务器进入到/CobalStrike4.8/Server/(四)对teamserver和TeamServerImage添加可执行权限命令:chmod+xteamserver  chmod+xTeamServerImage(五)运行团队服......
  • 【代码随想录Day9】字符串Part02
    151.翻转字符串里的单词解题思路如下:移除多余空格将整个字符串反转将每个单词反转举个例子,源字符串为:"theskyisblue"移除多余空格:"theskyisblue"字符串反转:"eulbsiykseht"单词反转:"blueisskythe"题目链接/文章讲解/视频讲解:代码随想录publicclassS......
  • leetcode刷题day9|字符串部分(151.翻转字符串里的单词、卡码网:55.右旋转字符串)
    前言:字符串章节的第二部分,复习第一部分的知识加提升。151.翻转字符串里的单词链接:https://leetcode.cn/problems/reverse-words-in-a-string/前言:不使用Java内部方法实现思路:先去除字符串中的空格;将整个字符串反转;将单词再一次反转。代码如下:classSolution{pub......
  • day9打卡
    用栈实现队列classMyQueue{public:MyQueue(){}voidpush(intx){stIn.push(x);}intpop(){inta;while(!stIn.empty()){a=stIn.top();stIn.pop();stOut.push(a);}a=stOut.top();stOut.pop();while(!stOut.empty()){intb=stOut.top();stOut.pop();......
  • 给自己复盘的tjxt笔记day9
    优惠券管理开发流程需求分析,接口统计,数据库设计,创建分支,创建新模块(依赖,配置,启动类),生成代码,引入枚举状态优惠券管理增删改查的业务代码,没有新的知识点新增优惠券@Override@TransactionalpublicvoidsaveCoupon(CouponFormDTOdto){//1.保存优惠券......
  • 代码训练营 Day9 | 151.翻转字符串里的单词 | 认识KMP算法
    151.翻转字符串里的单词这道题的难度在于如何高效率的处理空格对于python来说不能实现空间O(1)的复杂度,不过对于其他语言来说下面思路可以使用双指针方法同时指向数组的头部循环遍历整个字符串直到数组末尾快指针不能为空,因为快指针是要获取字母的,慢指针是用来获取新的字......
  • day9第四章 字符串part02| 151.翻转字符串里的单词 |卡码网:55.右旋转字符串|28. 实现
    151.翻转字符串里的单词classSolution{publicStringreverseWords(Strings){////删除首尾空格,分割字符串String[]str=s.trim().split("");StringBuildersb=newStringBuilder();////倒序遍历单词列表for(inti......
  • 【读书笔记-《30天自制操作系统》-8】Day9
    本篇的主题围绕着内存管理进行展开。首先编写了内存容量获取的程序,接下来详细讲解了内存管理的具体内容,以及两种实现内存管理的方式。1.内存容量获取前面已经实现了访问内存的扩展,能够使用的内存大大增加了。但是不同的应用程序在运行时,对内存的使用会有不同的要求,这就需......
  • Java后端面试题(mq相关)(day9)
    目录为什么用MQ?异步、削峰、解耦1.异步处理2.解耦3.削峰填谷Exchange类型什么是死信队列?如何保证消息的可靠性?RabbitMQ中如何解决消息堆积问题?RabbitMQ中如何保证消息有序性?如何防止消息重复消费?(如何保证消息幂等性)为什么用MQ?异步、削峰、解耦MQ(Message......