This article has participated in the activity of “New person creation Ceremony”, and started the road of digging gold creation together

Introduction to the

This article mainly describes how to use sklearn module to predict and learn, and finally in a more intuitive way in the chart

The data set

Study data

Forecast data

The data processing

Data separation

Because we open our learning data set, the last item is our real value. As we all know from Xiao Tang’s last article, the old rule is to split the data first, and put one piece of the front feature and one piece of the real value behind. At the same time, because there is no column name of the data, we choose to use ILOC [] to achieve the separation

def shuju(tr_path,ts_path,sep='\t'): Train =pd.read_csv(tr_path,sep=sep) test=pd.read_csv(ts_path,sep=sep) train_features=train.iloc[:,:-1].values train_labels=train.iloc[:,-1].values test_features = test.iloc[:, :-1].values test_labels = test.iloc[:, -1].values return train_features,test_features,train_labels,test_labelsCopy the code

Training data

We use the Sklearn function directly here, by selecting the model and then generating its recognition rules directly

Def train_tree(*data): X_train, x_test, y_test=data CLF =DecisionTreeClassifier() CLF. Fit (x_train,y_train) print(" {:.4f}". Format (clf.score(x_train, y_train)) print(" {:.4f}".format(clf.score(x_test, y_test))) #Copy the code

Data visualization

To make our observations more intuitive, we can also use matplotlib

def plot_imafe(test,test_labels,preds): plt.ion() plt.show() for i in range(50): 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 plt.imshow(img,cmap="binary") plt.title(title) plt.show() print('done')Copy the code

The results of

The complete code

import pandas as pd from sklearn.tree import DecisionTreeClassifier import matplotlib.pyplot as plt def shuju(tr_path,ts_path,sep='\t'): Train =pd.read_csv(tr_path,sep=sep) test=pd.read_csv(ts_path,sep=sep) train_features=train.iloc[:,:-1].values train_labels=train.iloc[:,-1].values test_features = test.iloc[:, :-1].values test_labels = test.iloc[:, -1]. Values return train_features,test_features,train_labels,test_labels # def train_tree(*data): X_train, x_test, y_test=data CLF =DecisionTreeClassifier() CLF. Fit (x_train,y_train) print(" {:.4f}". Format (clf.score(x_train, y_train)) print(" {:.4f}". Format (clf.score(x_test, y_test))) return CLF def plot_imafe(test,test_labels,preds): plt.ion() plt.show() for i in range(50): 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 plt.imshow(img,cmap="binary") plt.title(title) plt.show() print('done') train_features,test_features,train_labels,test_labels=shuju(r"C:\Users\twy\PycharmProjects\1\train_images.csv",r"C:\User s\twy\PycharmProjects\1\test_images.csv") clf=train_tree(train_features,test_features,train_labels,test_labels) preds=clf.predict(test_features) plot_imafe(test_features,test_labels,preds)Copy the code