In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
Today, I will talk to you about how to simulate the perceptron algorithm in python. Many people may not know much about it. In order to let everyone know more, Xiaobian summarized the following contents for everyone. I hope everyone can gain something according to this article.
Perceptron algorithms are simulated by gradient descent. The data are derived from classical datasets in sklearn.datasets.
import numpy as npimport pandas as pd#import dataset load_iris.# Among them, the first four columns are calyx length, calyx width, petal length, petal width, etc. Four attributes are used to identify iris flowers, from sklearn.datasets import load_iris import matplotlib.pyplot as plot# load datairis = load_iris()#constructor DataFrame (data,index,columns), data is data, index is row index, columns is column index #Construct data structure df = pd.DataFrame (data=iris.data, columns=iris.feature_names)#Added a new column to df: The data in the column name label is target, i.e. the type #is the fifth column of the category of iris (including Setosa, Versicolour, Virginika)# That is, the category of iris can be identified by determining the size of calyx length, calyx width, petal length and petal width. df['label'] = iris.targetdf.columns = ['separate length',' separate width','petal length','petal width','label']#Print the quantity corresponding to the label column value. and column name, type # print(df.label.value_counts())#Create an array,#First argument:100 means to take rows 0-99, i.e. the first 100 rows; but 100: represents all data after 100.# The second parameter, 0,1 indicates the first column and the second column,-1 indicates the last column data = np.array(df.iloc[:100, [0, 1, -1])#Array cut, X cut the last column;y cut only the last column X, y = data[:, :-1], data[:, -1]#Data sorting, change y non-1 to-1y = np.array([1 if i == 1 else -1 for i in y])#Data is linearly separable, binary data #Here is a linear equation with one variable class Model: def __init__(self): # w is initially a (1,1) matrix with the same number of arguments self.w = np.ones(len(data[0]) - 1, dtype=np.float32) self.b = 0 #Learning rate set to 0.1 self.l_rate = 0.1 def sign(self, x, w, b): The # dot function is dot multiplication, which is different from * multiplication. # dot Matrix multiplication, one row multiplied by one column. # * Multiplication is the multiplication of the corresponding elements when the matrix operation is performed y = np.dot(x, w) + b return y #Random Gradient Descent def fit(self, X_train, y_train): is_wrong = False while not is_wrong: #Record the current classification error points, when the classification error points return to zero, that is, classification ends wrong_count = 0 # d Travels from 0-49 for d in range(len(X_train)): #where X is a two-column array X = X_train[d] y = y_train[d] if y * self.sign(X, self.w, self.b)
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.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.