Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use python artificial Intelligence human learn drawing

2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to use python artificial intelligence human learn drawing for you. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

Nowadays, data scientists often give labeled machine learning model data so that it can find rules.

These rules can be used to predict labels for new data.

This is convenient, but some information may be lost in the process. It's also hard to know what's going on under the hood and why machine learning models produce specific predictions.

In addition to letting the machine learning model figure out everything, is there a way to use our domain knowledge to set the rules for data tags?

Yes, this can be done through human-learn.

What is human-learn?

Human-learn is a tool that allows you to set data marking rules using interactive drawings and custom models. In this article, we will explore how to use human-learn to create models with interactive drawings.

Install human-learnpip install human-learn

I'll use Iris data from sklearn to show how human-learn works.

From sklearn.datasets import load_irisfrom sklearn.model_selection import train_test_splitimport pandas as pd # Load dataX, y = load_iris (return_X_y=True, as_frame=True) X.columns = ['sepal_length',' sepal_width', 'petal_length',' petal_width'] # Train test splitX_train, X_test, y_train, y_test = train_test_split (X, y, random_state=1) # Concatenate features and labels of the training datatrain = pd.concat ([X_train Pd.DataFrame (y_train)], axis=1) train

Interactive drawing

Human-learn allows you to draw a dataset and then use drawings to convert it into a model. To demonstrate how useful this can be, imagine how to create a scatter plot of a dataset, as follows:

When you look at the figure above, you will see how to divide them into three different areas, as shown below:

However, it may be difficult to write graphics as rules and put them into functions, and human-learn 's interactive drawing will come in handy.

From hulearn.experimental.interactive import InteractiveChartscharts = InteractiveCharts (train, labels='target') charts.add_chart

-move figure 01

Drawing method: double-click to start drawing polygons. Then click to create the edges of the polygon. Double-click again to stop drawing the current polygon.

We do the same thing for other columns:

Charts.add_chart (xtended petalized widthwise, yawned petalized width`)

Create models and make predictions

Once you have finished drawing the dataset, you can create a model using the following methods:

From hulearn.classification import InteractiveClassifiermodel = InteractiveClassifier (json_desc=charts.data ()) preds = model.fit (X_train, y_train). Predict_proba (X_train) print (preds.shape) # Output: (150,3)

Cool! We enter the drawing into the InteractiveClassifier class and use a similar method to fit the model of sklearn, such as fit and predict_proba.

Let's take a look at the first five lines of pred:

Print ('Classes:', model.classes_) print (' Predictions:\ n, preds [: 5,:]) "OutputClasses: [1,2 0] Predictions: [[5.71326574e-01 4.28530630e-01 1.42795945e-04] [2.00079952e-01 7.99720168e-01 1.99880072e-04] [2.00079952e-01 7.99720168e-01 1.99880072e-04] [2.49812641e-04 2.49812641e-04 9.99500375e-01] [4.99916708e-01 4.99916708e-01 1.66583375e-04]] ""

It is important to note that predict_proba gives the probability that the sample has a specific label. For example, the first prediction of [5.71326574e-01 4.28530630e-01 1.42795945e-04] indicates that the probability that the sample has label 1 is 57.13%, the probability that the sample has label 2 is 42.85%, and the probability that the sample is label 2 is 0.014%. The label of the sample is 0.

Forecast new data # Get the first sample of X_testnew_sample = new_sample = X_test.iloc [: 1] # Predictpred = model.predict (new_sample) real = y_test [: 1] print ("The prediction is", pred [0]) print ("The real label is", real.iloc [0]) interpretation result

To understand how the model predicts based on this prediction, let's visualize the new sample.

Def plot_prediction (prediction: int, columns: list): "Plot new sample Parameters-prediction: int prediction of the new sample columns: list Features to create a scatter plot"index = prediction_to_ index [traditio n] col1, col2 = columns plt.figure (figsize= (12,3) plt.scatter (X_train [col1], X_train [col2], c=preds [: Index]) plt.plot (new_sample [col1], new_sample [col2], 'ro', cymbals, label='new_sample') plt.xlabel (col1) plt.ylabel (col2) plt.title (f "Label {model.classes_ [index]}") plt.colorbar () plt.legend ()

Use the above function to draw a new sample on the petal_length and petal_width drawings, and the points of the sample are marked with a probability coloring of 0.

Plot_prediction (0, columns= ['petal_length',' petal_width'])

The same is true for other columns, where we can see that the red dot is in the area with many yellow dots! This explains why the model predicts that the label of the new sample is 0. It's cool, isn't it?

Predict and evaluate test data

Now, let's use this model to predict all the samples in the test data and evaluate their performance. Start using the obfuscation matrix for evaluation:

From sklearn.metrics import confusion_matrix, f1_scorepredictions = model.predict (X_test) confusion_matrix (y_test, predictions, labels=]) array ([[13,0,0], [0,15,1], [0,0,9]])

We can also use F1 scores to evaluate the results:

F1_score (y_test, predictions, average='micro') on "how to use python artificial intelligence human learn drawing" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report