运行Flask应用程序代码时,出现以下错误

时间:2018-10-16 09:37:20

标签: flask

我在此处附加了代码和错误-我的数据未加载,我看到一个错误,因为没有要分析的功能。我已经创建了一个类似于此屏幕截图的文件夹结构enter image description here

# Flask Packages
from flask import Flask,render_template,request,url_for
from flask_bootstrap import Bootstrap 
from flask_uploads import UploadSet,configure_uploads,IMAGES,DATA,ALL
from flask_sqlalchemy import SQLAlchemy 

from werkzeug import secure_filename
import os
import datetime
import time


# EDA Packages
import pandas as pd 
import numpy as np 

# ML Packages
from sklearn import model_selection
from sklearn.linear_model import LogisticRegression
from sklearn.tree import DecisionTreeClassifier
from sklearn.neighbors import KNeighborsClassifier
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.naive_bayes import GaussianNB
from sklearn.svm import SVC


# ML Packages For Vectorization of Text For Feature Extraction
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfVectorizer


app = Flask(__name__)
Bootstrap(app)
db = SQLAlchemy(app)

# Configuration for File Uploads
files = UploadSet('files',ALL)
app.config['UPLOADED_FILES_DEST'] = 'static/uploadsDB'
configure_uploads(app,files)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///static/uploadsDB/filestorage.db'

# Saving Data To Database Storage
class FileContents(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    name = db.Column(db.String(300))
    modeldata = db.Column(db.String(300))
    data = db.Column(db.LargeBinary)


@app.route('/')
def index():
    return render_template('index.html')

# Route for our Processing and Details Page
@app.route('/dataupload',methods=['GET','POST'])
def dataupload():
    if request.method == 'POST' and 'csv_data' in request.files:
        file = request.files['csv_data']
        filename = secure_filename(file.filename)
        # os.path.join is used so that paths work in every operating system
        # file.save(os.path.join("wherever","you","want",filename))
        file.save(os.path.join('static/uploadsDB',filename))
        fullfile = os.path.join('static/uploadsDB',filename)

        # For Time
        date = str(datetime.datetime.fromtimestamp(time.time()).strftime("%Y-%m-%d %H:%M:%S"))

        # Exploratory Data Analaysis function
        df = pd.read_csv(os.path.join('static/uploadsDB',filename))
        df_size = df.size
        df_shape = df.shape
        df_columns = list(df.columns)
        df_targetname = df[df.columns[-1]].name
        df_featurenames = df_columns[0:-1] # select all columns till last column
        df_Xfeatures = df.iloc[:,0:-1] 
        df_Ylabels = df[df.columns[-1]] # Select the last column as target
        # same as above df_Ylabels = df.iloc[:,-1]


        # Model Building
        X = df_Xfeatures
        Y = df_Ylabels
        seed = 7
        # prepare models
        models = []
        models.append(('LR', LogisticRegression()))
        models.append(('LDA', LinearDiscriminantAnalysis()))
        models.append(('KNN', KNeighborsClassifier()))
        models.append(('CART', DecisionTreeClassifier()))
        models.append(('NB', GaussianNB()))
        models.append(('SVM', SVC()))
        # evaluate each model in turn


        results = []
        names = []
        allmodels = []
        scoring = 'accuracy'
        for name, model in models:
            kfold = model_selection.KFold(n_splits=10, random_state=seed)
            cv_results = model_selection.cross_val_score(model, X, Y, cv=kfold, scoring=scoring)
            results.append(cv_results)
            names.append(name)
            msg = "%s: %f (%f)" % (name, cv_results.mean(), cv_results.std())
            allmodels.append(msg)
            model_results = results
            model_names = names 

        # Saving Results of Uploaded Files  to MySql DB
        newfile = FileContents(name=file.filename,data=file.read(),modeldata=msg)
        db.session.add(newfile)
        db.session.commit()     

    return render_template('details.html',filename=filename,date=date,
        df_size=df_size,
        df_shape=df_shape,
        df_columns =df_columns,
        df_targetname =df_targetname,
        model_results = allmodels,
        model_names = names,
        fullfile = fullfile,
        dfplot = df
        )




if __name__ == '__main__':
    app.run(debug=True)

所以上面是代码,错误如下

ERROR in app: Exception on /dataupload [POST]
Traceback (most recent call last):
  File "/anaconda2/lib/python2.7/site-packages/flask/app.py", line 2292, in wsgi_app
    response = self.full_dispatch_request()
  File "/anaconda2/lib/python2.7/site-packages/flask/app.py", line 1815, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/anaconda2/lib/python2.7/site-packages/flask/app.py", line 1718, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/anaconda2/lib/python2.7/site-packages/flask/app.py", line 1813, in full_dispatch_request
    rv = self.dispatch_request()
  File "/anaconda2/lib/python2.7/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/Users/girish/Desktop/StartAutoML/App.py", line 101, in dataupload
    cv_results = model_selection.cross_val_score(model, X, Y, cv=kfold, scoring=scoring)
  File "/anaconda2/lib/python2.7/site-packages/sklearn/model_selection/_validation.py", line 342, in cross_val_score
    pre_dispatch=pre_dispatch)
  File "/anaconda2/lib/python2.7/site-packages/sklearn/model_selection/_validation.py", line 206, in cross_validate
    for train, test in cv.split(X, y, groups))
  File "/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 779, in __call__
    while self.dispatch_one_batch(iterator):
  File "/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 625, in dispatch_one_batch
    self._dispatch(tasks)
  File "/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 588, in _dispatch
    job = self._backend.apply_async(batch, callback=cb)
  File "/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 111, in apply_async
    result = ImmediateResult(func)
  File "/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/_parallel_backends.py", line 332, in __init__
    self.results = batch()
  File "/anaconda2/lib/python2.7/site-packages/sklearn/externals/joblib/parallel.py", line 131, in __call__
    return [func(*args, **kwargs) for func, args, kwargs in self.items]
  File "/anaconda2/lib/python2.7/site-packages/sklearn/model_selection/_validation.py", line 458, in _fit_and_score
    estimator.fit(X_train, y_train, **fit_params)
  File "/anaconda2/lib/python2.7/site-packages/sklearn/linear_model/logistic.py", line 1220, in fit
    order="C")
  File "/anaconda2/lib/python2.7/site-packages/sklearn/utils/validation.py", line 573, in check_X_y
    ensure_min_features, warn_on_dtype, estimator)
  File "/anaconda2/lib/python2.7/site-packages/sklearn/utils/validation.py", line 470, in check_array
    context))
ValueError: Found array with 0 feature(s) (shape=(1091, 0)) while a minimum of 1 is required.
127.0.0.1 - - [16/Oct/2018 16:34:26] "POST /dataupload HTTP/1.1" 500 -

数据加载必须通过sqlalchemy进行,并且正在使用filestorage.db,我看不到任何数据,并且该应用程序未运行

1 个答案:

答案 0 :(得分:0)

您从

得到的错误
File "/Users/girish/Desktop/StartAutoML/App.py", line 101, in dataupload
cv_results = model_selection.cross_val_score(model, X, Y, cv=kfold, scoring=scoring)

错误是

ValueError: Found array with 0 feature(s) (shape=(1091, 0)) while a minimum of 1 is required.

检查this page  并尝试找出哪个参数是ValueError的原因,即函数model_selection.cross_val_score()的值不正确。

希望您能得到一些指导。