Ai
1 Star 0 Fork 1

高鑫/Python

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
Logistic_Regression.py 2.02 KB
一键复制 编辑 原始数据 按行查看 历史
randerson112358 提交于 2019-06-13 02:27 +08:00 . Update Logistic_Regression.py
"""
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))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/gaoxin1999/Python.git
git@gitee.com:gaoxin1999/Python.git
gaoxin1999
Python
Python
master

搜索帮助