利用逻辑回归分析鸢尾花分类
import sklearn.datasetsimport 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]
train_targets = iris.target]
# Make the testing data
test_features = iris.data]
test_targets = iris.target]
# 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 == test_targets:
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() 最好能贴出理论依据、公式推导过程以及算法过程的自然语言描述~
我就能matlab重新写一遍了,不懂Python伤不起啊{:10_257:}
页:
[1]