示例#1
0
def home():
    banner = "<img src='data:image/png;base64,{}' class='img-fluid'>".format(
        img_to_bytes("./static/banner-home.png"))
    st.markdown(banner, unsafe_allow_html=True)
    st.markdown('')
    st.markdown('')
    intro_markdown = read_markdown_file("static/info.md")
    st.markdown(intro_markdown, unsafe_allow_html=True)
    st.markdown('')
    st.info('Navigate using the sidebar on the left side of the page')

    st.header('Purpose')
    st.markdown('''Allows the user to provide self-collected failure data and
                fit well-known statistical distributions to learn the
                underlying failure process in presence of censoring. This
                allows the user to experiment with time-based maintenance to
                improve system reliability, and save costs by proactively doing
                regular preventive maintenance as opposed to reactively fixing
                failures as they come. It also comes with a simulation platform
                to enable the user to gauge the uncertainty for analysis in
                shorter time periods.''')

    st.markdown('---')
    st.header('Assumptions')
    st.markdown('''   1. Maintenance restores asset to *good-as-new* condition.
    If the assets are replaced every time it fails, it will satisfy this
    assumption, no worries!
    2. Operating hours and operating conditions of the assets are the same
    across the whole system.''')

    st.markdown('---')
    st.header('Workflow')
    st.markdown('''   1. Input failure data at the `Fitter` page to learn the
    distribution of the failure process. If you do not have the data, you may
    generate a toy data by supplying the parameters, or skip to the step 2.
    2. On the `Maintenance` page, you will be able to input the learned (if you
    do step 1, else you can still plug and play with the parameters) failure
    process with the parameters on the sidebar. Important to note is the
    maintenance interval length parameter, which is the crux of our analysis.
    **Do take note of the additional assumption.**
    3. Input the estimated/expected costs of a maintenance action on the asset
    and the estimated/expected costs of a failure. A table will show at the
    bottom of the page showing the costs given the maintenance interval set at
    the sidebar. It will also show the cost-optimized length of the maintenance
    interval.
    4. In case the estimate of the uncertainty is needed, users may proceed to
    the `Simulation` page to simulate maintenance within a user-specified time
    period. **Note that the assumption in step 2 is made optional by allowing
    user to choose the maintenance type.** The choice `Component-wise` reflects
    said assumption. The simulation data may be downloaded to allow users to
    conduct their own analysis that is not covered by the application. The
    bottom of the page will show the uncertainty as distributions.''')

    st.markdown('''There are **two** maintenance types implemented:''')
    st.markdown('''   1. *Component-wise*: Maintains asset when it reaches a
    *mileage*, or time since last maintenance equal to the user-given interval.
    Once maintenance is done, the timer is reset to 0 for that asset only.
    2. *Fleetwide*: Whenever the system reaches *mileage* equal to the
    user-given interval, the whole system is maintained, regardless of time
    to last failures of individual assets.''')

    st.markdown('---')
    st.header('Disclaimer')
    st.markdown('''The developer is not responsible of any outcome resulting
    from decision-making based on this application. Understand the assumptions
    that allows the modeling, and decide whether it is suitable for your
    purposes. You may [contact](https://github.com/nichostst) the developer to
    report bugs, or if you have general inquiry related to the application.''')
示例#2
0
    average = int(totalOfValues / len(sentenceWorthiness))

    # Storing sentences into our summary.
    textsummary = ''
    for sentence in sentences:
        if (sentence in sentenceWorthiness) and (sentenceWorthiness[sentence] >
                                                 (1.2 * average)):
            textsummary += " " + sentence
    return textsummary


# In[19]:

# print(len(string_summary))
st.info(summarise_text(string_summary))

# In[20]:

# display the close price
st.header(" Close Price\n")
# st.line_chart(data_dated['4. close'])

# In[21]:


def plot_closingPrice(data_dated):
    closing_price = data_dated['4. close']
    coefficients, residuals, _, _, _ = np.polyfit(range(
        len(closing_price.index)),
                                                  closing_price,
示例#3
0
def pricePrediction_LSTM(symbol, days, start_date, end_date):
    #     obtain stock data

    stock_df = stock_data(symbol, start_date, end_date)

    #     obtaining technical indicators
    stochastic_Oscillator(stock_df)
    calc_williams_r(stock_df)
    calc_macd(stock_df)
    calc_price_rate_of_change(stock_df)

    stock_df.reset_index(inplace=True)

    #     set the trading window we are trying to predict
    stock_df_targeted = trading_window(stock_df, days)

    stock_df_targeted_ = stock_df_targeted.copy()
    stock_df_targeted.drop([
        'Ticker', '4. close', '7. dividend amount', '3. low',
        '5. adjusted close', '6. volume', '8. split coefficient', 'low_14',
        'high_14', 'MACD_EMA'
    ],
                           axis=1,
                           inplace=True)

    stock_df_targeted.dropna(inplace=True)
    training_data_X = stock_df_targeted.iloc[:, 1:7].values
    training_data_y = stock_df_targeted.iloc[:, 7:].values

    stock_df_targeted_scaled = sc.fit_transform(
        stock_df_targeted.drop(columns=['date']))

    X = sc.fit_transform(training_data_X)
    y = y_sc.fit_transform(training_data_y)

    # Convert the data into array format
    X = np.asarray(X)
    y = np.asarray(y)

    # Split the data
    split = int(0.65 * len(X))
    X_train = X[:split]
    y_train = y[:split]
    X_test = X[split:]
    y_test = y[split:]

    # Reshape the 1D arrays to 3D arrays to feed in the model
    X_train = np.reshape(X_train, (X_train.shape[0], X_train.shape[1], 1))
    X_test = np.reshape(X_test, (X_test.shape[0], X_test.shape[1], 1))

    # Create the model
    inputs = keras.layers.Input(shape=(X_train.shape[1], X_train.shape[2]))
    x = keras.layers.LSTM(150, return_sequences=True)(inputs)
    x = keras.layers.Dropout(0.3)(x)
    x = keras.layers.LSTM(150, return_sequences=True)(x)
    x = keras.layers.Dropout(0.3)(x)
    x = keras.layers.LSTM(150)(x)
    outputs = keras.layers.Dense(1, activation='linear')(x)

    model = keras.Model(inputs=inputs, outputs=outputs)
    model.compile(optimizer='adam',
                  loss="mse",
                  metrics=['mean_squared_error', 'mae'])
    model.summary()

    # Train the model
    history = model.fit(X_train,
                        y_train,
                        epochs=20,
                        batch_size=32,
                        validation_split=0.2)

    predicted = model.predict(X)
    eval_predict = model.predict(X_test)

    last_element = X_test[len(X_test) - 1]
    original_prices = stock_df_targeted_['4. close'].values
    current_price = original_prices[len(original_prices) - 1]
    last_element = last_element.reshape(1, -1)

    # predicting future price
    predicted_price = model.predict(last_element)
    predicted_price = predicted_price.reshape(1, -1)

    # calculating profits
    profitInXDays = y_sc.inverse_transform(predicted_price)
    profitInXDays = round(profitInXDays[0][0], 2)
    share_amount = investment_amount / current_price
    profit = round((share_amount * profitInXDays) - investment_amount, 2)

    test_predicted = []

    for i in predicted:
        test_predicted.append(i[0])

    close = []
    for i in stock_df_targeted_scaled:
        close.append(i[0])

    df_predicted = stock_df_targeted[['date']]
    df_predicted['Close'] = close
    df_predicted['Prediction'] = predicted

    #     interactive_plot(df_predicted, "Original Vs. Prediction for " )
    scores = model.evaluate(X, y, verbose=0)

    # add evaluations to list
    RMSE.append(math.sqrt(mean_squared_error(y_test, eval_predict)))
    Rsquared.append(r2_score(y_test, eval_predict))
    Mae.append(mean_absolute_error(y_test, eval_predict))
    adj_Rsquared.append(1 - (1 - r2_score(y_test, eval_predict)) *
                        (len(y_test) - 1) / (len(y_test) - X.shape[1] - 1))

    # Plot the data
    interactive_plot(df_predicted, "Original Vs Prediction")
    # appending information to web page

    st.info("in {} day(s) the price of this stock will be £{}".format(
        days, round(profitInXDays, 2)))
    st.info("You would make £{} in {} day(s)".format(profit, days))
示例#4
0
                         index=['probability']))

    if input_type == 'enter values':
        st.success('done 👍')
    else:
        pass

    import warnings
    warnings.filterwarnings('ignore')

except:
    st.write('*press submit to compute results*')
    # st.write("if you see this, it finally worked :O")

st.markdown('---')

st.info('''
Source code available [here](https://github.com/batmanscode/breastcancer-predictor), please feel free to leave feedback and contribute 😊
''')

# hide hamburger menu
# https://discuss.streamlit.io/t/remove-made-with-streamlit-from-bottom-of-app/1370/2
# https://github.com/streamlit/streamlit/issues/395#issuecomment-579526417

hide_menu_style = """
        <style>
        #MainMenu {visibility: hidden;}
        </style>
        """
st.markdown(hide_menu_style, unsafe_allow_html=True)
示例#5
0
def main():
    st.title("Contribute")
    st.info(
        "Feel free to contribute to this open source project. Check my github account "
        "[here](https://github.com/MonitSharma)")
示例#6
0
def main():
    """Tweet Classifier App with Streamlit """

    # Creates a main title and subheader on your page -
    # these are static across all pages

    # Creating sidebar with selection box
    options = [
        "Prediction", "Purpose of the App", "Exploratory Data Analysis",
        "About Global Warming", "Machine Learning Models",
        "Natural Language Processing"
    ]
    selection = st.sidebar.selectbox("Choose Option", options)

    if selection == "Exploratory Data Analysis":
        df_senti1 = raw[raw['sentiment'] == 1]
        tweet_senti1 = " ".join(review for review in df_senti1.message)

        #create word cloud in eda

        st.image(img3,
                 width=600,
                 caption="Visualising the climate change threat")

        st.title("Insight From The Data")
        st.subheader(
            "A Representation Of The Most Common Words In Each Sentiment Class"
        )
        sent_groups = st.radio('Sentiment Views:', (
            'Positive, those who believe climate change is a threat',
            'Negative sentiment, opposing the belief that climate change is a threat',
            'Neutral, an impartial stance on climate change',
            'News Report, topical news reported on climate change'))
        if sent_groups == (
                'Positive, those who believe climate change is a threat'):
            df_senti1 = clean[clean['sentiment'] == 1]
            tweet_senti1 = " ".join(review
                                    for review in df_senti1.clean_stp_words)
            # Create and generate a word cloud image:
            wordcloud_1 = WordCloud(
                max_font_size=50, max_words=100,
                background_color="white").generate(tweet_senti1)
            plt.imshow(wordcloud_1, interpolation='bilinear')
            #plt.set_title('Tweets under Pro Class 1',fontsize=50)
            plt.axis('off')
            plt.show()
            st.pyplot()
            if st.checkbox('Interpretation of Diagram, Sentiment 1'):
             """Common words of interest in pro-sentiment include `To fight`,`to tackle`, `belive in` and `fight climate`. It appears that 
					tweets in this category are providing solutions to fight climate change. Many of the sentiments reflected are related to 
					on Trumps commentary. In the pro sentiment class we find that people do not agree with Trump.')"""
        if sent_groups == 'News Report, topical news reported on climate change':
            df_senti_2 = clean[clean['sentiment'] == 2]
            tweet_senti_2 = " ".join(review
                                     for review in df_senti_2.clean_stp_words)
            # Create and generate a word cloud image:
            wordcloud_2 = WordCloud(
                max_font_size=50, max_words=100,
                background_color="white").generate(tweet_senti_2)
            plt.imshow(wordcloud_2, interpolation='bilinear')
            #plt.set_title('Tweets under Pro Class 1',fontsize=50)
            plt.axis('off')
            plt.show()
            st.pyplot()
            if st.checkbox('Interpretation of Diagram, Sentiment 2') :
             """Common words news tweets are `Trump, global warming, via`,`Scientists`,`researchers`,`ÈPA` and `report`.
				 	This could reveal the sentiment that humans are the cause of climate change because they burn fossil fuels. 
				 	News reports can be highly influential on overall sentiment as many rely of the media to validate their beliefs. 
				 	It is evident that the word Trump is  most common. According to research in the news, the momentum for these 
				 	sentiments comes from the commentary that president Trump has made about climate change."""

        if sent_groups == "Neutral, an impartial stance on climate change":
            df_senti_0 = clean[clean['sentiment'] == 0]
            tweet_senti_0 = " ".join(review
                                     for review in df_senti_0.clean_stp_words)
            #Create and generate a word cloud image:
            wordcloud_0 = WordCloud(
                max_font_size=50, max_words=100,
                background_color="white").generate(tweet_senti_0)
            plt.imshow(wordcloud_0, interpolation='bilinear')
            #plt.set_title('Tweets under Pro Class 1',fontsize=50)
            plt.axis('off')
            plt.axis("off")
            plt.show()
            st.pyplot()
            if st.checkbox('Interpretation of Diagram, Sentiment 0'):
             """The sentiments in class 0 represents people that are neutral towards climate change. The reason could be that they are
					not aware of climate change, or do not have enough information, this can be seen by words such as `interviewer`,
					`Trump`, `think`. Common words in neutral tweets include `care about`,`think`,`maybe`. This could indicate
					uncerainty toward climate change validity or an apathetic inclination.Interestingly, the appearance of the word
				`	ignore` tells us that these tweeters find the matter confusing.')"""

        st.subheader(
            "**Observe the frequency of the 20 most common words in each class**"
        )
        Pro = clean[clean['sentiment'] == 1]
        Anti = clean[clean['sentiment'] == -1]
        Neutral = clean[clean['sentiment'] == 0]
        News = clean[clean['sentiment'] == 2]

        common = st.selectbox('Select Sentiment Type',
                              ('Positive', 'Negative', 'Neutral', 'News'))
        if common == 'Positive':
            Pro['temp_list'] = Pro['clean_stp_words'].apply(
                lambda x: str(x).split())
            top = Counter(
                [item for sublist in Pro['temp_list'] for item in sublist])
            temp_positive = pd.DataFrame(top.most_common(20))
            temp_positive.columns = ['Common_words', 'count']
            temp_positive = temp_positive.style.background_gradient(
                cmap='Greens_r')
            st.write(temp_positive, width=200)
        if common == 'Negative':
            Anti['temp_list'] = Anti['clean_stp_words'].apply(
                lambda x: str(x).split())
            top = Counter(
                [item for sublist in Anti['temp_list'] for item in sublist])
            temp_neg = pd.DataFrame(top.most_common(20))
            temp_neg.columns = ['Common_words', 'count']
            temp_neg = temp_neg.style.background_gradient(cmap='Greens_r')
            st.write(temp_neg, width=200)

        if common == 'News':
            News['temp_list'] = News['clean_stp_words'].apply(
                lambda x: str(x).split())
            top = Counter(
                [item for sublist in News['temp_list'] for item in sublist])
            temp_news = pd.DataFrame(top.most_common(20))
            temp_news.columns = ['Common_words', 'count']
            temp_news = temp_news.style.background_gradient(cmap='Greens_r')
            st.write(temp_news, width=200)

        if common == 'Neutral':
            Neutral['temp_list'] = Neutral['clean_stp_words'].apply(
                lambda x: str(x).split())
            top = Counter(
                [item for sublist in Neutral['temp_list'] for item in sublist])
            temp_net = pd.DataFrame(top.most_common(20))
            temp_net.columns = ['Common_words', 'count']
            temp_net = temp_net.style.background_gradient(cmap='Greens_r')
            st.write(temp_net, width=200)

        st.subheader("**A Closer Look At The Data Distribution**")
        temp = raw.groupby(
            'sentiment').count()['message'].reset_index().sort_values(
                by='message', ascending=False)
        temp['percentage'] = round(
            (temp['message'] / temp['message'].sum()) * 100, 0)
        labels1 = temp['sentiment']
        labels = ["Sentiment  %s" % i for i in temp['sentiment']]
        sizes = temp['percentage']
        fig1, ax1 = plt.subplots(figsize=(6, 6))
        fig1.subplots_adjust(0.3, 0, 1, 1)

        theme = plt.get_cmap('Greens_r')
        ax1.set_prop_cycle(
            "color", [theme(1. * i / len(sizes)) for i in range(len(sizes))])
        _, _ = ax1.pie(sizes, startangle=90, labels=labels1, radius=1800)

        ax1.axis('equal')
        total = sum(sizes)
        plt.legend(loc='upper left',
                   labels=[
                       '%s, %1.1f%%' % (l, (float(s) / total) * 100)
                       for l, s in zip(labels, sizes)
                   ],
                   prop={'size': 7},
                   bbox_to_anchor=(0.0, 1),
                   bbox_transform=fig1.transFigure)

        plt.show()  # Equal aspect ratio ensures that pie is drawn as a circle.
        st.pyplot()  #c, use_container_width=True)

        if st.checkbox('Interpretation of Pie Chart'):
         """More than half of the tweets analysed reflect a belief in climate change. 
					Although it is not an overwhelming majority figure, believers are in the majority.
					As science begins to offer clearer evidence it is likely that many neutral tweeters 
					could sway their beliefs. Less than ten percent of the sample population do not believe 
					in climate change. If the sample is a good representation of the population than the
					market for evironmentally friendly or environmentally conscious goods and services could
					be a desireable product to fairly large sector of the population')"""




    if selection == "Purpose of the App":

        st.header(
            "**The Impact Of Climate Change Sentiment And Maximising Profit**")
        img2 = Image.open("Images/gw.jpeg.jpg")
        st.image(img2,
                 width=400,
                 caption="Visualising the climate change threat")
        """This app will reveal the overall sentiment toward climate change by analysing recent
			tweets (post made on the social media application Twitter).By understanding how potential consumers 
			view climate change, companies can make informed decisions on product development and marketing. This app
			 will answer the question: Do people see climate change as a real threat?"""

        st.subheader(
            "A brief Look At The Raw Data (Database of tweets analysed)")

        if st.checkbox('Show raw data'):  # data is hidden if box is unchecked
            st.write(raw[["sentiment",
                          "message"]])  # will write the df to the page
            data = pd.DataFrame(raw, columns=['sentiment', 'message'])
            st.write(data.plot(kind='hist', color='green'))
            st.pyplot()
            data = {
                'Sentiment Type': ['-1', '0', '1', '2'],
                'Sentiment Meaning': [
                    'Negative sentiment, opposing the belief that climate change is a threat',
                    'Neutral, an impartial stance on climate change',
                    'Positive, supporting the belief that climate change poses a threat',
                    'News Report, topical news reported on climate change'
                ]
            }
            sentiment = pd.DataFrame(
                data, columns=['Sentiment Type', 'Sentiment Meaning'])
            sentiment = sentiment.set_index('Sentiment Type')
            st.write(sentiment, width=800)

            st.subheader("**Interpretation Of Sentiment Distribution**")
            """In the database ,most of the tweets indicate that alot of people believe climate change is a real threat and is man-man."""
            """Media coverage on climate change concerns substantiates the belief that climate change is a real threat.There are tweets 
				in the database that indicate that there are people who are nuetral on the subject of the subject
			    of Global warming ,however ,they are vastly outnumbered"""

    if selection == "Machine Learning Models":

        st.header("**Logistic Regression**")
        """The Logistic regression algorithm builds a regression model to predict
			the probability that a given data entry belongs to the category numbered as “1”.
			Logistic regression becomes a classification technique only when a decision
			threshold is brought into the picture. The setting of the threshold value is a very 
			is dependent on the classification problem itself.
			Logistic regression models the data using the sigmoid function.
			It squeezes the range of output values to exist only between 0 and 1.
			For binary classification ,the output value of a logistic regre.The threshold 
			value is usually set to 0.5 and determine if an observation will belong to class 0 or 1."""

        logistic_regression = Image.open("Images/logistic_regression.jpg")
        st.image(logistic_regression,
                 caption="sigmoid function for logistic regression ",
                 use_column_width=True)
        """For multiclass classification problems ,
			logistic regression models are combined into what is known as the one-vs-rest approach (or OvR).
			In the OvR case, a separate logistic regression model is trained for each label that the response
			variable takes on."""
        st.subheader("Pros and cons of Logistic Regression")
        """ - easy to implement and very efficient to train"""
        """ - Can overfit when data is unbalanced and Doesn't handle large number of categorical variables well."""
        logistic_reg_perf = Image.open('Images/logistic_reg_perfomance.jpg')
        st.image(logistic_reg_perf, use_column_width=True)

        st.header("**Random Forest tree**")
        """The building blocks of the random first model are Decision trees.Simple put ,the decision tree is a flowchart
			 of questions leading to a prediction.
			Random forest is a technique used in modeling predictions and behavior analysis and is built on decision trees.
			It contains many decision trees that represent a distinct instance of the classification of data input into the random forest. 
			The random forest technique takes consideration of the instances individually, taking the one with the majority of votes as 
			the selected prediction."""
        """Each decision tree in the forest considers a random subset of features when forming questions and only has access
				to a random set of the training data points.This increases diversity in the forest leading to more robust overall predictions and the name
				 ‘random forest.’ When it comes time to make a prediction, the random forest takes an average of all the individual decision tree estimates
				"""
        """Each tree in the classifications takes input from samples in the initial dataset.This is followed by a random selection of Features 
			(or indipendent variables) , which are used in growing the tree at each node. Every tree in the forest is pruned until 
			 the end of the exercise when the prediction is reached decisively. 
			Thus ,the random forest enables any classifiers with weak correlations to create a strong classifier"""
        decisiontree = Image.open("Images/random_forest.png")
        st.image(decisiontree,
                 caption="Random Forest tree process to predict a label ",
                 width=None)

        st.subheader("Pros and cons of the random forrest")
        """ - Can handle missing values well. Missing values are substituted by the variable appearing the most in a particular node."""
        """ - Provides the some of the highest accuracy of available classification methods"""
        """ - Some drawbacks is that the random forst classifyer method is that it requires a lot of computational reasources
			 time consuming ,and less intuitive compared to other algorithms"""
        random_for_perf = Image.open("Images/random_forest_perf.jpg")
        st.image(random_for_perf, use_column_width=True)

        st.header("Support Vector Machine")
        """A Support Vector Machine (SVM) is a supervised machine learning algorithm that can be employed for both 
			classification and regression purposes.SVMs are based on the idea of finding a hyperplane that best divides 
			a dataset into two classes"""
        """Support vectors are the data points nearest to the hyperplane, the points of a data set that, if removed, would alter 
			the position of the dividing hyperplane. Because of this, they can be considered the critical elements of a data set."""
        """Simply put ,a hyperplane is a line that linearly separates and classifies a set of data."""
        """The further from the hyperplane a data point lies, the higher the probability that it has been 
			classified correctly. Ideally ,we require a data point to be as far away as possible , while still being on 
			the correct side of the hyperplane .Whenever new testing data is added ,the side of the hyperplane is 
			lands on decides the class it is assigned to.
			"""
        svm = Image.open("Images/support_vector1.jpg")
        st.image(svm,
                 caption="Hyperplane deviding data points",
                 use_column_width=True)
        st.subheader("Pros and Cons of Support Vector Machines")
        """- it is very accurate and works well on smaller cleaner datasets"""
        """ - It can be more efficient because it uses a subset of training points"""
        """ - Less effective on noisier datasets with overlapping classes , 
			training time with SVMs can be high ,thus not suitable for larger datasets"""
        svm_perf = Image.open("Images/support_vector_perfomance.jpg")
        st.image(svm_perf, use_column_width=True)
        st.header("For more information on algorithm implimentation")
        "**Logistic regression**"
        " https://scikit-learn.org/stable/modules/generated/sklearn.linear_model.LogisticRegression.html"
        "**Random Forest **"

        " https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.RandomForestClassifier.html"
        "**Support Vector Machines** "
        " https://scikit-learn.org/stable/modules/svm.html"

#Natural language Processing page ,it slowed down my computer because of the english library
    #if selection == 'Natural Language Processing':
    #st.info("Natural Language Processing")
    #tweet_text = st.text_area("Enter Text","Type Here")
    #nlp_task = ["Tokenization","NER","Lemmatization","POS Tags"]
    #task_choice = st.selectbox("Choose NLP Task",nlp_task)
    #if st.button("Analyze"):
    #st.info("Original Text {}".format(tweet_text))

    #docx = nlp(tweet_text)
    #if task_choice == 'Tokenization':
    #result = [ token.text for token in docx ]

    #elif task_choice == 'Lemmatization':
    #result = ["'Token':{},'Lemma':{}".format(token.text,token.lemma_) for token in docx]
    #elif task_choice == 'NER':
    #result = [(entity.text,entity.label_)for entity in docx.ents]
    #elif task_choice == 'POS Tags':
    #st.json(result)

    #if st.button("Tabulize"):
    #docx = nlp(tweet_text)
    #c_tokens = [ token.text for token in docx ]
    #c_lemma = [token.lemma_ for token in docx]
    #c_pos = [word.tag_ for word in docx]

    #new_df = pd.DataFrame(zip(c_tokens,c_lemma,c_pos),columns=['Tokens','Lemma','POS'])
    #st.dataframe(new_df)

    #if st.checkbox("Wordcloud"):

    #wordcloud =  WordCloud(max_font_size=30, max_words=100, background_color="orange").generate(tweet_text)
    #plt.imshow(wordcloud,interpolation='bilinear')
    #plt.axis("off")
    #st.pyplot()

    # Building out the "Information" page

    if selection == "About Global Warming":
        st.info("General Information")
        """ # Global Warming in 5 minutes """
        st.header("Natural Climate change")
        """ - Throughout its long history, Earth has warmed and cooled time and again.
		Climate has changed when the planet received more or less sunlight due to subtle shifts
		 in its orbit, as the atmosphere or surface changed, or when the Sun’s energy varied .This was
		 all without any help from humanity"""
        """ - Earth’s temperature begins with the Sun. Roughly 30% of incoming sunlight is 
		reflected back into space by bright surfaces like clouds and ice. The rest is absorbed by
		 the land and ocean, and the atmosphere. 
		 The absorbed solar energy heats our planet and makes it habitable."""
        """ - As the rocks, the air, and the seas get warmer, they radiate “heat” energy which 
		 travels into the atmosphere ,where it is absorbed by water vapor and long-lived greenhouse 
		 gases """
        """ - Greenhouse gases are those gases in the atmosphere that have an influence on the earth's energy balance. 
		The best known greenhouse gases, carbon dioxide (CO₂), methane and
		 nitrous oxide, can be found naturally in low concentrations in the atmosphere.
		"""
        """ - After absorbing the heat energy ,these greenhouse gases will radiate energy in all directions. Some of this energy is 
		 radiated back towards the Earth ,further warming atmosphere and surfaces - This is the natural greenhouse"""
        """ - Some natural forces that contribute to climate change include volcanic eruptions, which
		pump out clouds of dust and ash, which block out some sunlight. Volcanic debris also includes sulfur dioxide,
		 combines with water vapor and dust in the atmosphere to form sulfate aerosols, which reflect sunlight away
		  from the Earth’s leading to a cooling effect."""
        """ - Earth orbital changes - Shifts and wobbles in the Earth’s orbit can trigger changes in climate such as 
		the beginning and end of ice ages"""
        """ - Also natural is Solar variations. Although the Sun’s energy output appears constant
		from an everyday point of view, small changes over an extended period of time can lead to climate changes.
		 Some scientists suspect that a portion of the warming in the first half of the 20th century was due to an
		  increase in the output of solar energy"""
        """- Scientists constantly measure these natural effects, but none can account for the observed trend since 1970.
		 Scientists can only account for recent global warming by including the effects of human greenhouse gas emissions."""

        image = Image.open("Images/global temperature.jpg")
        st.image(image,
                 caption="Global temperature graph(Image: Global Warming Art)",
                 use_column_width=True)
        st.subheader("Some notable events in The Global Temperature timeline")
        """ Between 1850-1890 , the Mean global temperature was roughly 13.7°C.This is the time period of the First Industrial Revolution. 
			Coal, railroads, and land clearing speed up greenhouse gas emission, while 
			better agriculture and sanitation speed up population growth."""
        """Between 1870-1910 was the Second Industrial Revolution. Fertilizers and other chemicals, 
		electricity, and public health further accelerate population growth."""
        """ Around 1940 ,massive output of aerosols from industries and power plants 
		contributed to the global cooling trend from 1940-1970."""
        """ two major volcanic eruptions, El Chichon in 1982 and Pinatubo in 1991, pumped sulfur dioxide gas high into the atmosphere.
		 The gas was converted into tiny particles that lingered for more than a year, reflecting sunlight and shading Earth’s surface
		 causing cooling for two to three years."""
        """The 10 warmest years on record have all occurred since 1998, and 9 of the 10 have occurred since 2005."""
        """Models predict that Earth will warm between 2 and 6 degrees Celsius in the next century. When global warming has
		 happened at various times in the past two million years, it has taken the planet about 5,000 years to warm 5 degrees.
		 e predicted rate of warming for the next century is at least 20 times faster"""
        """- Factuations climate is natural but scientists say temperatures are now rising faster 
		than at many other times."""
        """ - Humans have been artificially raising the concentration of greenhouse gases in the atmosphere ,causing the enhanced
		Greenhouse effect """
        """ - Global warming is the unusually rapid increase in Earth’s average surface temperature over the past 
		century primarily due to the greenhouse gases released as people burn fossil fuels. """
        """ - According to IPCC in its 5th 2013 fifth assessment report ,there is 
		between a 95% and 100% probability that more than half of modern day warming was due to humans."""
        """ - Recent US fourth national climate assessment found that between 93% to 123% of observed 
			1951-2010 warming was due to human activities"""
        """ - Human activities like burning fossil fuels leading to higher carbon dioxide concentrations,
			farming and forestry — including land use change via agriculture and livestock
			cement manufacture
			aerosols — chlorofluorocarbons (CFCs) have been linked to Global warming"""
        """ - Greenhouse gases from these activities collect in the atmosphere and absorb sunlight and 
		solar radiation that have bounced off the earth’s surface. Normally, this radiation would escape 
		 into space—but these pollutants, which can last for
		 years to centuries in the atmosphere, trap the heat and cause the planet
		  to get hotter. That's what's known as the greenhouse effect """
        """- There are Natural external causes such as increases or decreases in volcanic activity or solar radiation.
		 For example, every 11 years or so, the Sun’s magnetic field flips ,this can cause small 
		 fluctuations in global temperature, up to about 0.2 degrees. On longer time scales – tens to hundreds
		  of millions of years – geological processes can drive changes in the climate, due to shifting
		   continents and mountain building"""
        """ # Evidence of Global Warming 📈 """
        """ - Across the globe, average sea level increased by 3.6mm per year between 2005 and 2015 """
        """ - According to the World Meteorological Organization (WMO),The world is about one degree Celsius warmer 
		than before widespread industrialisation"""
        """ - Data from NASA's Gravity Recovery and Climate Experiment show 
		The Greenland and Antarctic ice sheets have decreased in mass"""

        st.subheader("Suggested Readings :earth_africa: ")
        st.markdown("https://www.bbc.com/news/science-environment-24021772")
        st.markdown("https://climate.nasa.gov/evidence/")
        st.markdown(
            "https://earthobservatory.nasa.gov/features/GlobalWarming/page2.php"
        )
        st.markdown(
            "https://www.carbonbrief.org/analysis-why-scientists-think-100-of-global-warming-is-due-to-humans"
        )
        # read in word file

        st.subheader("Refereces")
        """1.https://www.newscientist.com/article/dn11639-climate-myths-the-cooling-after-1940-shows-co2-does-not-cause-warming/"""

        st.subheader("Climate change tweet classification")

    # Building out the predication page
    if selection == "Prediction":
        st.info("Prediction with ML Models")
        # Creating a text box for user input
        tweet_text = st.text_area("Enter Text to Classify ", "Type Here")
        #tweet_text=[tweet_text]
        #tweet_text = st.text_area("Enter Text","Type Here")
        all_ml_models = [
            "Logistic Regression", "Support Vector Machine",
            "Random Forest Tree"
        ]
        model_choice = st.selectbox("Choose ML Model", all_ml_models)
        prediction_labels = {
            "Neutral : This text neither supports nor refutes the belief of man-made Climate change":
            0,
            "Pro : This text shows belief in man-man climate change":
            1,
            "news : This text is links to factual news about climate change":
            2,
            "Anti : This text shows lack of belief in man-made climate change":
            -1
        }
        if st.button("Classify"):
            if model_choice == "Logistic Regression":
                predictor = joblib.load(
                    open(os.path.join("resources/saved_model_for_App.pkl"),
                         "rb"))
                prediction = predictor.predict([tweet_text])
            elif model_choice == "Support Vector Machine":
                predictor = joblib.load(
                    open(os.path.join("resources/saved_model_for_App.pkl"),
                         "rb"))
                prediction = predictor.predict([tweet_text])
                # st.write(prediction)
            elif model_choice == "Random Forest Tree":
                predictor = joblib.load(
                    open(os.path.join("resources/saved_model_for_App.pkl"),
                         "rb"))
                prediction = predictor.predict([tweet_text])

            #Results displayed on screen after User has clicked the classify button
            final_result = get_keys(prediction, prediction_labels)
            st.success("{}".format(final_result))
示例#7
0
st.title("Diagnosa COVID-19 Chest X-Ray")
image_file = st.sidebar.file_uploader("Upload Image",
                                      type=['png', 'jpeg', 'jpg'])
#image_file = image_file_upload.read()

#uploaded_file = st.sidebar.file_uploader(type="xls", encoding =None, key = 'a')
#bytes_data = uploaded_file.read()

if image_file is not None:
    file_details = {
        "Filename": image_file.name,
        "FileType": image_file.type,
        "FileSize": image_file.size
    }
    st.write(file_details)

    img_uji = load_image(image_file)

    st.write("HASIL PREDIKSI = ", prediksi(img_uji))

    st.image(img_uji)  # ,width=250,height=250)

else:
    st.header("About")
    st.info(
        "Prediksi diagnosa ini dilakukan dengan metoda Deep Learning Convolutional Neural Network, model yang dipergunakan adalah hasil trainning dari 10.300 image dengan nilai akurasi > 90'%"
    )
    st.info("*****@*****.**")
    st.text("Terimakasih, semoga bermanfaat di era pandemi ini")
示例#8
0
    st.write("\n")
    st.write("\n")

    col1, col2, col3 = st.beta_columns(3)

    with col1:
        st.header("Cas confirmes")
        st.warning(df['Cas confirmes'][df.index[1]])

    with col2:
        st.header("Tests effectues")
        st.success(df['Tests effectues'][df.index[1]])
    with col3:
        st.header("Deces")
        st.info(df['Deces'][df.index[1]])

    st.write("\n")
    st.write("\n")
    st.title("Visualisation des données quotidiennes")

    all_columns_names = df.columns.tolist()
    selected_column_names = st.multiselect(
        "Choisir la(les) colonne(s) à dessiner dans le graphe",
        all_columns_names,
        default=["Cas confirmes", "Deces"])

    fig = px.line(df,
                  y=selected_column_names,
                  title=f'Èvolution de {selected_column_names}')
示例#9
0
def main():
    # application title
    st.title("历史天气图分析")
    st.header('——检索1979年1月1日以来的地面观测日值和天气图')

    # application information
    st.sidebar.image('http://image.nmc.cn/assets/img/index/nmc_logo_3.png',
                     width=300)
    st.sidebar.markdown('**天气预报技术研发室**')

    # Input data date
    data_date = st.sidebar.date_input("输入日期:",
                                      datetime.date(2016, 7, 19),
                                      min_value=datetime.date(1979, 1, 1),
                                      max_value=datetime.date.today() -
                                      datetime.timedelta(days=2))

    # Input data time
    data_time = st.sidebar.selectbox('选择时刻(UTC):', ('00', '06', '12', '18'),
                                     index=2)

    # construct datetime string
    datetime_str = data_date.strftime('%Y%m%d') + data_time
    date_obj = datetime.datetime.strptime(datetime_str, '%Y%m%d%H')

    # subset data
    map_regions = get_map_regions()
    select_items = list(map_regions.keys())
    select_items.append('自定义')
    select_item = st.sidebar.selectbox('选择空间范围:', select_items)
    if select_item == '自定义':
        map_region_str = st.sidebar.text_input(
            '输入空间范围[West, East, South, North]:', '70, 140, 10, 65')
        map_region = list(map(float, map_region_str.split(", ")))
    else:
        map_region = map_regions[select_item]

    # draw the figures
    if st.sidebar.button('绘制天气图'):
        # load data
        st.info(
            '载入CFSR数据, http://thredds.atmos.albany.edu:8080/thredds/dodsC/ (taking 30s)'
        )
        data = load_variables(date_obj, map_region=map_region)

        # draw the synoptic compsite
        fig = draw_maps.draw_composite_map(date_obj, data['t850'],
                                           data['u200'], data['v200'],
                                           data['u500'], data['v500'],
                                           data['mslp'], data['gh500'],
                                           data['u850'], data['v850'],
                                           data['pwat'])
        state.img1 = fig

        # draw weather analysis maps
        manager = Manager()
        return_dict = manager.dict()
        p = Process(target=draw_maps.draw_weather_analysis,
                    args=(date_obj, data, map_region, return_dict))
        p.start()
        p.join()
        if return_dict[0] is not None:
            state.img2 = return_dict[0]
        else:
            st.info('制作各层天气图失效!')
            state.img2 = None
        return_dict.clear()
        manager.shutdown()
        p.close()
        del data

        # load observation data
        obs_data = cmadaas_obs_by_time(
            date_obj.strftime('%Y%m%d000000'),
            data_code="SURF_CHN_MUL_DAY",
            sta_levels="011,012,013",
            elements=
            "Station_Id_C,Lat,Lon,Alti,TEM_Max,TEM_Min,VIS_Min,PRE_Time_0808,WIN_S_Max"
        )
        if obs_data is not None:
            state.img3 = draw_maps.draw_observation(obs_data, date_obj,
                                                    map_region)
        else:
            state.img3 = None

    if state.img1 is None or state.img2 is None:
        st.info('请点击左侧**绘制天气图**按钮生成或更新图像.')
    else:
        # display observation
        if state.img3 is not None:
            st.markdown('''
                ------
                ### 选择站点实况观测''')
            options = [key for key in state.img3.keys()]
            option = st.selectbox('', options, 0)
            st.plotly_chart(state.img3[option], use_container_width=False)

        # display synoptic composite
        st.markdown('''
                ------
                ### 环流形势综合图''')
        st.pyplot(state.img1)

        # display weather analysis maps
        st.markdown('''
                ------
                ### 点击天气图弹出放大''')
        images = np.asarray([*state.img2.values()], dtype=object)
        labels = np.asarray([*state.img2.keys()])
        html = ipyplot.display_image_gallery(images, labels, img_width=200)
        st.markdown(html, unsafe_allow_html=True)
        # options = [key for key in state.img2.keys()]
        #options = st.multiselect(
        #    '', options, ['500hPa_height', 'precipitable_water', 'mean_sea_level_pressure'])
        #for option in options:
        #    st.image(state.img2[option], use_column_width=True)

    st.sidebar.markdown('''
    ------
    [CFSR](https://climatedataguide.ucar.edu/climate-data/climate-forecast-system-reanalysis-cfsr)(CLIMATE FORECAST SYSTEM REANALYSIS)
    再分析资料是NCEP提供的第三代再分析产品. 本程序从Albany大学Thredds服务器检索指定日期时刻及范围的CFSR数据(从1979年1月1日以来每日4次, 全球范围), 
    并绘制高空环流形势天气图. 
    ''')
示例#10
0
import streamlit as st
import pandas as pd
import pickle

st.title('Forex TimeSeries Model Maker')

uploaded_file = st.file_uploader('upload historical data',
                                 type=['pickle', 'csv'],
                                 accept_multiple_files=False,
                                 help='upload time series data')
print(uploaded_file)

if uploaded_file is not None:

    st.header('Data View')
    #st.write('interact with the data with your mouse')

    if uploaded_file.name.endswith('.csv'):
        data = pd.read_csv(uploaded_file.name)
        st.write(data)

    elif uploaded_file.name.endswith('.pickle'):
        data = pickle.load(open(uploaded_file.name, 'rb'))
        st.write(data)
    st.info('interact with the data with your mouse')

    st.header('Data Plot')
    #st.write('interact with the plot with your mouse')
    st.line_chart(data[['open', 'high', 'low', 'close']])
    st.info('interact with the plot with your mouse')
                #df1.columns = df1.columns.str.replace(r'vte','')
                st.dataframe(df1)

            #df.columns = df.columns.str.replace(r"[()]", "_
            #df2 = df1.val.replace({'vte':'test'}, regex=True)
        else:
            st.error(
                '⚠️ - URL needs to be in a valid format, starting with *https://* or *http://*'
            )

    else:

        st.warning('ℹ️ - Paste a URL in the field above')

except ValueError:
    st.info("ℹ️ - No table(s) to scrape on this page! 😊")

try:

    csv = df1.to_csv(index=False)
    b64 = base64.b64encode(csv.encode()).decode()
    st.markdown('### ** ⬇️ Download the selected table to CSV **')
    href = f'<a href="data:file/csv;base64,{b64}" download="filtered_table.csv">** Download file! **</a>'
    st.markdown(href, unsafe_allow_html=True)

except NameError:
    print('wait')

st.markdown("---")

# st.markdown('*Made * by [@cristianmonroy ](https://twitter.com/cristianmonroy)* [![this is an image link](https://i.imgur.com/Ltgzb7Y.png)](https://www.buymeacoffee.com/cristianmonroy)')
def main():

    # DO NOT REMOVE the 'Recommender System' option below, however,
    # you are welcome to add more options to enrich your app.
    page_options = ["Recommender System","EDA", "Solution Overview", "About Us"]

    # -------------------------------------------------------------------
    # ----------- !! THIS CODE MUST NOT BE ALTERED !! -------------------
    # -------------------------------------------------------------------
    page_selection = st.sidebar.selectbox("Choose Option", page_options)
    if page_selection == "Recommender System":
        # Header contents
        st.write('# Movie Recommender Engine')
        st.write('### EXPLORE Data Science Academy Unsupervised Predict')
        st.image('resources/imgs/Image_header.png',use_column_width=True)
        # Recommender System algorithm selection
        sys = st.radio("Select an algorithm",
                       ('Content Based Filtering',
                        'Collaborative Based Filtering'))

        # User-based preferences
        st.write('### Enter Your Three Favorite Movies')
        movie_1 = st.selectbox('Fisrt Option',title_list[14930:15200])
        movie_2 = st.selectbox('Second Option',title_list[25055:25255])
        movie_3 = st.selectbox('Third Option',title_list[21100:21200])
        fav_movies = [movie_1,movie_2,movie_3]

        # Perform top-10 movie recommendation generation
        if sys == 'Content Based Filtering':
            if st.button("Recommend"):
                try:
                    with st.spinner('Crunching the numbers...'):
                        top_recommendations = content_model(movie_list=fav_movies,
                                                            top_n=10)
                    st.title("We think you'll like:")
                    for i,j in enumerate(top_recommendations):
                        st.subheader(str(i+1)+'. '+j)
                except:
                    st.error("Oops! Looks like this algorithm does't work.\
                              We'll need to fix it!")


        if sys == 'Collaborative Based Filtering':
            if st.button("Recommend"):
                try:
                    with st.spinner('Crunching the numbers...'):
                        top_recommendations = collab_model(movie_list=fav_movies,
                                                           top_n=10)
                    st.title("We think you'll like:")
                    for i,j in enumerate(top_recommendations):
                        st.subheader(str(i+1)+'. '+j)
                except:
                    st.error("Oops! Looks like this algorithm does't work.\
                              We'll need to fix it!")


    # -------------------------------------------------------------------

    # ------------- SAFE FOR ALTERING/EXTENSION -------------------
    if page_selection == "EDA":
        st.header("Movie Recommender System Datasets explorer")
        st.markdown(""" This dataset shown below was extracted from set of data available from a kaggle competition obtained from the link https://www.kaggle.com/c/edsa-recommender-system-predict/data""")
        st.sidebar.header("Configuration")
        st.sidebar.subheader("Available Visuals obtained from the sections below:")
        all_cols = df_merge1.columns.values
        numeric_cols = df_merge1.columns.values
        obj_cols = df_merge1.columns.values

        if st.sidebar.checkbox("Data preview", True):

            st.subheader("Data preview")
            st.markdown(f"Shape of dataset : {df_merge3.shape[0]} rows, {df_merge3.shape[1]} columns")
            if st.checkbox("Data types"):
                st.dataframe(df_merge3.dtypes)
            if st.checkbox("Pandas Summary"):
                st.write(df_merge1.describe())
            cols_to_style = st.multiselect(
                "Choose numeric columns to apply BG gradient", numeric_cols
                )
            st.dataframe(df_merge3.head(50).style.background_gradient(subset=cols_to_style, cmap="BuGn"))
            st.markdown("---")
        #st.markdown("<h1 style='text-align: center; color: black;'>Exploratory Data Analysis</h1>", unsafe_allow_html=True)
        st.markdown("""The Data Visualisation done on this page was extracted from the a kaggle notebook which can be found from the link:
            https://www.kaggle.com/kundaninetshiongolwe/team-5-recommenders""")



        if st.sidebar.checkbox("Visuals on Ratings"):
            if st.checkbox("Ratings count by year"):
                fig, ax = plt.subplots(1, 1, figsize = (12, 6))
                ax1 = df_merge1.groupby('rating_year')['rating'].count().plot(kind='bar', title='Ratings by year')
                st.write(fig)


            if st.checkbox("How ratings are distributed: histogram"):
                f = px.histogram(df_merge1["rating"], x="rating", nbins=10, title="The Distribution of the Movie Ratings")
                f.update_xaxes(title="Ratings")
                f.update_yaxes(title="Number of Movies per rating")
                st.plotly_chart(f)
            if st.checkbox("How ratings are distributed: scatter plot"):
                fig, ax = plt.subplots(figsize=(14, 7))
                ax.spines['top'].set_visible(False)
                ax.spines['right'].set_visible(False)
                ax.set_title('Rating vs. Number of Ratings', fontsize=24, pad=20)
                ax.set_xlabel('Rating', fontsize=16, labelpad=20)
                ax.set_ylabel('Number of Ratings', fontsize=16, labelpad=20)

                plt.scatter(ratings_df['Mean_Rating'], ratings_df['Num_Ratings'], alpha=0.5, color='green')
                st.pyplot(fig)

        if st.sidebar.checkbox("Visuals on Genres"):
            st.info("The number of movie per genre")
            fig=make_bar_chart(genre_df, 'Genre', title='Most Popular Movie Genres', xlab='Genre', ylab='Counts')
            st.pyplot(fig)

        if st.sidebar.checkbox("Movie published"):
            st.info("Movies published by year")
            st.pyplot(make_histogram(df_merge3, 'moviePubYear', title='Movies Published per Year', xlab='Year', ylab='Counts'))


    st.sidebar.header("About")
    st.sidebar.text("Team name : Team_5_EDSAJuly2020")
    st.sidebar.text(
        "Code : https://github.com/Thami-ex/unsupervised-predict-streamlit-template"
    )




    if page_selection == "Solution Overview":
        st.title("Solution Overview")
        st.write("Describe your winning approach on this page")

        st.markdown("""A `Recommender System (RS)` is no doubt one of the most obvious ways in which companies are enhancing the user experience
        in the platform that they provide their customers services. Companies Like Facebook, Netflix, Amazon, and Youtube are using RS to do so.
        More likely, these companies and other companies that are implementing the RS are doing so in introducing machine learning into these
        companies. It is therefore important for aspiring Data Scientists to develop skills in such areas. At `Explore Data Science Academy (EDSA)`,
        this team was given a task to build a RS. There are 3 available approaches to building a recommender system. As part of this project the
        team explored two of these which were the `Content Based Filtering (CBF)` and `Collaborative Filtering (CF)` algorithm.

            """)

        st.subheader("**Collaborative Filtering (CF)**")
        st.markdown("""This recommender engine was easy to implement in this work as it provides us with the recommendation of the 10 movies easily
         as compared to the other approach. On the other hand, the CF is one of the most popular implemented recommender engines and it is based on
         the assumption that the people were in agreement in the past and there is a high chance that they are in agreement in the future. An example
          indicating what is meant by the statement about agreement is considering that a friend and the other friend have probably liked an identical
          range of books in the past. Because the friend has now read new books that the other has not read there is a high chance that the other friend
          will enjoy and probably like those same books. This logic describes what is known as `user-based` collaborative filtering which was implemented
          in this application. """)

        st.subheader("**Building the Recommender Sytem**")
        st.markdown("""The recommender system application was built mainly for consumers to have an experience of watching movies that they are
        likely to enjoy based on the three movies they have selected. Figure below shows a recommender engine from Netflix showing new release
         movies. Ideally, more recommender systems look like the one from the figure below, however, the approach to building this one was somehow
         different. """)


        image=Image.open("./images/rs.jpg")
        st.image(image, use_column_width=True)

        st.markdown("""In building this web application, a couple of steps were followed starting with forking the repository from Github given by EDSA,
         using the dataset provided and obtained from the repository to build the recommender system. Following that was working with the script of the
         collaborative filtering algorithm by editing the code to obtain a movie prediction when using the main script run with streamlit. The `about
         us` has a link to the Github repo for if the intention is to attain better grasp on how the code works using python code.
            """)

        st.markdown("""This recommender engine is considered to be user friendly and one can easily use it to get movies that others have enjoyed and are
         related to the movies that they enjoy. This is done by only selecting three movies and press `Recommend` and 10 movies will be suggested. """ )

    if page_selection == "About Us":
        # st.markdown("<h1 style='text-align: center; color: black;'>About Us</h1>", unsafe_allow_html=True)
        image = Image.open("./images/about_us3.jpeg")
        st.image(image, use_column_width=True)
def results(language, risk_probability, method, no_infection_likelihood_today,
            no_infection_likelihood_tmrw, no_infection_likelihood_next_week):
    if language == 'English':
        st.write('   ')
        st.title('Resutls')
        st.write('   ')

        business_type = st.selectbox(
            'Business type',
            ['', 'Individual business', 'Group of businesses'])
        st.write('   ')

        if business_type == 'Group of businesses':
            num_group = st.number_input('Number of members in the group')
            st.subheader(
                'Likelihood that at least one company is infected in the group'
            )
            st.write(
                'Likelihood that at least one company in the group is infected today',
                f'{round((1 - ((1 - round(1 - (no_infection_likelihood_today) / 100, 2)) ** num_group)) * 100, 1)}%'
            )
            st.write(
                'Likelihood that at least one company in the group will be infected tomorrow',
                f'{round((1 - ((1 - round(1 - (no_infection_likelihood_tmrw) / 100, 2)) ** num_group)) * 100, 1)}%'
            )
            st.write(
                'Likelihood that at least one company in the group will be infected in a week',
                f'{round((1 - ((1 - round(1 - (no_infection_likelihood_next_week) / 100, 2)) ** num_group)) * 100, 1)}%'
            )

        st.subheader('Likelihood that an average company is infected')
        st.write(
            'Probability that at least one of your employees is infected today: ',
            f'{round(1 - (no_infection_likelihood_today) / 100, 2) * 100}%')
        st.write(
            'Probability that at least one of your employees is infected tomorrow: ',
            f'{round(1 - (no_infection_likelihood_tmrw) / 100, 2) * 100}%')
        st.write(
            'Probability that at least one of your employees is infected in a week',
            f'{round(round(1 - (no_infection_likelihood_next_week) / 100, 2) * 100, 1)}%'
        )

        if (round(1 - (no_infection_likelihood_today) / 100, 2) *
                100) > risk_probability:
            st.error('Close immediately')
        elif (round(1 - (no_infection_likelihood_tmrw) / 100, 2) *
              100) > risk_probability:
            st.warning('Close before tomorrow')
        elif (round(1 - (no_infection_likelihood_next_week) / 100, 2) *
              100) > risk_probability:
            st.info('Close within a week')
        else:
            st.success('No need to close this coming week')

    elif language == 'Somali':
        st.write('   ')
        st.title('Natiijada')
        st.write('   ')

        business_type = st.selectbox(
            'Nooca meherada',
            ['', 'Meherad gooni isku-taaga', 'Meherado isku biirey'])
        st.write('   ')

        if business_type == 'Meherado isku biirey':
            num_group = st.number_input('Tirada meheradaha isku biirey')
            st.subheader(
                'Ixtimaalka in mid ka mid ah meheradaha ugu yaraan uu xanuunku gaadhey'
            )
            st.write(
                'Ixtimaalka in mid ka mid ah meheradaha ugu yaraan uu xanuunku gaadhey maanta',
                f'{round((1 - ((1 - round(1 - (no_infection_likelihood_today) / 100, 2)) ** num_group)) * 100, 1)}%'
            )
            st.write(
                'Ixtimaalka in mid ka mid ah meheradaha ugu yaraan uu xanuunku gaadhi doono barri',
                f'{round((1 - ((1 - round(1 - (no_infection_likelihood_tmrw) / 100, 2)) ** num_group)) * 100, 1)}%'
            )
            st.write(
                'Ixtimaalka in mid ka mid ah meheradaha ugu yaraan uu xanuunku gaadhi 7aad gudihii',
                f'{round((1 - ((1 - round(1 - (no_infection_likelihood_next_week) / 100, 2)) ** num_group)) * 100, 1)}%'
            )

        st.subheader(
            'Ixtimaalka in mid ka mid ah shaqalaha uu xanuunku ku dhacay')
        st.write(
            'Ixtimaalka in mid ka mid ah shaqalaha uu xanuunku ku dhacay maanta: ',
            f'{round(1 - (no_infection_likelihood_today) / 100, 2) * 100}%')
        st.write(
            'Ixtimaalka in mid ka mid ah shaqalaha uu xanuunku ku dhici doono barri: ',
            f'{round(1 - (no_infection_likelihood_tmrw) / 100, 2) * 100}%')
        st.write(
            'Ixtimaalka in mid ka mid ah shaqalaha uu xanuunku ku dhici doono 7aad gudihii',
            f'{round(round(1 - (no_infection_likelihood_next_week) / 100, 2) * 100, 1)}%'
        )

        if (round(1 - (no_infection_likelihood_today) / 100, 2) *
                100) > risk_probability:
            st.error('Sida ugu dhakhsiyaha badan uxidh meherada [imikadan]')
        elif (round(1 - (no_infection_likelihood_tmrw) / 100, 2) *
              100) > risk_probability:
            st.warning('Barri kohor xidh meherada')
        elif (round(1 - (no_infection_likelihood_next_week) / 100, 2) *
              100) > risk_probability:
            st.info('7aad gudihii ku xidh meherada')
        else:
            st.success('Uma baahnid inaad xidhid meherada 7aadkan soo socda')
def main():
    st.sidebar.subheader('Language/Luuqada')
    language = st.sidebar.radio('', ('English', 'Somali'))

    if language == 'English':
        st.title('COVID-19: A model for when you should close your office')
        for i in range(3):
            st.write(" ")

        st.markdown(
            "> ### A model to help you and your company make a decision on whether you should open your office or send everybody home amid the COVID-19 crisis."
        )

        for i in range(2):
            st.write(" ")

        st.write(
            "It was first developed by [Tomas Pueyo](https://medium.com/@tomaspueyo) as a complimentary tool to his viral article on Medium [Coronavirus: Why You Must Act Now](https://medium.com/@tomaspueyo/coronavirus-act-today-or-people-will-die-f4d3d9cd99ca), an impressive article that I strongly recommend you read. This is a python adaptation of the model, developed by [Abdurahman Shiine](https://github.com/abdurahman-shiine)."
        )
        st.write(
            "It's based on how many COVID-19 cases are probably in your area, and the likelihood that at least one of your employees catches it.\nIt has lots of assumptions, but all the data necessary is [here](https://docs.google.com/spreadsheets/u/1/d/17YyCmjb2Z2QwMiRRwAb7W0vQoEAiL9Co0ARsl03dSlw/copy?usp=sharing), so you can play with the assumptions to adapt them to your situation.\nNote that only the necessary portions of the data were migrated to the [model_data.csv](https://github.com/abdurahman-shiine/CoronaVirus-A-model-for-When-you-should-close-your-office) file used for this model, so you might need to get some data from the original source if you need to recompute some specific parameters."
        )
        st.write(
            "The way to use this dashbourd is by filling in all the empty boxes, and select one of the options in each of the dropdown menus to apply the model to your case. The model is available in both English and Somali languages."
        )

        st.markdown(
            "<div align='center'><br>"
            "<img src='https://img.shields.io/badge/MADE%20WITH-PYTHON%20-red?style=for-the-badge'"
            "alt='API stability' height='25'/>"
            "<img src='https://img.shields.io/badge/SERVED%20WITH-Heroku-blue?style=for-the-badge'"
            "alt='API stability' height='25'/>"
            "<img src='https://img.shields.io/badge/DASHBOARDING%20WITH-Streamlit-green?style=for-the-badge'"
            "alt='API stability' height='25'/></div>",
            unsafe_allow_html=True)

        for i in range(3):
            st.write(" ")

        st.header('🌀 The model')
        num_employees = st.number_input('The number of employees you have')
        risk_probability = st.number_input(
            "Tolerable risk % (I'm ok with this probability that one or more of my employees has the coronavirus.)"
        )
        method = st.selectbox('Calculation method', [
            '',
            'Deaths method (more reliable, use if there have been deaths in your area)',
            'Cases method (not as reliable, making wild guesses on actual current cases in your area)'
        ])

        # Deaths method
        if method == 'Deaths method (more reliable, use if there have been deaths in your area)':
            total_deaths = st.number_input(
                'Total deaths as of today (Number of deaths you are seeing in the geographic area of influence of your office. Eg Sonomish / King / Grant counties)'
            )

            st.markdown('### The following results are based on:')
            st.markdown('#### - Fatality rate = 0.87%')
            st.markdown(
                '#### - Number of days from infection to death = 17.3 days')
            st.markdown('#### - Doubling time = 6.2 days')
            st.markdown('#             ')

            fatality_rate = 0.87 / 100
            # How many cases were there some time ago that caused these deaths?
            num_cases_caused_death = total_deaths / fatality_rate
            # How many days did it take for the cases to become deaths? Based on 4 papers
            days_from_infection = 17.3
            # How many days does it take for cases to double?
            doubling_time = 6.2
            # How many times do cases double between the time of the cases that caused deaths and the deaths
            num_times_cases_doubled = days_from_infection / doubling_time
            # Estimation of true cases today in your area
            num_true_cases = num_cases_caused_death * 2**num_times_cases_doubled

            # The likelihood of true and new cases in your area tomorrow and in a week
            likely_true_cases_tmrw = num_true_cases * 2**(1 / doubling_time)
            likely_new_cases_tmrw = likely_true_cases_tmrw - num_true_cases
            likely_true_cases_week = num_true_cases * 2**(7 / doubling_time)
            likely_new_cases_week = likely_true_cases_week - num_true_cases

            st.write('Number of cases that caused the death: ',
                     round(num_cases_caused_death))
            st.write('True cases today: ', round(num_true_cases))
            st.write('Likely true cases tomorrow: ',
                     round(likely_true_cases_tmrw))
            st.write('Likely true cases in a week: ',
                     round(likely_true_cases_week))
            st.write('Likely new cases tomorrow: ',
                     round(likely_new_cases_tmrw))
            st.write('Likely new cases in a week: ',
                     round(likely_true_cases_week))

            # ******* Estimation of the likelihood of a person getting infected *******
            st.subheader(
                'Estimation of the likelihood of a person getting infected')
            num_people = st.number_input(
                'Number of people in the area of deaths (must be >= 10,000)',
                value=10000,
                min_value=10000)

            try:
                if num_true_cases >= num_people:
                    no_infection_likelihood_today = 0.0
                    no_infection_likelihood_tmrw = 0.0
                    no_infection_likelihood_next_week = 0.0

                else:
                    # Current infection rate
                    infection_rate_today = num_true_cases / num_people
                    # Expected infection rate the next day
                    infection_rate_tmrw = likely_true_cases_tmrw / num_people
                    # Expected infection rate a week later
                    infection_rate_next_week = likely_true_cases_week / num_people

                    no_infection_likelihood_today = (
                        (1 - infection_rate_today)**num_employees) * 100
                    no_infection_likelihood_tmrw = (
                        (1 - infection_rate_tmrw)**num_employees) * 100
                    no_infection_likelihood_next_week = (
                        (1 - infection_rate_next_week)**num_employees) * 100

                st.write(
                    'likelihood that none of your employees already have the coronavirus',
                    f'{round(no_infection_likelihood_today, 1)}%')
                st.write(
                    'likelihood that none of your employees will have the coronavirus by tomorrow',
                    f'{round(no_infection_likelihood_tmrw, 1)}%')
                st.write(
                    'likelihood that none of your employees will have the coronavirus by next week',
                    f'{round(no_infection_likelihood_next_week, 1)}%')

                results(language, risk_probability, method,
                        no_infection_likelihood_today,
                        no_infection_likelihood_tmrw,
                        no_infection_likelihood_next_week)

            except ZeroDivisionError:
                st.error('Number of people can\'t be zero')

        # Cases method
        if method == 'Cases method (not as reliable, making wild guesses on actual current cases in your area)':
            total_cases = st.number_input(
                'Total cases in your area as of today')
            testing_type = st.selectbox(
                'Is testing exhaustive, or only if connected to a known case?',
                ['', 'Only if connected to a case', 'Exhaustive'])
            community_spread = st.selectbox('Is community spread happening?',
                                            ['', 'Yes', 'No'])

            if testing_type == 'Only if connected to a case' and community_spread == 'Yes':
                df = pd.read_csv('model_data.csv', header=None,
                                 thousands=',').transpose()
                new_header = df.iloc[0]
                df = df[1:]
                df.columns = new_header
                df.reset_index(inplace=True)

                foreign_spread_share_data = df['Share China'].apply(
                    lambda x: float(x.strip('%')) / 100)

                if total_cases <= 180:
                    foreign_spread_share = foreign_spread_share_data[
                        int(total_cases) - 1]
                else:
                    foreign_spread_share = 1 / 100

                num_true_cases = total_cases / foreign_spread_share
                cases_data = df[
                    'Typical contagion as a blend of other countries (US UK DE FR ES IT IR SK JP CH) (you can insert your own #s here)'][:
                                                                                                                                         32]
                cases_data = cases_data.str.replace(',', '').astype(float)
                starting_day_index = min(
                    filter(lambda x: x > num_true_cases, cases_data.tolist()))

                likely_true_cases_tmrw = cases_data[cases_data[
                    cases_data == starting_day_index].index[0]]
                likely_true_cases_week = cases_data[
                    cases_data[cases_data == starting_day_index].index[0] + 6]
                likely_new_cases_tmrw = likely_true_cases_tmrw - num_true_cases
                likely_new_cases_week = likely_true_cases_week - num_true_cases

                st.write('True cases today: ', round(num_true_cases))
                st.write('Likely true cases tomorrow: ',
                         round(likely_true_cases_tmrw))
                st.write('Likely true cases in a week: ',
                         round(likely_true_cases_week))
                st.write('Likely new cases tomorrow: ',
                         round(likely_new_cases_tmrw))
                st.write('Likely new cases in a week: ',
                         round(likely_new_cases_week))

                # ******* Estimation of the likelihood of a person getting infected *******
                st.subheader(
                    'Estimation of the likelihood of a person getting infected'
                )
                num_people = st.number_input(
                    'Number of people in the infected area (must be >= 10,000)',
                    value=10000,
                    min_value=10000)

                try:
                    if num_true_cases >= num_people:
                        no_infection_likelihood_today = 0.0
                        no_infection_likelihood_tmrw = 0.0
                        no_infection_likelihood_next_week = 0.0

                    else:
                        # Current infection rate
                        infection_rate_today = num_true_cases / num_people
                        # Expected infection rate the next day
                        infection_rate_tmrw = likely_true_cases_tmrw / num_people
                        # Expected infection rate a week later
                        infection_rate_next_week = likely_true_cases_week / num_people

                        no_infection_likelihood_today = (
                            (1 - infection_rate_today)**num_employees) * 100
                        no_infection_likelihood_tmrw = (
                            (1 - infection_rate_tmrw)**num_employees) * 100
                        no_infection_likelihood_next_week = ((
                            1 - infection_rate_next_week)**num_employees) * 100

                    st.write(
                        'likelihood that none of your employees already have the coronavirus',
                        f'{round(no_infection_likelihood_today, 3)}%')
                    st.write(
                        'likelihood that none of your employees will have the coronavirus by tomorrow',
                        f'{round(no_infection_likelihood_tmrw, 3)}%')
                    st.write(
                        'likelihood that none of your employees will have the coronavirus by next week',
                        f'{round(no_infection_likelihood_next_week, 3)}%')

                    results(language, risk_probability, method,
                            no_infection_likelihood_today,
                            no_infection_likelihood_tmrw,
                            no_infection_likelihood_next_week)

                except ZeroDivisionError:
                    st.error('Number of people can\'t be zero')

            elif testing_type == '' or community_spread == '':
                pass

            else:
                if testing_type == 'Exhaustive':
                    st.info(
                        'Testing type: only "Connected to a Case" is built right now'
                    )
                if community_spread == 'No':
                    st.info('Community spread: only "Yes" is built right now')

    elif language == 'Somali':
        st.title(
            'COVID-19: Hab-xisaabineed aad ku ogaanayso goorta ay tahey in aad meheradaada xidho'
        )
        for i in range(3):
            st.write(" ")

        st.markdown(
            "> ### Model kaa caawinaya adi iyo sharikadaadaba ka go'aan qaadashada goorma ayaa ay tahey in aad xidho xafiisyada oo shaqaalaha aad fasaxdo"
        )

        for i in range(2):
            st.write(" ")

        st.write(
            "Waxa markii ugu horraysay curiyey model-kan [Tomas Pueyo](https://medium.com/@tomaspueyo) oo ugu talo-galay in ay isticmaalaan akhristayaashaa maqaalkiisii caanka noqdey ee uu ku shaaciyey wargayska Medium [Coronavirus: Why You Must Act Now](https://medium.com/@tomaspueyo/coronavirus-act-today-or-people-will-die-f4d3d9cd99ca), kaasi oo aad aan ugula talinayo cid kasta in ay akhirdo, (tarjumaadiisa somaliga ahna aad ka helaysiin [halkan](https://www.facebook.com/notes/abdurahman-shiine/-covid-19-maxay-waajib-kuugu-tahey-inaad-maanta-fal-qaaddo/2941779562584988/)). Kani waa model kii oo Python lagu qorey, waxana qorey [Abdurahman Shiine](https://github.com/abdurahman-shiine)."
        )
        st.write(
            "Waxa uu model-ku ku salaysanyahey imisa xaaladood oo COVID_19 ah ayaa ay ubadantahey in ay ka jiraan aagaaga, iyo ixtimaalka in mid shaqaalahaaga ka mid ah uu ku qaadi karo xanuunkaas. Xisaabuhu waxa ay ku salaysan yihiin xog laga helayo [halkan](https://docs.google.com/spreadsheets/u/1/d/17YyCmjb2Z2QwMiRRwAb7W0vQoEAiL9Co0ARsl03dSlw/copy?usp=sharing), inteeda badanna ay tahey qiyaasid/dhadhawayn ee ayna ahayn wax la hubo 100%, sidaa darteed haddii aad ubaahato xogta aasaasiga ah wax waa aad ka beddeli kartaa si model-ka aad ugu salaysid duruufahaaga. FG. Xogta inteeda lagama maarmaanka ah kaliya ayaa lagu xareeyey file-ka [model_data.csv](https://github.com/abdurahman-shiine/CoronaVirus-A-model-for-When-you-should-close-your-office) ee uu xogta ka akhrinayo program-kani, sidaa darteed laga yaabaa in xogta qaybo ka mid ah aad kaliya ka hesho lifaaqa hore."
        )
        st.write(
            "Qaabka loo isticmaalayo program-kan waa in aad buuxiso bogosyada bannaan meelaha ka xulasho ubaahanna aad options-ka ku hor yaalla mid ka xulato, si aad model-ka ugu dabbaqdo xaaladaada. Labada luuqadood ee Somali iyo Ingiriisiba waa aad ku isticmaali kartaa program-kan."
        )

        st.markdown(
            "<div align='center'><br>"
            "<img src='https://img.shields.io/badge/MADE%20WITH-PYTHON%20-red?style=for-the-badge'"
            "alt='API stability' height='25'/>"
            "<img src='https://img.shields.io/badge/SERVED%20WITH-Heroku-blue?style=for-the-badge'"
            "alt='API stability' height='25'/>"
            "<img src='https://img.shields.io/badge/DASHBOARDING%20WITH-Streamlit-green?style=for-the-badge'"
            "alt='API stability' height='25'/></div>",
            unsafe_allow_html=True)

        for i in range(3):
            st.write(" ")

        st.header('🌀 Model-ka')
        num_employees = st.number_input('Tirada shaqaalahaaga')
        risk_probability = st.number_input(
            " % khatarta aad xamili karto (Waxa aan xamili karaa ixtimaal ah in mid ama wax ka badan oo shaqaalahayga ka mid ahi ay boqolkiiba intaa xanuunku hayyo.)"
        )
        method = st.selectbox('Habka xisaabinta', [
            '',
            'Habka tirada dhimashooyinka (waa la isku hallayn karaa, isticmaal haddii aagaaga wax dhimasho ahi ka dhacdey)',
            'Habka tirada xaaladaha (Sida habka kale la iskuguma hallayn karo)'
        ])

        # Deaths method
        if method == 'Habka tirada dhimashooyinka (waa la isku hallayn karaa, isticmaal haddii aagaaga wax dhimasho ahi ka dhacdey)':
            total_deaths = st.number_input(
                'Wadarta dhimashooyinka illaa maanta (Tirada xaaladaha ku dhintey COVID-19 aaga xafiisyada sharikadaadu ku yaallaan)'
            )

            st.markdown(
                '### Natiijooyinka soo socdaa waxa ay ku salaysanyihiin:')
            st.markdown('#### - Heer dhimasho = 0.87%')
            st.markdown(
                '#### - Muddada udhaxaysa qaadida xanuunka iyo ku dhimashadiisa = 17.3 maaalmood'
            )
            st.markdown(
                '#### - Muddada ay ku libinlaabmayaan xaaladuhu = 6.2 maalmood'
            )
            st.markdown('#             ')

            fatality_rate = 0.87 / 100
            # How many cases were there some time ago that caused these deaths?
            num_cases_caused_death = total_deaths / fatality_rate
            # How many days did it take for the cases to become deaths? Based on 4 papers
            days_from_infection = 17.3
            # How many days does it take for cases to double?
            doubling_time = 6.2
            # How many times do cases double between the time of the cases that caused deaths and the deaths
            num_times_cases_doubled = days_from_infection / doubling_time
            # Estimation of true cases today in your area
            num_true_cases = num_cases_caused_death * 2**num_times_cases_doubled

            # The likelihood of true and new cases in your area tomorrow and in a week
            likely_true_cases_tmrw = num_true_cases * 2**(1 / doubling_time)
            likely_new_cases_tmrw = likely_true_cases_tmrw - num_true_cases
            likely_true_cases_week = num_true_cases * 2**(7 / doubling_time)
            likely_new_cases_week = likely_true_cases_week - num_true_cases

            st.write('Tirada xaaladaha keenay dhimashada: ',
                     round(num_cases_caused_death))
            st.write('Tirada xaaladaha dhabta ah maanta: ',
                     round(num_true_cases))
            st.write('Tirada la filanayo in ay xaaladuhu barri gaadhaan: ',
                     round(likely_true_cases_tmrw))
            st.write(
                'Tirada la filanayo in ay xaaladuhu 7aad ka bacdi gaadhaan: ',
                round(likely_true_cases_week))
            st.write('Tirada xaaladaha la filanayo in ay barri soo kordhaan: ',
                     round(likely_new_cases_tmrw))
            st.write(
                'Tirada xaaladaha la filanayo in ay 7aad ka bacdi soo kordhaan: ',
                round(likely_true_cases_week))

            # ******* Estimation of the likelihood of a person getting infected *******
            st.subheader(
                'Qiyaasida ixtimaalka uu hal qof ku qaadi karo xanuunka')
            num_people = st.number_input(
                'Tirada dadka ku sugan aaga dhimashadu ka dhacdey (waa in ay >= 10,000)',
                value=10000,
                min_value=10000)

            try:
                if num_true_cases >= num_people:
                    no_infection_likelihood_today = 0.0
                    no_infection_likelihood_tmrw = 0.0
                    no_infection_likelihood_next_week = 0.0

                else:
                    # Current infection rate
                    infection_rate_today = num_true_cases / num_people
                    # Expected infection rate the next day
                    infection_rate_tmrw = likely_true_cases_tmrw / num_people
                    # Expected infection rate a week later
                    infection_rate_next_week = likely_true_cases_week / num_people

                    no_infection_likelihood_today = (
                        (1 - infection_rate_today)**num_employees) * 100
                    no_infection_likelihood_tmrw = (
                        (1 - infection_rate_tmrw)**num_employees) * 100
                    no_infection_likelihood_next_week = (
                        (1 - infection_rate_next_week)**num_employees) * 100

                st.write(
                    'Ixtimaalka in shaqaalahaaga midna uuna qabin COVID-19 hadda',
                    f'{round(no_infection_likelihood_today, 1)}%')
                st.write(
                    'Ixtimaalka in shaqaalahaaga midna uuna qabin doonin COVID-19 barri',
                    f'{round(no_infection_likelihood_tmrw, 1)}%')
                st.write(
                    'Ixtimaalka in shaqaalahaaga midna uuna qabin doonin COVID-19 7aad ka bacdi',
                    f'{round(no_infection_likelihood_next_week, 1)}%')

                results(language, risk_probability, method,
                        no_infection_likelihood_today,
                        no_infection_likelihood_tmrw,
                        no_infection_likelihood_next_week)

            except ZeroDivisionError:
                st.error('Tirada dadka dagan aagu 0 ma noqon karto')

        # Cases method
        if method == 'Habka tirada xaaladaha (Sida habka kale la iskuguma hallayn karo)':
            total_cases = st.number_input(
                'Wadarta tirada xaaladaha ka jira aagaaga maanta ahaan')
            testing_type = st.selectbox(
                'Ma lawada baadhey dadka aaga oo dhan mise kaliya kuwa la kulmey qof buka?',
                ['', 'Kaliya kuwa la kulmey qof buka', 'Waa la wada baadhey'])
            community_spread = st.selectbox(
                'Bulshadu miyey isku faafinaysaa xanuunka?',
                ['', 'Haa', 'Maya'])

            if testing_type == 'Kaliya kuwa la kulmey qof buka' and community_spread == 'Haa':
                df = pd.read_csv('model_data.csv', header=None,
                                 thousands=',').transpose()
                new_header = df.iloc[0]
                df = df[1:]
                df.columns = new_header
                df.reset_index(inplace=True)

                foreign_spread_share_data = df['Share China'].apply(
                    lambda x: float(x.strip('%')) / 100)

                if total_cases <= 180:
                    foreign_spread_share = foreign_spread_share_data[
                        int(total_cases) - 1]
                else:
                    foreign_spread_share = 1 / 100

                num_true_cases = total_cases / foreign_spread_share
                cases_data = df[
                    'Typical contagion as a blend of other countries (US UK DE FR ES IT IR SK JP CH) (you can insert your own #s here)'][:
                                                                                                                                         32]
                cases_data = cases_data.str.replace(',', '').astype(float)
                starting_day_index = min(
                    filter(lambda x: x > num_true_cases, cases_data.tolist()))

                likely_true_cases_tmrw = cases_data[cases_data[
                    cases_data == starting_day_index].index[0]]
                likely_true_cases_week = cases_data[
                    cases_data[cases_data == starting_day_index].index[0] + 6]
                likely_new_cases_tmrw = likely_true_cases_tmrw - num_true_cases
                likely_new_cases_week = likely_true_cases_week - num_true_cases

                st.write('Tirada xaaladaha dhabta ah maanta: ',
                         round(num_true_cases))
                st.write('Tirada la filanayo in ay xaaladuhu barri gaadhaan: ',
                         round(likely_true_cases_tmrw))
                st.write(
                    'Tirada la filanayo in ay xaaladuhu 7aad ka bacdi gaadhaan: ',
                    round(likely_true_cases_week))
                st.write(
                    'Tirada xaaladaha la filanayo in ay barri soo kordhaan: ',
                    round(likely_new_cases_tmrw))
                st.write(
                    'Tirada xaaladaha la filanayo in ay 7aad ka bacdi soo kordhaan: ',
                    round(likely_new_cases_week))

                # ******* Estimation of the likelihood of a person getting infected *******
                st.subheader(
                    'Qiyaasida ixtimaalka uu hal qof ku qaadi karo xanuunka')
                num_people = st.number_input(
                    'Tirada dadka ku sugan aaga xanuunku ku faafey (waa in ay >= 10,000)',
                    value=10000,
                    min_value=10000)

                try:
                    if num_true_cases >= num_people:
                        no_infection_likelihood_today = 0.0
                        no_infection_likelihood_tmrw = 0.0
                        no_infection_likelihood_next_week = 0.0

                    else:
                        # Current infection rate
                        infection_rate_today = num_true_cases / num_people
                        # Expected infection rate the next day
                        infection_rate_tmrw = likely_true_cases_tmrw / num_people
                        # Expected infection rate a week later
                        infection_rate_next_week = likely_true_cases_week / num_people

                        no_infection_likelihood_today = (
                            (1 - infection_rate_today)**num_employees) * 100
                        no_infection_likelihood_tmrw = (
                            (1 - infection_rate_tmrw)**num_employees) * 100
                        no_infection_likelihood_next_week = ((
                            1 - infection_rate_next_week)**num_employees) * 100

                    st.write(
                        'Ixtimaalka in shaqaalahaaga midna uuna qabin COVID-19 hadda',
                        f'{round(no_infection_likelihood_today, 3)}%')
                    st.write(
                        'Ixtimaalka in shaqaalahaaga midna uuna qabin doonin COVID-19 barri',
                        f'{round(no_infection_likelihood_tmrw, 3)}%')
                    st.write(
                        'Ixtimaalka in shaqaalahaaga midna uuna qabin doonin COVID-19 7aad ka bacdi',
                        f'{round(no_infection_likelihood_next_week, 3)}%')

                    results(language, risk_probability, method,
                            no_infection_likelihood_today,
                            no_infection_likelihood_tmrw,
                            no_infection_likelihood_next_week)

                except ZeroDivisionError:
                    st.error('Tirada dadka dagan aagu 0 ma noqon karto')

            elif testing_type == '' or community_spread == '':
                pass

            else:
                if testing_type == 'Waa la wada baadhey':
                    st.info(
                        'Nooca baadhitaanka: kaliya hab-xisaabineedka "Kaliya kuwa la kulmey qof buka" ayaa la sameeyey hadda'
                    )
                if community_spread == 'Maya':
                    st.info(
                        'Isku-faafinta bulshada: kaliya "Haa" ayaa la sameeyey hadda'
                    )

    else:
        pass
示例#15
0
# pylint: disable=invalid-name
from typing import List
import streamlit as st
import awesome_streamlit as ast
from awesome_streamlit.testing.models import (
    TesTItem,  # Special Capitalization is due to PyTest
)

st.markdown("""# Important Notes

This app requires the Awesome Streamlit package.
The Awesome Streamlit package can be installed using

`pip install awesome-streamlit`
""")
st.info("""**Click a button below to start testing** a Test Suite or
study the **test_runner_app source code** below.""")

simple = st.button("Single File")
advanced = st.button("The Awesome Streamlit Gallery")

if simple or advanced:
    if simple:

        def test_items_collector() -> List[TesTItem]:
            """A function to collect a list of test items based on a set of hardcode testitems"""
            return [
                TesTItem(
                    name="Spreadsheet",
                    location=
                    ("https://raw.githubusercontent.com/MarcSkovMadsen/awesome-streamlit/master"
                     "/src/pages/gallery/contributions/marc_skov_madsen/spreadsheet.py"
def main():
    """Hep Mortality Prediction App"""
    # st.title("Hepatitis Mortality Prediction App")
    st.markdown(html_temp.format('royalblue'), unsafe_allow_html=True)

    menu = ["Home", "Login", "SignUp"]
    sub_menu = ["Plot", "Prediction"]  #,"Metrics"]

    choice = st.sidebar.selectbox("Menu", menu)
    if choice == "Home":
        st.subheader("Home")
        # st.text("What is Hepatitis?")
        st.markdown(descriptive_message_temp, unsafe_allow_html=True)
        st.image(load_image('hepimage.jpg'))

    elif choice == "Login":
        username = st.sidebar.text_input("Username")
        password = st.sidebar.text_input("Password", type='password')
        if st.sidebar.checkbox("Login"):
            create_usertable()
            hashed_pswd = generate_hashes(password)
            result = login_user(username, verify_hashes(password, hashed_pswd))
            # if password == "12345":
            if result:
                st.success("Welcome {}".format(username))

                activity = st.selectbox("Activity", sub_menu)
                if activity == "Plot":
                    st.subheader("Data Vis Plot")
                    df = pd.read_csv("clean_hepatitis_dataset.csv")
                    st.dataframe(df)

                    df['class'].value_counts().plot(kind='bar')
                    st.pyplot()

                    # Freq Dist Plot
                    freq_df = pd.read_csv("freq_df_hepatitis_dataset.csv")
                    st.bar_chart(freq_df['count'])

                    if st.checkbox("Area Chart"):
                        all_columns = df.columns.to_list()
                        feat_choices = st.multiselect("Choose a Feature",
                                                      all_columns)
                        new_df = df[feat_choices]
                        st.area_chart(new_df)

                elif activity == "Prediction":
                    st.subheader("Predictive Analytics")

                    age = st.number_input("Age", 7, 80)
                    sex = st.radio("Sex", tuple(gender_dict.keys()))
                    steroid = st.radio("Do You Take Steroids?",
                                       tuple(feature_dict.keys()))
                    antivirals = st.radio("Do You Take Antivirals?",
                                          tuple(feature_dict.keys()))
                    fatigue = st.radio("Do You Have Fatigue",
                                       tuple(feature_dict.keys()))
                    spiders = st.radio("Presence of Spider Naeve",
                                       tuple(feature_dict.keys()))
                    ascites = st.selectbox("Ascities",
                                           tuple(feature_dict.keys()))
                    varices = st.selectbox("Presence of Varices",
                                           tuple(feature_dict.keys()))
                    bilirubin = st.number_input("bilirubin Content", 0.0, 8.0)
                    alk_phosphate = st.number_input(
                        "Alkaline Phosphate Content", 0.0, 296.0)
                    sgot = st.number_input("Sgot", 0.0, 648.0)
                    albumin = st.number_input("Albumin", 0.0, 6.4)
                    protime = st.number_input("Prothrombin Time", 0.0, 100.0)
                    histology = st.selectbox("Histology",
                                             tuple(feature_dict.keys()))
                    feature_list = [
                        age,
                        get_value(sex, gender_dict),
                        get_fvalue(steroid),
                        get_fvalue(antivirals),
                        get_fvalue(fatigue),
                        get_fvalue(spiders),
                        get_fvalue(ascites),
                        get_fvalue(varices), bilirubin, alk_phosphate, sgot,
                        albumin,
                        int(protime),
                        get_fvalue(histology)
                    ]
                    st.write(len(feature_list))
                    st.write(feature_list)
                    pretty_result = {
                        "age": age,
                        "sex": sex,
                        "steroid": steroid,
                        "antivirals": antivirals,
                        "fatigue": fatigue,
                        "spiders": spiders,
                        "ascites": ascites,
                        "varices": varices,
                        "bilirubin": bilirubin,
                        "alk_phosphate": alk_phosphate,
                        "sgot": sgot,
                        "albumin": albumin,
                        "protime": protime,
                        "histolog": histology
                    }
                    st.json(pretty_result)
                    single_sample = np.array(feature_list).reshape(1, -1)

                    # ML
                    model_choice = st.selectbox("Select Model",
                                                ["LR", "KNN", "DecisionTree"])
                    if st.button("Predict"):
                        if model_choice == "KNN":
                            loaded_model = load_model("knn_hepB_model.pkl")
                            prediction = loaded_model.predict(single_sample)
                            pred_prob = loaded_model.predict_proba(
                                single_sample)
                        elif model_choice == "DecisionTree":
                            loaded_model = load_model(
                                "decision_tree_clf_hepB_model.pkl")
                            prediction = loaded_model.predict(single_sample)
                            pred_prob = loaded_model.predict_proba(
                                single_sample)
                        else:
                            loaded_model = load_model(
                                "logistic_regression_hepB_model.pkl")
                            prediction = loaded_model.predict(single_sample)
                            pred_prob = loaded_model.predict_proba(
                                single_sample)

                        # st.write(prediction)
                        # prediction_label = {"Die":1,"Live":2}
                        # final_result = get_key(prediction,prediction_label)
                        if prediction == 1:
                            st.warning("Patient Dies")
                            pred_probability_score = {
                                "Die": pred_prob[0][0] * 100,
                                "Live": pred_prob[0][1] * 100
                            }
                            st.subheader(
                                "Prediction Probability Score using {}".format(
                                    model_choice))
                            st.json(pred_probability_score)
                            st.subheader("Prescriptive Analytics")
                            st.markdown(prescriptive_message_temp,
                                        unsafe_allow_html=True)

                        else:
                            st.success("Patient Lives")
                            pred_probability_score = {
                                "Die": pred_prob[0][0] * 100,
                                "Live": pred_prob[0][1] * 100
                            }
                            st.subheader(
                                "Prediction Probability Score using {}".format(
                                    model_choice))
                            st.json(pred_probability_score)

                    if st.checkbox("Interpret"):
                        if model_choice == "KNN":
                            loaded_model = load_model("knn_hepB_model.pkl")

                        elif model_choice == "DecisionTree":
                            loaded_model = load_model(
                                "decision_tree_clf_hepB_model.pkl")

                        else:
                            loaded_model = load_model(
                                "logistic_regression_hepB_model.pkl")

                            # loaded_model = load_model("models/logistic_regression_model.pkl")
                            # 1 Die and 2 Live
                            df = pd.read_csv("clean_hepatitis_dataset.csv")
                            x = df[[
                                'age', 'sex', 'steroid', 'antivirals',
                                'fatigue', 'spiders', 'ascites', 'varices',
                                'bilirubin', 'alk_phosphate', 'sgot',
                                'albumin', 'protime', 'histology'
                            ]]
                            feature_names = [
                                'age', 'sex', 'steroid', 'antivirals',
                                'fatigue', 'spiders', 'ascites', 'varices',
                                'bilirubin', 'alk_phosphate', 'sgot',
                                'albumin', 'protime', 'histology'
                            ]
                            class_names = ['Die(1)', 'Live(2)']
                            explainer = lime.lime_tabular.LimeTabularExplainer(
                                x.values,
                                feature_names=feature_names,
                                class_names=class_names,
                                discretize_continuous=True)
                            # The Explainer Instance
                            exp = explainer.explain_instance(
                                np.array(feature_list),
                                loaded_model.predict_proba,
                                num_features=13,
                                top_labels=1)
                            exp.show_in_notebook(show_table=True,
                                                 show_all=False)
                            # exp.save_to_file('lime_oi.html')
                            st.write(exp.as_list())
                            new_exp = exp.as_list()
                            label_limits = [i[0] for i in new_exp]
                            # st.write(label_limits)
                            label_scores = [i[1] for i in new_exp]
                            plt.barh(label_limits, label_scores)
                            st.pyplot()
                            plt.figure(figsize=(20, 10))
                            fig = exp.as_pyplot_figure()
                            st.pyplot()

            else:
                st.warning("Incorrect Username/Password")

    elif choice == "SignUp":
        new_username = st.text_input("User name")
        new_password = st.text_input("Password", type='password')

        confirm_password = st.text_input("Confirm Password", type='password')
        if new_password == confirm_password:
            st.success("Password Confirmed")
        else:
            st.warning("Passwords not the same")

        if st.button("Submit"):
            create_usertable()
            hashed_new_password = generate_hashes(new_password)
            add_userdata(new_username, hashed_new_password)
            st.success("You have successfully created a new account")
            st.info("Login to Get Started")
示例#17
0
# Title
st.title("Streamlit Tutorial")
# Heading
st.header('Heading')
# sub-heading
st.subheader('Sub-heading')
# Text
st.text("Hello World")

# Markdown
st.markdown("#### Markdown")

# Error and colorful text
st.subheader('Some coloured tags')
st.success("Success")
st.info("Information")
st.warning("Warning!")
st.error("Error!")
st.exception("A dummy Exception")

# immediate printing help for any python fucntion etc.
st.help(range)

# writing text with the function
st.write("Some text with the function")
st.write(range(10))

# Images
from PIL import Image
st.subheader('Displaying Images')
img = Image.open('example_image.png')
示例#18
0
from random import random
import os
import platform
import streamlit as st
import time

st.title("Test of run-on-save")
secs_to_wait = 5
"""
How to test this:
"""

st.info("""
    **First of all, make sure you're running the dev version of Streamlit** or
    that this file lives outside the Streamlit distribution. Otherwise, changes
    to this file may be ignored!
""")
"""
1. If run-on-save is on, make sure the page changes every few seconds. Then
   turn run-on-save off in the settigns menu and check (2).
2. If run-on-save is off, make sure "Rerun"/"Always rerun" buttons appear in
   the status area. Click "Always rerun" and check (1).
"""

st.write("This should change every ", secs_to_wait, " seconds: ", random())

# Sleep for 5s (rather than, say, 1s) because on the first run we need to make
# sure Streamlit is fully initialized before the timer below expires. This can
# take several seconds.
status = st.empty()
示例#19
0
[福井県新型コロナウイルス感染症のオープンデータ](https://www.pref.fukui.lg.jp/doc/toukei-jouhou/covid-19.html) 
""")

st.sidebar.write("""
[COVID-19 Japan-新型コロナウイルス対策ダッシュボード](https://www.stopcovid19.jp/) 
""")

st.sidebar.write('')
st.sidebar.write("""
(C)2021 Pukumon Go All rights reserved.
""")

st.title('福井県新型コロナウイルス情報')
now = datetime.now(timezone(timedelta(hours=9))).strftime("%Y/%m/%d %H:%M")
st.info(
    now + '現在公開分まで\n' +
    '\nデータ元:福井県新型コロナウイルス感染症のオープンデータhttps://www.pref.fukui.lg.jp/doc/toukei-jouhou/covid-19_d/fil/covid19_patients.csv'
)
'\n'

url = 'https://www.pref.fukui.lg.jp/doc/toukei-jouhou/covid-19_d/fil/covid19_patients.csv'
r = requests.get(url).content
df = pd.read_csv(
    io.BytesIO(r), index_col='No',
    sep=',').drop(index=['※1011は欠番']).dropna(how='all').fillna("非公表")

# indexの型変更
df.index = df.index.astype(int)
df = df.sort_values('No', ascending=False)

st.write('陽性患者属性')
st.dataframe(df[['公表_年月日', '患者_居住地', '患者_年代', '患者_性別', '患者_職業']],
示例#20
0
        test_nb_tokens = remove_noise(word_tokenize(test_nb_headline))
        results_nb = clf_nb.classify(
            dict([token, True] for token in test_nb_tokens))
        if results_nb == 1:
            st.write("It's from the Onion!")
        else:
            st.write("It's not from the Onion!")

st.markdown('---')
if st.checkbox('Show background info'):
    st.subheader('Background Info')
    st.write(
        'Taking a dataset of headlines that are either from the Onion or '
        "from the r/NotTheOnion subreddit, let's develop a model that can "
        'determine whether or not the headline is an Onion headline or not!')
    st.write('The Onion is a satirical newspaper that publishes articles '
             'on current events, oftentimes making everyday events as '
             'newsworthy. r/NotTheOnion is a subreddit where users can post '
             'news articles that sound like they belong on the Onion.')
    st.subheader('Sample of dataset used to develop the model (18000 samples)')
    st.markdown('The dataset used to develop the model is found '
                '[here. ](https://www.kaggle.com/chrisfilo/onion-or-not)'
                'The dataset for the model is found on the project '
                '[Github.](https://github.com/oliverchen415/onion_ml/)')
    st.info(
        '1 indicates a headline by the Onion, '
        'while 0 is a real headline found on r/NotTheOnion. '
        'The data was originally 24000 samples, and rebalanced to 18000 (9000 samples of each). '
        'The first five rows are shown below.')
    st.table(df.head())
示例#21
0
v2 = st.selectbox("Variable 2", v2_vals)
v3 = st.multiselect("Variable 3", v3_vals)

if len(v3) == 0:
    st.error(f'Nothing is selected')

v4 = st.slider("Variable 4", v4_range_start, v4_range_end)

v5 = st.text_input("Variable 5")
v6 = st.text_area("Variable 6")

v7 = st.date_input("Variable 7", datetime.now())
v8 = st.time_input("Variable 8", time())


if st.button("Submit and Predict"):
    values_list = [v1, v2, v3, v4, v5, v6, v7, v8]
    X = []
    for element in values_list:
        if type(element) == list:
            for value in element:
                X.append(value)
        else:
            X.append(element)

    if '' in X:
        st.error(f"Empty values submitted")
    else:
        st.info(f"Variables Submitted: {X}")
        st.success(f"Use ML model to predict using {X}")
示例#22
0
if minimum > maximum:
    st.error("Por favor ingrese un rando válido")
else:
    df.query("@minimum<=number_of_reviews<=@maximum").sort_values("number_of_reviews", ascending=False)\
        .head(50)[["name", "number_of_reviews", "neighbourhood", "host_name", "room_type", "price"]]

st.write(
    "486 es el número más alto de comentarios y dos propiedades lo tienen.\
     Ambos están en el Este de Elmhurst y son habitaciones privadas con precios de $ 65 y $ 45. \
     En general, los listados con más de 400 comentarios tienen un precio inferior a $ 100. \
     Algunos cuestan entre $ 100 y $ 200, y solo uno tiene un precio superior a $ 200."
)

#EXTRA
st.header("Bónus Imagenes")
st.info("Como desplegar imagenes")
pics = {
    "Cat":
    "https://cdn.pixabay.com/photo/2016/09/24/22/20/cat-1692702_960_720.jpg",
    "Puppy":
    "https://cdn.pixabay.com/photo/2019/03/15/19/19/puppy-4057786_960_720.jpg",
    "Sci-fi city":
    "https://storage.needpix.com/rsynced_images/science-fiction-2971848_1280.jpg"
}
pic = st.selectbox("Escoja una imagen", list(pics.keys()), 0)
st.image(pics[pic], use_column_width=True, caption=pics[pic])

st.markdown("## Hemos Finalizado!")
btn = st.button("Celebrar!")
if btn:
    st.balloons()
示例#23
0
##Datos limpios
st.success("Estos son los datos limpios")
dato.shape

dato.isnull().sum()
dato.head()

##tabla
dato = get_data()
st.dataframe(dato.head())
##mapa
st.map(dato)

##1. ##
st.info("¿Qué tipo de alojamiento es el que más hay (cuarto, dept.completo, etc)?")
tipal=dato.room_type.value_counts().head()
tipal

##2. ##
#import matplotlib.pyplot as plt
st.info("¿Cuales son los neighbourhood con más alojamientos?")
masal=dato.neighbourhood.value_counts().head()
masal 
#dato.neighbourhood.value_counts(normalize=True).plot.barh()

##5##
st.info("Mostrar la distribución de los precios de los alojamientos")
distr =(dato.groupby('price').size())
distr
示例#24
0
def getPosts():
  blue_posts = pd.read_json('https://us.forums.blizzard.com/en/wow/groups/blizzard-tracker/posts.json')
  blue_posts.drop_duplicates(subset=['topic_id'], inplace=True)
  blue_posts.reset_index(drop=True, inplace=True)
  return blue_posts
  

last_blue_posts = getPosts()[['title', 'url']].head(5)
last_blue_posts.loc[len(last_blue_posts)] = ['Enter an URL 🔗', None]

title = st.radio( 
  "Enter an URL, or pick one of the most recent developer posts:", last_blue_posts, index=5)

url = last_blue_posts.loc[last_blue_posts['title'] == title]['url'].iloc[0]
if url is None:
  st.info('The model can only judge English posts.')
  url = st.text_input('URL', value="https://us.forums.blizzard.com/en/wow/t/redeemed-soul/737747")
else:
  # https://us.forums.blizzard.com/en/wow/t/<thread_id>.json
  url = 'https://us.forums.blizzard.com/en/wow'+url
  st.markdown('Forum Link: [%s](%s)' % (title, url))


if not uri_validator(url):
  "URL is not valid: " + url
  st.stop()


url = url + '.json'

post_ids = requests.get(url).json()['post_stream']['stream']
示例#25
0
def RFR():
    try:
        parameter_test_size = st.sidebar.slider(
            "test size (fraction)", 0.02, 0.90, 0.2)
        st.sidebar.write("test size: ", parameter_test_size)
        parameter_n_estimators = st.sidebar.slider("n estimators", 1, 100, 10)
        st.sidebar.write("n  estimators: ", parameter_n_estimators)

        st.sidebar.info("""
            [More information](http://gonzalezmaw.pythonanywhere.com/)
            """)

        uploaded_file = st.file_uploader("Choose a file")
        if uploaded_file is not None:

            df = pd.read_csv(uploaded_file)

            #X = df.iloc[:, :-1].values
            X = df.iloc[:, 0].values
            X_name = df.iloc[:, 0].name
            y_name = df.iloc[:, -1].name
            y = df.iloc[:, -1].values  # Selecting the last column as Y

            #y_name = df.iloc[:, 1].name

            # Taking N% of the data for training and (1-N%) for testing:
            num = int(len(df)*(1-parameter_test_size))

            # training data:
            train = df[:num]
            # Testing data:
            test = df[num:]

            X_train = np.array(train[[X_name]])
            y_train = np.array(train[[y_name]])
            X_test = np.array(test[[X_name]])
            y_test = np.array(test[[y_name]])

            regressor = RandomForestRegressor(
                n_estimators=parameter_n_estimators, random_state=0)
            regressor.fit(X_train.reshape(-1, 1), y_train.reshape(-1, 1))

            y_pred = regressor.predict(X_test.reshape(-1, 1))

            df2 = pd.DataFrame({'Real Values': y_test.reshape(-1),
                                'Predicted Values': y_pred.reshape(-1)})

            # # Visualising the Random Forest Regression Results

            showData = st.checkbox('Show Dataset')

            if showData:
                st.subheader('Dataset')
                st.write(df)
                figure1, ax = plt.subplots()
                ax.scatter(X, y, label="Dataset")
                plt.title('Dataset')
                plt.xlabel(X_name)
                plt.ylabel(y_name)
                plt.legend()
                st.pyplot(figure1)

            st.subheader("""
                **Regression**
                """)
            st.write(df2)

            st.subheader('Coefficient of determination ($R^2$):')
            resultR2 = round(r2_score(y_test, y_pred), 6)

            st.info(resultR2)

            # chart = st.line_chart(X_test, y_test)
            figure2, ax = plt.subplots()
            ax.scatter(X_test, y_test, label="Real values", color='green')
            ax.scatter(X_test, y_pred, label="Predicted values", color='red')
            plt.title('Random Forest Regression')
            plt.xlabel(X_name)
            plt.ylabel(y_name)
            plt.legend()
            st.pyplot(figure2)

            X_grid = np.arange(min(X), max(X), 0.01)
            X_grid = X_grid.reshape((len(X_grid), 1))
            figure3, ax2 = plt.subplots()
            ax2.plot(X_grid, regressor.predict(X_grid),
                     label="Prediction", color='black')
            plt.title('Random Forest Regression')
            plt.xlabel(X_name)
            plt.ylabel(y_name)
            plt.legend()
            st.pyplot(figure3)

            st.subheader("Prediction:")
            X_new = st.number_input('Input a number:')

            st.write("Result:")
            y_new = np.reshape(X_new, (1, -1))
            y_new = regressor.predict(y_new)
            st.info(round(y_new[0], 6))

            # st.set_option('deprecation.showPyplotGlobalUse', False)
        else:
            st.info('Awaiting for CSV file to be uploaded.')
    except:
        st.info('ERROR - please check your Dataset')
    return
示例#26
0
文件: main.py 项目: tejasvi/routero
                'Choose source depot ID',range(10),index=4)
        srcdf0 = df0.loc[df0['src'] == src]
        ntype = srcdf0['type'].nunique()
        tp = st.selectbox(
                'Choose vehicle type',range(int(ntype)+1),index=2,
                format_func=(lambda x: ['Pickup','Truck','Any'][x]))
        if tp != 2:
            vehdf0 = srcdf0.loc[srcdf0['type']==tp]
            veh = st.selectbox(
                    'Choose vehicle ID',range(vehdf0.shape[0]),
                        index=0)
            getpath(src, tp, veh)
        else:
            srcpath(src)
    else:
        st.info('Full demo takes few seconds to load..')
        fullpath()
    st.write("_(hover and scroll to zoom)_")
    '''
    The hackathon was a great learning opportunity to get familiar with cloud-based development. Since we were free to choose our dataset, a considerable effort put into ensuring our synthetic data was sufficiently realistic. Apart from that, we experimented with multiple existing approaches to VRP, including genetic algorithms and recursive-DBSCAN. However, we found our strategy to be most performant when scaled to large data. One of the primary reasons is the relative simplicity and the speed of execution, which allows much-needed flexibility. 
### Team Members
* Tejasvi S Tomar [@tejasvi](http://github.com/tejasvi)
* Pooja Dhane [@poojaDh](http://github.com/poojadh)

### Data used
Synthetic data based on [The heterogeneous fleet vehicle routing problem with overloads and time windows]( https://www.sciencedirect.com/science/article/pii/S0925527313000388) after heavy modification according to the given specifications.

### Azure Services Used
* Azure Maps (distance API and routing)
* Azure Machine Learning (training modified K-NN)
* Azure Notebooks (prototyping and data analysis)
示例#27
0
def pricePrediction_RandomForest(symbol, days, start_date, end_date):

    #     obtain stock data
    stock_df = stock_data(symbol, start_date, end_date)

    #     obtaining technical indicators
    stochastic_Oscillator(stock_df)
    calc_williams_r(stock_df)
    calc_macd(stock_df)
    calc_price_rate_of_change(stock_df)

    #     set the trading window we are trying to predict
    stock_df_targeted = trading_window(stock_df, days)

    # drop any row with null values
    stock_df_targeted.reset_index(inplace=True)
    stock_df_targeted = stock_df_targeted.dropna()

    stock_df_targeted_scaled = stock_df_targeted.copy()

    # dropping columns that were not used in the data
    stock_df_targeted_scaled.drop([
        'Ticker', '4. close', '7. dividend amount', '3. low',
        '5. adjusted close', '6. volume', '8. split coefficient', 'low_14',
        'high_14', 'MACD_EMA'
    ],
                                  axis=1,
                                  inplace=True)

    # scale feature and target
    target_price = stock_df_targeted_scaled.filter(['Target'])
    stock_df_targeted_scaled = sc.fit_transform(
        stock_df_targeted_scaled.drop(columns=['date', 'Target']))
    target_price = y_sc.fit_transform(target_price)

    # Creating Feature and Target
    X = stock_df_targeted_scaled[:, :6]
    y = target_price

    # splitting data into training and testing
    split = int(0.65 * len(X))
    X_train = X[:split]
    y_train = y[:split]
    X_test = X[split:]
    y_test = y[split:]

    # Building and fitting the model
    rf = RandomForestRegressor(n_estimators=1000)
    rf.fit(X_train, y_train.ravel())

    last_element = X_test[len(X_test) - 1]
    original_prices = stock_df_targeted['4. close'].values
    current_price = original_prices[len(original_prices) - 1]
    last_element = last_element.reshape(1, -1)
    # predicting future price
    predicted_price = rf.predict(last_element)
    predicted_price = predicted_price.reshape(1, -1)

    # making model prediction on the whole dataset
    pred_rf = rf.predict(X)
    # making predictions on the test data
    eval_predict = rf.predict(X_test)

    # calculating profits
    profitInXDays = y_sc.inverse_transform(predicted_price)
    share_amount = investment_amount / current_price
    profit = round((share_amount * profitInXDays[0][0]) - investment_amount, 2)

    # getting predicted prices for for the test data
    Predicted = []
    for i in pred_rf:
        Predicted.append(i)
    # getting the original prices
    close = []
    for i in stock_df_targeted_scaled:
        close.append(i[0])
    # adding predicted and actual prices to a data frame
    df_predicted = stock_df_targeted[['date']]
    df_predicted['Close'] = close
    df_predicted['Prediction'] = Predicted

    # evaluating the prediction metrics
    RMSE.append(math.sqrt(mean_squared_error(y_test, eval_predict)))
    Rsquared.append(r2_score(y_test, eval_predict))
    Mae.append(mean_absolute_error(y_test, eval_predict))
    adj_Rsquared.append(1 - (1 - r2_score(y_test, eval_predict)) *
                        (len(y_test) - 1) / (len(y_test) - X.shape[1] - 1))

    # plottig actual vs predicted graph
    interactive_plot(df_predicted, "Original Vs. Prediction for ")

    # appending information to web page
    st.info("in {} day(s) the price of this stock will be £{}".format(
        days, round(profitInXDays[0][0], 2)))
    st.info("You would make £{} in {} day(s)".format(profit, days))
def main():
    menu = ['Home', 'Login', 'Signup']
    st.sidebar.title("MENU")
    choice = st.sidebar.selectbox("Menu", menu)
    st.markdown(
        """
    <style>
    .sidebar .sidebar-content {
        background: url(https://images.pexels.com/photos/1111316/pexels-photo-1111316.jpeg?auto=compress&cs=tinysrgb&dpr=1&w=500);
        color: white;
    }
    </style>
    """,
        unsafe_allow_html=True,
    )
    #st.sidebar.button("GOKU")

    if (choice == "Home"):
        st.title("Welcome To the HomePage")
        homepagebck()

        #[[thisisanimagelink](upload://7FxfXwDqJIZdYJ2QYADywvNRjB.png)](https://data.cityofnewyork.us/Public-Safety/Motor-Vehicle-Collisions-Crashes/h9gi-nx95/data)
        st.subheader("Latest Traffic updates")
        date = datetime.datetime.now()
        st.subheader(date)
        query_view = view_all_info()
        clean_db = pd.DataFrame(
            query_view, columns=["LOCATION", "STREET NAME", "TYPE OF TRAFFIC"])
        st.dataframe(clean_db)
        #show data and ...
        #st.write("https://data.cityofnewyork.us/Public-Safety/Motor-Vehicle-Collisions-Crashes/h9gi-nx95/data")

    elif (choice == "Login"):
        st.header("Login Section")

        # if (st.sidebar.button("veronica")):
        #     deleteUserNull()
        loginpagebck()

        username = st.sidebar.text_input("Username")
        password = st.sidebar.text_input("Password", type='password')
        if (st.sidebar.checkbox("Login")):

            create_usertable()
            result = login_user(username, password)
            if (result):

                st.success("Logged in as {}".format(username))

                task = st.selectbox("Task", [
                    "UPDATE",
                    "Data Set",
                    "Visualise",
                ])
                #update column of login page
                if (task == "UPDATE"):
                    st.subheader(
                        "Add current traffic update here and see that table")

                    create_todaytraffic()

                    borough = st.text_input("enter the borough")
                    on_street_name = st.text_input("enter the on_street_name")
                    type = st.text_input("enter the type")
                    #update button in updateLoginpage
                    if (st.button("UPDATE")):
                        add_trafficdata(borough, on_street_name, type)
                        query_view = view_all_info()
                        clean_db = pd.DataFrame(query_view,
                                                columns=[
                                                    "LOCATION", "STREET NAME",
                                                    "TYPE OF TRAFFIC"
                                                ])
                        st.dataframe(clean_db)
                    if (st.button("Drop all from today")):
                        deleteNull()
                        query_view = view_all_info()
                        clean_db = pd.DataFrame(query_view,
                                                columns=[
                                                    "LOCATION", "STREET NAME",
                                                    "TYPE OF TRAFFIC"
                                                ])
                        st.dataframe(clean_db)

                elif (task == "Data Set"):
                    st.subheader("Data Set from Uber about Newyork City")
                    #add set here

                    DATA_URL = (
                        " https://data.cityofnewyork.us/Public-Safety/Motor-Vehicle-Collisions-Crashes/h9gi-nx95/data "
                    )

                    st.markdown(
                        "This application is a streamlit dashboared that can "
                        "be used to analyse motor vhicle collision in nyc")

                    data = load_data(100000)
                    if (st.checkbox("data head", False)):
                        st.write(data.head())
                        st.info("Successfully Loaded Data Frame Head")
                    if st.checkbox("Show Raw Data", False):
                        st.subheader('Raw data')
                        st.info("Note That The Dataset is very large")
                        st.write(data)
                    # if(st.button("Visit Column Name")):
                    #     for col in data.columns:
                    #         st.write(col)

                elif (task == "Visualise"):
                    #st.subheader("Edith")

                    #add plots here
                    DATA_URL = (
                        " https://data.cityofnewyork.us/Public-Safety/Motor-Vehicle-Collisions-Crashes/h9gi-nx95/data "
                    )

                    st.markdown(
                        "This application is a streamlit dashboared that can "
                        "be used to analyse motor vhicle collision in nyc")

                    data = load_data(100000)
                    original_data = data
                    if (st.checkbox("data head", False)):
                        st.write(data.head())
                        st.info("Successfully Loaded Data Frame Head")
                    if st.checkbox("Show Raw Data", False):
                        st.subheader('Raw data')
                        st.info("Note That The Dataset is very large")
                        st.write(data)
                    st.header("Where are he most people injured in NYC?")
                    injured_people = st.slider(
                        "Number of persons injured in vehicle collisions", 0,
                        19)
                    st.map(
                        data.query("injured_persons >= @injured_people")[[
                            "latitude", "longitude"
                        ]].dropna(how="any"))

                    st.header(
                        "How many collisions occured during a given time of day?"
                    )
                    hour = st.slider("Hour to look at", 0, 23)
                    data = data[data['crash_date_crash_time'].dt.hour == hour]

                    st.markdown("Vehicle Collisions between %i:00 and %i:00" %
                                (hour, (hour + 1) % 24))

                    midpoint = (np.average(data['latitude']),
                                np.average(data['longitude']))

                    st.write(
                        pdk.Deck(
                            map_style="mapbox://styles/mapbox/light-v9",
                            initial_view_state={
                                "latitude": midpoint[0],
                                "longitude": midpoint[1],
                                "zoom": 11,
                                "pitch": 50,
                            },
                            layers=[
                                pdk.Layer(
                                    "HexagonLayer",
                                    data=data[[
                                        'crash_date_crash_time', 'latitude',
                                        'longitude'
                                    ]],
                                    get_position=['longitude', 'latitude'],
                                    radius=100,
                                    extruded=True,
                                    pickable=True,
                                    elevation_range=[0, 1000],
                                ),
                            ],
                        ))

                    st.subheader(
                        "Breakdown by minute between %i:00 and %i:00" %
                        (hour, (hour + 1) % 24))
                    filtered = data[
                        (data['crash_date_crash_time'].dt.hour >= hour)
                        & (data['crash_date_crash_time'].dt.hour < (hour + 1))]
                    hist = np.histogram(
                        filtered['crash_date_crash_time'].dt.minute,
                        bins=60,
                        range=(0, 60))[0]
                    chart_data = pd.DataFrame({
                        'minute': range(60),
                        'crashes': hist
                    })
                    fig = px.bar(chart_data,
                                 x='minute',
                                 y='crashes',
                                 hover_data=['minute', 'crashes'],
                                 height=400)
                    st.write(fig)

                    st.header("Top 5 dangerous streets by affected type")
                    st.markdown(
                        "based on visualisation and data interpretation")
                    select = st.selectbox(
                        'Affected type of people',
                        ['Pedestrians', 'Cyclists', 'Motorists'])

                    if (select == 'Pedestrians'):
                        st.write(
                            original_data.query("injured_pedestrians >= 1")[[
                                "on_street_name", "injured_pedestrians"
                            ]].sort_values(
                                by=['injured_pedestrians'],
                                ascending=False).dropna(how="any")[:5])

                    elif (select == 'Cyclists'):
                        st.write(
                            original_data.query("injured_cyclists >= 1")[[
                                "on_street_name", "injured_cyclists"
                            ]].sort_values(
                                by=['injured_cyclists'],
                                ascending=False).dropna(how="any")[:5])

                    else:
                        st.write(
                            original_data.query("injured_motorists >= 1")[[
                                "on_street_name", "injured_motorists"
                            ]].sort_values(
                                by=['injured_motorists'],
                                ascending=False).dropna(how="any")[:5])

            else:
                st.warning("Incorrect Username/Password")
                st.markdown("Enter correct Username/Password and try again")
                st.info("Go in sidebar to Signup for free")

    elif (choice == "Signup"):
        signuppagebck()
        # background for signup pages
        #https://images.pexels.com/photos/399161/pexels-photo-399161.jpeg?cs=srgb&dl=pexels-lumn-399161.jpg&fm=jpg
        st.subheader("Create New Account")
        new_user = st.text_input("Username")
        new_password = st.text_input("Password", type="password")

        if (st.button("Signup")):
            create_usertable()
            add_userdata(new_user, new_password)
            st.success("You Have Succesfully Created a Valid Account")
            st.info("Go in to Login Menu to login ")
        df1 = data[data["label"] == word_sentiment]
        words = " ".join(df1["text"])
        processed_words = " ".join(
            [
                word
                for word in words.split()
                if "http" not in word and not word.startswith("@") and word != "RT"
            ]
        )
        wordcloud = WordCloud(
            stopwords=STOPWORDS, background_color="white", height=640, width=800
        ).generate(processed_words)
        plt.imshow(wordcloud)
        plt.xticks([])
        plt.yticks([])
        st.pyplot()

elif select == "Source Code":
    # ******************Display Source Code***********************#
    st.info("---------------------------------------------Source Code-------------------------------------------")
    st.code(get_file_content_as_string("app.py"))

elif select == "About":
    # ******************About Developer***********************#
    st.subheader("About:Twitter Sentiment & Intent Analysis App on Coronavirus 🦠 😷🔬")
    st.info("Built with Streamlit,nltk, SVM based Intent recognizer, googlemaps & tweepy")
    st.markdown("### **Karry Harsh**")
    st.markdown("Gmail: [✉]([email protected]) ⬅ click")
    st.markdown("Linkedin: [🧑‍💼](https://www.linkedin.com/in/karryharsh/) ⬅ click")
    st.markdown("Github: [😺](https://karryharsh.github.io/)⬅ click")
示例#30
0
test_y_ = model.predict(test_x)

from sklearn.metrics import r2_score

score = r2_score(test_y_, test_y)
st.subheader('The Prediction Accuracy of this model is ' + str(score))

st.header('')
st.subheader('Green = ScatterPlot')
st.subheader('Red = Prediction for Test cases')
plt.scatter(train['ENGINESIZE'], train['CO2EMISSIONS'], color='green')
plt.title('Graph of Linear Regression')
plt.xlabel('ENGINE SIZE')
plt.ylabel('CO2 EMISSIONS')
plt.plot(test['ENGINESIZE'], test_y_, color='red')
st.pyplot()

st.info("Hello, check the box below to show the model code!")


@st.cache
def codeData():
    file_object = open('modelC.py', 'r')
    data00 = file_object.read()
    return data00


if st.checkbox('Show Model Code'):
    data = codeData()
    st.code(data)