diff --git a/essays/smart-questions.md b/essays/smart-questions.md index 3d73f03..d27b562 100644 --- a/essays/smart-questions.md +++ b/essays/smart-questions.md @@ -1,99 +1,76 @@ --- -layout: essay -type: essay -title: "Smart Questions, Good Answers" +layout: project +type: project +title: "Concrete Strength Predictions" # All dates must be YYYY-MM-DD format! -date: 2015-09-08 +date: 2023-12-12 published: true labels: - - Questions - - Answers - - StackOverflow + - Machine Learning + - Data Analysis + - Model Building --- - - - -## Is there such thing as a stupid question? - -I’ve had instructors address a whole class and say, “There’s no such thing as a stupid question.” I now know that is in fact not true because I’ve challenged the statement and received the appropriate dumb-stricken, annoyed look. There are definitely stupid questions, and along with that, usually unhelpful answers. Though we all might be guilty of being callous and making people victim to our poorly formed questions, there are steps we can take to ask smarter questions that hopefully don’t illicit the dreaded “rtfm” or “stfw” response. - -## What’s a smart question? - -Stack Overflow, a question and answer site for programmers, is a great resource for anyone who may have issues with code or who may simply want to learn new or different methods of doing something. There I found examples of good questions and bad questions, which could probably be improved. - -In the following example, we examine the components of a decent question. In this case, the asker is trying to figure out a way to get the date of the previous month in Python. - -``` -Q: python date of the previous month - -I am trying to get the date of the previous month with python. Here is what i've tried: - -str( time.strftime('%Y') ) + str( int(time.strftime('%m'))-1 ) - -However, this way is bad for 2 reasons: First it returns 20122 for the February of 2012 (instead of 201202) -and secondly it will return 0 instead of 12 on January. - -I have solved this trouble in bash with: - -echo $(date -d"3 month ago" "+%G%m%d") - -I think that if bash has a built-in way for this purpose, then python, much more equipped, should provide something -better than forcing writing one's own script to achieve this goal. Of course i could do something like: - -if int(time.strftime('%m')) == 1: - return '12' -else: - if int(time.strftime('%m')) < 10: - return '0'+str(time.strftime('%m')-1) - else: - return str(time.strftime('%m') -1) - -I have not tested this code and i don't want to use it anyway (unless I can't find any other way:/) - -Thanks for your help! +## Project Description +For my freshman year Computational Thinking and Data Science(ENGR 1330) final, we had to take a given set of data about concrete strength based on mix ratio and create an python program that could predict the strength of other mixtures, created by the user. To do this, we used a variety of libraries such as [Pandas](https://pandas.pydata.org), [matplotlib](matplotlib.org), [SeaBorn](https://seaborn.pydata.org/#), [Sklearn](https://scikit-learn.org/stable/) and more, to build RFR models and analyze their results to calculate the most optimal concrete mix ratio. + +## Project Excerpts +To predict and model our data, we used a RFR Model style, which displays its deviation from our expected values. To do this, we implemented the following code. +```python +# Create a RandomForestRegressor model with 100 estimators +rfr0 = RandomForestRegressor(n_estimators=100) + +# Train the model on the training data +rfr0.fit(x_train, y_train) + +# Use the trained model to predict y values for the test set +y_pred_rfr0 = rfr0.predict(x_test) + +# Print evaluation metrics for the model +print("model\t\t\t\t RMSE \t\t MSE \t\t MAE \t\t R2") +print("""RandomForestRegressor \t {:.2f} \t\t {:.2f} \t\t {:.2f} \t\t {:.2f}""".format( + np.sqrt(mean_squared_error(y_test, y_pred_rfr0)), mean_squared_error(y_test, y_pred_rfr0), + mean_absolute_error(y_test, y_pred_rfr0), r2_score(y_test, y_pred_rfr0))) + +# Create a scatter plot comparing true and predicted values +plt.scatter(y_test, y_pred_rfr0) +plt.plot([y_test.min(), y_test.max()], [y_test.min(), y_test.max()], 'k--', lw=2) +plt.xlabel("predicted") +plt.ylabel("true") +plt.title("Random Forest Regressor") + +# Display the plot +plt.show() ``` +> Which returns: -While the heading of his question could be better, it does convey what he’s trying to figure out. Usually something as brief as “python date of previous month” is what other users would enter in as search terms on Google, making it easily found. Another good thing about the question is that it’s not just a question. The asker shows what he or she has done and that he or she has put in some effort to answer the question. And while it may not be as important as the question itself, the asker shows courtesy, which does increase the chance of getting an answer. +![image](https://github.com/cgolden15/cgolden15.github.io/assets/61284764/668eddf7-2a95-433d-963b-806099034ae3) -``` -A: datetime and the datetime.timedelta classes are your friend. +While this may not look like much, this gave us a good baseline of our data for the next step: -1. find today -2. use that to find the first day of this month. -3. use timedelta to backup a single day, to the last day of the previous month. -4. print the YYYYMM string you're looking for. +Creating an Ordinary Least Square(OLS) regression model for each of our 4 tests is rather simple. To do this we created the following code. +```python -Like this: +x1 = df_1[['BlastFurnaceSlag','Cement','FlyAsh','coarseaggregate','FineAggregate','water','superplasticizer','age']] +y1 = df_1['Cement'] - >>> import datetime - >>> today = datetime.date.today() - >>> first = datetime.date(day=1, month=today.month, year=today.year) - >>> lastMonth = first - datetime.timedelta(days=1) - >>> print lastMonth.strftime("%Y%m") - 201202 - >>> +regr = linear_model.LinearRegression() +regr.fit(x1,y1) -``` - -The asker received six possible answers, and he or she was successful in inciting discussion from multiple users. The answers themselves were clear and were devoid of the rumored sarcasm and hostility of “hackers.” Since I myself have referenced this page and found it useful, I can confidently say that it is a good question. - -## The foolproof way to get ignored. - -While there are decent questions that benefit everyone, there are those one can ask to create an entirely different effect. In the following example, a user asks how he would, in short, create a desktop application with Facebook. - -``` -Q: Facebook Desktop Notifier +print('Intercept" \n',regr.intercept_) +print('Coefficiens: \n', regr.coef_) -I am a beginner programmer that have never used anything other than what's included in a language. +new_water = 2.75 +new_age = 5.3 +x= sm.add_constant(x1) -I am trying to create a desktop application that notifies me anytime I get an update onfacebook. -How should go about doing this? Thanks in advance. +model = sm.OLS(y1,x1).fit() +print(print_model) -edit Sorry I was not clear. Is there any way to make a DESKTOP application with facebook? ``` +> Which returns: -A simple “yes” would have answered the question, but we know that’s not the sort of answer he or she is looking for. Fortunately, someone kindly responded with a link to Facebook’s developer website. The asker should have done more research on his or her potential project. Then further down the road, he or she could have asked more specific and detailed questions that wouldn’t require a thousand-paged response for a sufficient answer. +![image](https://github.com/cgolden15/cgolden15.github.io/assets/61284764/79a3bd2c-2fa7-484a-8260-ab0a03ed05b8) -## Conclusion +Both of these sections of code allow us to then go on to model the relationship between our input features and the concrete compressive strength. Eventually after training the models with enough datasets, we can safely do our final calculation and predict the strongest concrete mix ratio. -When we rely on others’ generosity and expertise to provide answers to our questions, it should hold that the question we ask should be one that leads to efficient and effective help that not only benefits us, but also the people we ask and others who might ask the same question in the future. Thus, if you have a question… make it a smart one! Asking questions may not always get you the best answer, but asking them in a way that will make others want to answer them will increase the success of finding a good solution and make it a positive experience on all sides. +**View the full project on [github](https://github.com/cgolden15/concrete-strength/)!**