<jupyter_start><jupyter_text>Pumpkin PricingLoad up required libraries and dataset. Convert the data to a dataframe containing a subset of the data: - Only get pumpkins priced by the bushel- Convert the date to a month- Calculate the price to be an average of high and low prices- Convert the price to reflect the pricing by bushel quantity<jupyter_code>import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

pumpkins = pd.read_csv('../data/US-pumpkins.csv')

pumpkins.head()


pumpkins = pumpkins[pumpkins['Package'].str.contains('bushel', case=True, regex=True)]

new_columns = ['Package', 'Variety', 'City Name', 'Month', 'Low Price', 'High Price', 'Date', 'City Num', 'Variety Num']


pumpkins = pumpkins.drop([c for c in pumpkins.columns if c not in new_columns], axis=1)

price = (pumpkins['Low Price'] + pumpkins['High Price']) / 2

month = pd.DatetimeIndex(pumpkins['Date']).month


new_pumpkins = pd.DataFrame({'Month': month, 'Variety': pumpkins['Variety'], 'City': pumpkins['City Name'], 'Package': pumpkins['Package'], 'Low Price': pumpkins['Low Price'],'High Price': pumpkins['High Price'], 'Price': price})
# label access of arrays
new_pumpkins.loc[new_pumpkins['Package'].str.contains('1 1/9'), 'Price'] = price/1.1

new_pumpkins.loc[new_pumpkins['Package'].str.contains('1/2'), 'Price'] = price*2

new_pumpkins.head()<jupyter_output><empty_output><jupyter_text>A basic scatterplot reminds us that we only have month data from August through December. We probably need more data to be able to draw conclusions in a linear fashion.<jupyter_code>import matplotlib.pyplot as plt
plt.scatter('Month','Price',data=new_pumpkins)
from sklearn.preprocessing import LabelEncoder
# turn strings into #'s
# [0:-1] -> get slice from 0 to the second to last, basically -1 from end
# don't want to mess with the prices
# seems to make them into discrete values
new_pumpkins.iloc[:, 0:-1] = new_pumpkins.iloc[:, 0:-1].apply(LabelEncoder().fit_transform)
new_pumpkins.head(200)
# try to find correlations between two points of data, range of 0-1
print(new_pumpkins['City'].corr(new_pumpkins['Price']))
# 0.32363971816089215
# not very strong correlation between City and Price

print(new_pumpkins['Package'].corr(new_pumpkins['Price']))
# much higher and a better correlation

# time to make the regression model
new_pumpkins.dropna(inplace=True)
# shows data about the table and not the actual data
new_pumpkins.info()
new_cols = ['Package', 'Price']
# axis type determine what we are looking for 
# if looking to remove an example then do a row
# linear_pumpkins = new_pumpkins.drop([c for c in new_pumpkins.columns if c not in new_cols], axis='columns')
linear_pumpkins = new_pumpkins[new_cols]
linear_pumpkins.head()
X = linear_pumpkins.values[:, :1]
y = linear_pumpkins.values[:, 1:2]

from sklearn.linear_model import LinearRegression
from sklearn import linear_model
from sklearn.metrics import r2_score, mean_squared_error, mean_absolute_error
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X,y,random_state=0)
lin_reg_model = linear_model.LinearRegression()
lin_reg_model.fit(X_train, y_train)

pred = lin_reg_model.predict(X_test)
accuracy_score = lin_reg_model.score(X_train,y_train)
print('Model Accuracy: ', accuracy_score)

plt.scatter(X_test, y_test,  color='black')
plt.plot(X_test, pred, color='blue', linewidth=3)

plt.xlabel('Package')
plt.ylabel('Price')
plt.rcParams['figure.facecolor'] = 'white'
plt.show()


# now try to predict
# 2d array b/c this is one package for one example
newPred = lin_reg_model.predict(np.array([[2.75]]))
print(newPred)
# Multiple Linear Regression
new_cols = ['Variety', 'Package', 'City', 'Month', 'Price']
poly_pumpkins = new_pumpkins.drop([c for c in new_pumpkins.columns if c not in new_cols], axis='columns')
poly_pumpkins
# cool warm chart -> correlations between data
corr = poly_pumpkins.corr()
corr.style.background_gradient(cmap='coolwarm')

# PIPELINES
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import make_pipeline
# used for making polynomial regression = chain of estimators (or as we call it polynomial features)
# iloc is index access
X=poly_pumpkins.iloc[:,3:4].values
y=poly_pumpkins.iloc[:,4:5].values
# PolynomialFeatures(1) -> straight line
# PolynomialFeatures(2) -> more like a parabola
# PolynomialFeatures(3) -> looks like a 3rd degree polynomial wave
# PolynomialFeatures(4) -> no diff from 3
pipeline = make_pipeline(PolynomialFeatures(3), LinearRegression())

X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=0)
pipeline.fit(X_train, y_train)

y_pred=pipeline.predict(X_test)
# plotting the prediction and the data
df = pd.DataFrame({'x': X_test[:,0], 'y': y_pred[:,0]})
df.sort_values(by='x',inplace = True)
points = pd.DataFrame(df).to_numpy()
df.head(100)

plt.plot(points[:, 0], points[:, 1],color="blue", linewidth=3)
plt.xlabel('Package')
plt.ylabel('Price')
plt.scatter(X,y, color="black")
plt.show()
# there are 4 lines b/c of the pipeline and because the x vals go from 0 to 4 = 5 points
accuracy_score = pipeline.score(X_train,y_train)
print('Model Accuracy: ', accuracy_score)
pred = pipeline.predict( np.array([ [2.75] ]) )
print('Prediction of 2.75: ', pred)<jupyter_output>Model Accuracy:  0.8537946517073784
Prediction of 2.75:  [[46.34509342]]
{'fraction_non_alphanumeric': 0.12597547380156077, 'fraction_numerical': 0.020995912300260128, 'mean_word_length': 6.244952893674293, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '2', 'dir': 'jupyter-structured-clean-dedup', 'id': '659355', 'license': 'MIT', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1810, 'n_words': 525, 'path': '2-Regression/3-Linear/notebook.ipynb', 'repo_name': 'JuliaWada/ML-For-Beginners', 'size': '5382'}