代码拉取完成,页面将自动刷新
"""
This Is A Simple Logistic Regression Program To Classify Iris Species
Resources:
(1) https://towardsdatascience.com/building-a-logistic-regression-in-python-301d27367c24
(2) https://towardsdatascience.com/logistic-regression-a-simplified-approach-using-python-c4bc81a87c31
(3) https://scikit-learn.org/stable/modules/generated/sklearn.metrics.classification_report.html
"""
# Import the dependencies
import matplotlib.pyplot as plt
import seaborn as sns
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import classification_report
from sklearn.metrics import accuracy_score
from sklearn.model_selection import train_test_split
#Load the data set
data = sns.load_dataset("iris")
data.head()
#Prepare the training set
# X = feature values, all the columns except the last column
X = data.iloc[:, :-1]# or data.loc[:,'sepal_length':'petal_width']
# y = target values, last column of the data frame
y = data.iloc[:, -1]# or data.loc[:,'species']
# Plot the relation of each feature with each species
plt.xlabel('Features')
plt.ylabel('Species')
pltX = data.loc[:, 'sepal_length']
pltY = data.loc[:,'species']
plt.scatter(pltX, pltY, color='blue', label='sepal_length')
pltX = data.loc[:, 'sepal_width']
pltY = data.loc[:,'species']
plt.scatter(pltX, pltY, color='green', label='sepal_width')
pltX = data.loc[:, 'petal_length']
pltY = data.loc[:,'species']
plt.scatter(pltX, pltY, color='red', label='petal_length')
pltX = data.loc[:, 'petal_width']
pltY = data.loc[:,'species']
plt.scatter(pltX, pltY, color='black', label='petal_width')
plt.legend(loc=4, prop={'size':8})
plt.show()
#Split the data into 80% training and 20% testing
x_train, x_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
#Train the model
model = LogisticRegression()
model.fit(x_train, y_train) #Training the model
#Test the model
predictions = model.predict(x_test)
print(predictions)
#Check precision, recall, f1-score
print( classification_report(y_test, predictions) )
print( accuracy_score(y_test, predictions))
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。