决策树
特征值提取
字典特征提取
from sklearn.feature_extraction import DictVectorizer
def dict_demo():
# 获取数据
data = [{'city': '北京', 'temperature': 100},
{'city': '上海', 'temperature': 60},
{'city': '深圳', 'temperature': 30}]
# 字典特征提取
# 实例化
transfer = DictVectorizer(sparse=True)
# 转换
new_data = transfer.fit_transform(data)
print(new_data)
# 获取具体属性名
names = transfer.get_feature_names_out()
print("属性名字是:\n", names)
if __name__ == '__main__':
dict_demo()
文本特征提取
from sklearn.feature_extraction.text import CountVectorizer
def text_count_demo():
data = ["life is short,i like like python","life is too long,i dislike python"]
#实例化一个转换器
#CountVectorizer(stop_words=["写在这里的不统计"])
transfer = CountVectorizer()
data = transfer.fit_transform(data)
print("文本特征抽取结果:\n",data.toarray())
print("返回特征名称:\n",transfer.get_feature_names_out())
if __name__=='__main__':
text_count_demo()
中文分词
import jieba
def cut_word(text):
result = " ".join(list(jieba.cut(text)))
return result
TF-IDF
from sklearn.feature_extraction.text import TfidfVectorizer
def tfidf_demo():
data = ["life is short,i like like python","life is too long,i dislike python"]
#实例化一个转换器
transfer = TfidfVectorizer()
data = transfer.fit_transform(data)
print("文本特征抽取结果:\n",data.toarray())
print("返回特征名称:\n",transfer.get_feature_names_out())
if __name__=='__main__':
tfidf_demo()
决策树算法API
import pandas as pd
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.feature_extraction import DictVectorizer
from sklearn.tree import DecisionTreeClassifier,export_graphviz
#获取数据
data = pd.read_csv("http://biostat.mc.vanderbilt.edu/wiki/pub/Main/DataSets/titanic.txt")
#基本数据处理
#确定特征值,目标值
x = data[["pclass","age","sex"]]
y = data['survived']
#缺失值处理
x['age'].fillna(value=data["age"].mean(),inplace=True)
#分割数据
x_train,x_test,y_train,y_test = train_test_split(x,y,random_state=22,test_size=0.2)
#特征工程(字典特征提取)
x_train = x_train.to_dict(orient="records")
x_test = x_test.to_dict(orient="records")
transfer = DictVectorizer()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
#机器学习(决策树)
estimator = DecisionTreeClassifier(max_depth=5)
estimator.fit(x_train,y_train)
#评估模型
y_pre = estimator.predict(x_test)
print("预测值是:\n",y_pre)
ret = estimator.score(x_test,y_test)
print("准确率为:\n",ret)
#决策树可视化
export_graphviz(estimator,out_file="保存路径.dot",feature_names=['要输出的字段名'])
显示结构网站:http://webgraphviz.com
回归决策树
import numpy as np
import matplotlib.pyplot as plt
from sklearn.tree import DecisionTreeRegressor
from sklearn import linear_model
#生成数据
x = np.array(list(range(1,11))).reshape(-1,1)
y = np.array([5.56,5.70,5.91,6.40,6.80,7.05,8.90,8.70,9.00,9.05])
#训练模型
model1 = DecisionTreeRegressor(max_depth=1)
model2 = DecisionTreeRegressor(max_depth=3)
model3 = linear_model.LinearRegression()
model1.fit(x,y)
model2.fit(x,y)
model3.fit(x,y)
#模型预测
x_test = np.arange(0.0,10.0,0.01).reshape(-1,1)
y_1 = model1.predict(x_test)
y_2 = model2.predict(x_test)
y_3= model3.predict(x_test)
#结果可视化
plt.figure(figsize=(10,6),dpi=100)
plt.scatter(x,y,label="data")
plt.plot(x_test,y_1,label="max_depth=1")
plt.plot(x_test,y_2,label="max_depth=3")
plt.plot(x_test,y_3,label="liner regression")
plt.xlabel("data")
plt.ylabel("target")
plt.title("Decision Tree Regression")
plt.legend()
plt.show()