追溯(最近一次呼叫最近):文件“ ann.py”,第35行,在<模块>预测= Neuro_net.predict(b)中

时间:2019-06-21 16:06:15

标签: python machine-learning scikit-learn neural-network

File "C:\Users\dell\AppData\Local\Programs\Python\Python37\lib\site-packages\sklearn\utils\extmath.py", line 173, in safe_sparse_dot
    return np.dot(a, b)
ValueError: shapes (4,9) and (10,10) not aligned: 9 (dim 1) != 10 (dim 0)

我看到类似这样的队列试图修复矩阵不起作用!

    import pandas as pd 
    import numpy as np 
    from sklearn.model_selection import KFold
    from sklearn.feature_extraction import DictVectorizer
    from sklearn.model_selection import train_test_split
    from sklearn.neural_network import MLPClassifier
    from sklearn import metrics

    data_frame = pd.read_csv("dataset.csv")
    feature_column_names = [0,1,2,3]
    predicted_class_name = [4]
    print("Enter KFold Cross Validation number:")
    number = int(input(""))
    k_fold = KFold(n_splits = number)
    sum = 0

    for train, test in k_fold.split(data_frame):
        x1 = np.array(data_frame.values[train])
        y1 =np.array(data_frame.values[test])

    train_test_size = len(y1)
    data = np.concatenate((x1,y1), axis = 0)

    dt = pd.DataFrame(data)
    x = dt[feature_column_names].values
    y = dt[predicted_class_name].values
    train_column,test_column,train_class,test_class = train_test_split(x,y,test_size = train_test_size,random_state = 42)
    neural_net = MLPClassifier(activation = 'relu', solver = 'lbfgs', alpha=1e-05, hidden_layer_sizes = (10,5))
    train_column = [dict(enumerate(train_columns)) for train_columns in train_column]
    test_column = [dict(enumerate(test_columns)) for test_columns in test_column]
    vect = DictVectorizer(sparse = False)
    a = vect.fit_transform(train_column)
    b = vect.fit_transform(test_column)
    neural_net.fit(a, train_class.ravel())
    prediction = neural_net.predict(b)
    accuracy = metrics.accuracy_score(test_class,prediction)
    sum += accuracy
    print(accuracy)
    print("Accuracy is: ", sum/number)

0 个答案:

没有答案