データセットで機械学習を行い、サンプルとテストデータを表示しようとしています。私の目標は、後で私のイメージをロードし、それをモデルでテストすることです。間違いなく私はエラーを解決するのに役立ちます。ランダムフォレストのモデルと入力機能が一致しません
%matplotlib inline
import matplotlib.pyplot as plt
from sklearn.datasets import fetch_mldata
import numpy as np
import random
from sklearn import ensemble
mnist = fetch_mldata('MNIST original', data_home='./')
#Define variables
n_samples = len(mnist.data)
x = mnist.data.reshape((n_samples, -1))# array of feature of 28*28 pixel
y = mnist.target # Class label from 0-9 as there are digits
#Create random indices
sample_index=random.sample(range(len(x)),len(x)/5) #Selecting randomly list of len(x)/5 from the size of x
valid_index=[i for i in range(len(x)) if i not in sample_index]# Selecting the rest of the digits
#Sample and validation images
sample_images=[x[i] for i in sample_index]# 28*28 size of array which was used to classify digits in different classes
valid_images=[x[i] for i in valid_index]
#Sample and validation targets
sample_target=[y[i] for i in sample_index] # digits 0-9
valid_target=[y[i] for i in valid_index]
#Using the Random Tree Classifier
classifier = ensemble.RandomForestClassifier(n_estimators=30)
#Fit model with sample data
classifier.fit(sample_images, sample_target)
#Attempt to predict validation data
score=classifier.score(valid_images, valid_target)
print 'Random Tree Classifier:\n'
print 'Score\t'+str(score)
from matplotlib.colors import ListedColormap
import matplotlib.pyplot as plt
def plot_decision_regions(X, y, classifier,
test_idx=None, resolution=0.02):
# plot the decision surface
x1_min, x1_max = X[:, 0].min() - 1, X[:, 0].max() + 1
x2_min, x2_max = X[:, 1].min() - 1, X[:, 1].max() + 1
xx1, xx2 = np.meshgrid(np.arange(x1_min, x1_max, resolution),
np.arange(x2_min, x2_max, resolution))
ここで私はすべてのエラーを持っていないのですが、この行の後にティル:あなたはclassifier.predict
関数を呼び出しているとき
ValueError: Number of features of the model must match the input. Model n_features is 784 and input n_features is 2
私はそれがnp配列の形状と関係があると思います。それらを再形成しようとします。こちらもご覧ください:[ここで同じ質問](https://stackoverflow.com/a/28855470/5025009) – sera