鱼C论坛

 找回密码
 立即注册
查看: 1664|回复: 1

[原创] 利用逻辑回归分析鸢尾花分类

[复制链接]
发表于 2018-10-18 13:52:58 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
import sklearn.datasets
import sklearn.linear_model
import numpy.random
import matplotlib.pyplot

# Load iris dataset
iris = sklearn.datasets.load_iris()

# Split the dataset with sampleRatio
sampleRatio = 0.7
n_samples = len(iris.target)
sampleBoundary = int(n_samples * sampleRatio)

# Shuffle the whole data
shuffleIdx = list(range(n_samples) )
numpy.random.shuffle(shuffleIdx)


# Make the training data
train_features = iris.data[shuffleIdx[:sampleBoundary]]
train_targets = iris.target[shuffleIdx [:sampleBoundary]]

# Make the testing data
test_features = iris.data[shuffleIdx[sampleBoundary:]]
test_targets = iris.target[shuffleIdx[sampleBoundary:]]

# Train
logisticRegression = sklearn.linear_model.LogisticRegression()
logisticRegression.fit(train_features, train_targets)
# Predict
predict_targets = logisticRegression.predict(test_features)

# Evaluation
n_test_samples = len(test_targets)
X = range(n_test_samples)
correctNum = 0
for i in X:
    if predict_targets[i] == test_targets[i]:
        correctNum += 1
accuracy = correctNum * 1.0 / n_test_samples
print ('Logistic Regression (Iris) Accuracy: %.2f' %(accuracy) )
# Draw
matplotlib.pyplot.figure(figsize=(32,24))
matplotlib.pyplot.title('Logistic Regression (Iris)')
matplotlib.pyplot.plot(X, predict_targets, 'ro-', label = 'Predict Labels')
matplotlib.pyplot.plot(X, test_targets, 'g+-', label='True Labels')
legend = matplotlib.pyplot.legend()

matplotlib.pyplot.ylabel('iris Class')
matplotlib.pyplot.savefig('Logistic Regression (Iris).png', format='png')
matplotlib.pyplot.show()
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-10-18 17:35:30 | 显示全部楼层
最好能贴出理论依据、公式推导过程以及算法过程的自然语言描述~
我就能matlab重新写一遍了,不懂Python伤不起啊
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-1-3 06:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表