机器学习基础
1.机器学习概述
2.特征处理:
归一化:
例题一
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams["font.family"]="FangSong"
plt.rcParams["axes.unicode_minus"]=False
pd.set_option("max_columns",None) #显示所有列
pd.set_option("max_rows",None) #显示所有行
df=pd.read_csv("house_data.txt",names=["area","price"])
print(df)
# 观察数据是否有线性关系
plt.scatter(x=df["area"],y=df["price"])
# x=x轴的列名 y=y轴的列名 data=数据来源的dataframe aspect是调节图的宽度
sns.lmplot(x="area",y="price",data=df,aspect=2)
# 归一化 df=(df-df.min())/(df.max()-df.min())
df=(df-df.min())/(df.max()-df.min())
print(df)
sns.lmplot(x="area",y="price",data=df,aspect=2)
plt.show()
例题二
df=pd.read_excel("朝阳医院2018年销售数据1.xlsx")
print(df)
df=df[["销售数量","实收金额"]]#切出来,有其他数据
print(df)
# 观察数据是否有线性关系
plt.scatter(x=df["销售数量"],y=df["实收金额"])
# x=x轴的列名 y=y轴的列名 data=数据来源的dataframe aspect是调节图的宽度
sns.lmplot(x="销售数量",y="实收金额",data=df,aspect=2)
# 归一化 df=(df-df.min())/(df.max()-df.min())
df=(df-df.min())/(df.max()-df.min())
sns.lmplot(x="销售数量",y="实收金额",data=df,aspect=2)
plt.show()
标准化
例题一
df=pd.read_csv("house_data.txt",names=["area","price"])
print(df)
# 观察数据是否有线性关系
plt.scatter(x=df["area"],y=df["price"])
# x=x轴的列名 y=y轴的列名 data=数据来源的dataframe aspect是调节图的宽度
sns.lmplot(x="area",y="price",data=df,aspect=2)
# 标准化 df=(df-df.mean())/df.std()
df=(df-df.mean())/df.std()
sns.lmplot(x="area",y="price",data=df,aspect=2)
plt.show()
例题二
df=pd.read_csv("datingTestSet.csv")
df=df[["milage","Liters","Consumtime"]]
print(df)
#标准化 df=(df-df.mean())/df.std()
df=(df-df.mean())/df.std()
sns.lmplot(x="milage",y="Liters",data=df,aspect=2)
plt.show()
离散化:
#离散值
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams["font.family"]="FangSong"
plt.rcParams["axes.unicode_minus"]=False
pd.set_option("max_columns",None) #显示所有列
pd.set_option("max_rows",None) #显示所有行
df=pd.read_csv("insurance.csv")
print(df)
data=df["age"]
# print(data)
bins=[0,15,20,35,50,65,100]
# print(bins)
#(right 是否包含右边的边界值) (include_lowest=True 是否包含左边的边界值) (labels=每组标签)
df["组"]=pd.cut(data,bins,right=False,include_lowest=True,labels=[1,2,3,4,5,6])
print(df["组"])
统一
#离散值
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
plt.rcParams["font.family"]="FangSong"
plt.rcParams["axes.unicode_minus"]=False
pd.set_option("max_columns",None) #显示所有列
pd.set_option("max_rows",None) #显示所有行
df=pd.read_csv("insurance.csv")
# print(df)
# data=df["age"]
# print(data)
bins=[0,15,20,35,50,65,100]
# print(bins)
#(right 是否包含右边的边界值) (include_lowest=True 是否包含左边的边界值) (labels=每组标签)
df["age1"]=pd.cut(df["age"],bins,right=False,include_lowest=True,labels=[1,2,3,4,5,6])
# print(df["age1"])
df["sex"]=df["sex"].map(lambda a:"1" if a=="male" else "0")
df["smoker"]=df["smoker"].map(lambda a:"1" if a=="yes" else "0")
# print(df["sex"],df["smoker"])
def cn(a):
if a=="southwest":
return 1
elif a=="southeast":
return 2
elif a=="northwest":
return 3
else:
return 4
df["region"]=df["region"].map(cn)
df1=df[["bmi","charges"]]
df[["bmi","charges"]]=(df1-df1.mean())/df1.std()
print(df)