def pred(data, countries, start): # ***** Get predictions for all country *****# open('predictions.txt', 'w').close() for country in countries: record_predictions('\n' + country + ':') print('\n', country, ':') df = data[['Week', country]] df = df[np.isfinite(df[country])] # remove empty rows df = df.rename(index=str, columns={country: "Now"}) for i in range(3): df_ = df.copy() df_ = prepare(df_, pred=True) # train the model using all data except for the latest record X = df_.iloc[:,:-1] y = df_.iloc[:,-1] X,y = model.preprocess_data(X,y) X_train, y_train = X[:-1], y[:-1] # printX_train, '\n', y_train lr, label = model.lr(X_train,y_train) model.evaluate(lr,X,y,X_train,X_train,y_train,y_train,label,graph=False) fea = X[-1] y_pred = lr.predict([fea]) print('features:', fea, '\npredictions:', y_pred[0]) record_predictions(str(y_pred[0])) rec = pd.DataFrame([[start+i, y_pred[0]]], columns=['Week','Now']) # print'\nbefore appending\n', df df = df.append(rec)
def index(): """ POSTでリクエストを受け付けた場合、画像をtensorflowで分析して予測結果を返す """ if request.method == 'GET': return render_template('index.html') # NOTE: POST request file = request.files['file'] file_body = file.read() # tensorflowに読み込ませるように画像を変換 image_data = preprocess_data( 255 - np.array(Image.open(BytesIO(file_body)).resize((28, 28)).convert('L'))) # TODO: 毎回modelを訓練するのではなく、保存したものを使い回すようにする # NOTE: なぜかトップレベルでmodelを定義すると"Tensor(...) is not an element of this graph."というエラーが出る model = build_model() # 判定 prediction = model.predict(np.array([image_data]))[0] # 小数点2桁で四捨五入 prediction = [round(float(n), 4) for n in prediction] print(prediction) return render_template( 'index.html', image_source=base64.b64encode(file_body).decode( "utf-8"), # 画像の保存があるとなにかと面倒くさいのでbase64で prediction=sorted(dict(zip(class_names_ja, prediction)).items(), key=lambda x: x[1], reverse=True), )
def runTests(): model.load_data('data/train.csv', 'data/test.csv') methodPreprocess_list = [1, 2] methodFeatureExtraction = [1, 2] maxFeatures_list = [16000, 20000, 25000] ngrams_list = [(1, 3), (1, 4), (1, 5), (1, 2)] maxdf_list = [1.0, 0.95, 0.9, 0.85] mindf_list = [0.0001] binary_list = [True] for methodPreprocess in methodPreprocess_list: train_tweets, test_tweets = model.preprocess_data( method=methodPreprocess) for maxfeat in maxFeatures_list: for ng in ngrams_list: for maxd in maxdf_list: for mind in mindf_list: for bin in binary_list: for featureExt in methodFeatureExtraction: X, y, test, feature_names = model.feature_extraction( train_tweets, test_tweets, maxFeatures=maxfeat, ngrams=ng, maxdf=maxd, mindf=mind, isbinary=bin, method=featureExt) print('\n\n\n') print( "#########################################################################" ) print( "##############################################" ) print( 'Params preprocessing and features extraction:' ) print( '{0}, {1}, {2}, {3}, {4}, {5}, {6}'.format( methodPreprocess, maxfeat, ng, maxd, mind, bin, featureExt)) print( "##############################################" ) gsearch(X, y) print( "#########################################################################" ) print('\n\n\n')
def second(): text = [x for x in request.form.values()] processed_data = preprocess_data(text, tokenizer) results = new_model.predict(processed_data) print(results) if results > .75: positive = results negative = 1 - positive else: positive = results negative = 1 - positive return render_template('index.html', positive=positive, negative=negative)
@author: Ashima """ import config #import data import model import pandas as pd import numpy as np import seaborn as sns from sklearn.datasets import load_wine if __name__ == "__main__": #Load Data data = load_wine(return_X_y = False) #build model model = model.Model(data) model.preprocess_data() #model.visualize_data() # #Train using SVM CLassifier_One Vs One #model.linearsvc_ovo() #print("SVM trained model-One Vs One") ## #Train using SVM CLassifier_One Vs One #model.linearsvc_ovr() #print("SVM trained model-One Vs Rest") # Train using Gaussian Naive Bayes Classifier #model.naive_bayes() #print("Naive Bayes trained model") #Train using Decision trees Classifier model.decision_trees() #print("Decision Trees trained model")
from scipy.sparse import coo_matrix, hstack # #model.load_data('data/train.csv', 'data/test.csv') #train_tweets, test_tweets = model.preprocess_data(method=1) #dictFeatures_train, dictFeatures_test, y = model.new_extractFeatures(train_tweets, test_tweets) #results, s = model.new_gsearchRidge(dictFeatures_train, dictFeatures_test, y, nSplits = 3, testSize=0.4) # # # #clfs = model.new_train3(dictFeatures_train, y) #train_prediction, l = model.new_predict3(clfs, dictFeatures_train, y=y) #test_prediction, l = model.new_predict3(clfs, dictFeatures_test, y=None, clfsLabels=l) #model.saveResults('output/xxx1.csv', test_prediction) model.load_data('data/train.csv', 'data/test.csv') train_tweets, test_tweets = model.preprocess_data(method=1) emo_train, emo_test, sent_train, sent_test, k_train, k_test, w_train, w_test = model.new_extract_addicional_features( ) l_tfidf = [] l_cv = [] print() print() for mF in [ 12500, 15000, 20000, 25000, 30000, 35000, 40000, 45000, 50000, 55000, 60000 ]: print('Max Features = {0}'.format(mF)) for maxd in [0.55, 0.5, 0.45, 0.4, 0.35, 0.3, 0.25, 0.2]: print(
for i in range(min([len(images), 25])): plt.subplot(5, 5, i + 1) plt.xticks([]) plt.yticks([]) plt.grid('off') plt.imshow(images[i], cmap=plt.cm.binary) print(predictions[i]) predicted_label = np.argmax(predictions[i]) true_label = labels[i] if predicted_label == true_label: color = 'green' else: color = 'red' accuracy = round(float(predictions[i][predicted_label]), 4) label = "{} ({}) ({})".format(class_names[predicted_label], class_names[true_label], accuracy) plt.xlabel(label, color=color) plt.savefig('test_results/{}.png'.format(datetime.now().strftime('%s'))) plt.show() _, (test_images, test_labels) = keras.datasets.fashion_mnist.load_data() test_images = preprocess_data(test_images) model = build_model() run(model, test_images, test_labels)
from model import preprocess_data from model import keras_model from model import simple_model from keras.utils.np_utils import to_categorical from keras import optimizers size = 64 class_size = 3500 batch_size = 32 X, y = load_data(size, class_size) n_classes = len(np.unique(y)) print "Number of classes =", n_classes print "Img set shape", X.shape X = preprocess_data(X) y = to_categorical(y) print "y shape", y.shape rand_state = np.random.randint(0, 100) X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=rand_state) print "train shape X", X_train.shape print "train shape y", y_train.shape # np.save("X_train.data", X_train) # np.save("y_train.data", y_train) # np.save("X_test.data", X_test) # np.save("y_test.data", y_test)