<jupyter_start><jupyter_text>Importing of primary data frame<jupyter_code>file_path="food_feed.csv"
df=pd.read_csv(file_path, encoding='unicode_escape')
df.head()

by_country=df.groupby(["Area","Element"]).sum()
by_country.head()<jupyter_output><empty_output><jupyter_text>IMPORTING SECONDARY DF WITH WORLD POP(RESHAPING)<jupyter_code>#reading the file with pandas and printing as is 
file_path2="country_population.csv"
df2=pd.read_csv(file_path2)
df2.head()

countries=[]
countries=df2['Country Name'].tolist()
d={'Country Name':'s', 'Year':0, 'Population':0}
df_pop=pd.DataFrame(data=d, index=[0])

index_data=0
index_input=0
for c in countries:  
    for y in range(1961, 2014, 1):
        p=df2.loc[index_data,str(y)]
        df_pop.loc[index_input]=[c,y,p] 
        index_input=index_input+1
    index_data=index_data+1

df_pop.sort_values(by=['Country Name'])
df_pop.head()

df_pop_filter=df_pop.loc[df_pop['Country Name'] == 'World']
total_pop=df_pop_filter.groupby(["Year"]).sum()
total_pop['Population_bi']=total_pop['Population']/1000000000
total_pop.head()
year=[]
for x in range(1961, 2014, 1):
  year.append(x)   


X=year
y=total_pop.Population_bi.values


# Plot line of agr prod worldwide
plt.plot(X, y, marker='|', color="blue", label="Population(bi)")

# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("World Population Growth (1961-2013)", fontsize=16, fontweight="bold")
plt.xlabel("Years")
plt.ylabel("Population")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/world_pop.png")
plt.show()<jupyter_output><empty_output><jupyter_text>US POPULATION DF<jupyter_code>#Setting the columns 'Area' as the new index of the original df
us_pop=df_pop.set_index('Country Name')
us_pop_filter=us_pop.loc[['United States']]
us_pop_filter['Population_bi']=us_pop_filter['Population']/1000000000
us_pop_filter.head()<jupyter_output><empty_output><jupyter_text>IN POPULATION DF<jupyter_code>in_pop_filter=us_pop.loc[['India']]
in_pop_filter['Population_bi']=in_pop_filter['Population']/1000000000
in_pop_filter.head()<jupyter_output><empty_output><jupyter_text>DF FOR PLOTING US NUMBERS<jupyter_code>#Setting the columns 'Area' as the new index of the original df
new_index_df=df.set_index('Area')

#filtering by US
df_filtered_us=new_index_df.loc[['United States of America']]

#grouping by element(food/feed), Dropping the columns that are not relevant
by_element=df_filtered_us.groupby(["Element"]).sum()
df_element=by_element.drop(columns=['Area Code', 'Item Code', 'Element Code', 'latitude', 'longitude'])

#transposing the table and adding percentage columns
df_element_us= df_element.T

#adding collumns to plot
df_element_us['Total']=df_element_us['Feed']+df_element_us['Food']
df_element_us['Feed_%']=df_element_us['Feed']/df_element_us['Total']
df_element_us['Food_%']=df_element_us['Food']/df_element_us['Total']
df_element_us['Food_mi']=df_element_us['Food']/1000000
df_element_us['Feed_mi']=df_element_us['Feed']/1000000
year=[]
for x in range(1961, 2014, 1):
  year.append(x)   
df_element_us['Year']=year

df_us=pd.DataFrame(df_element_us)
df_us.head()

us_x1=df_us['Feed_%']
us_x2=df_us['Food_%']
# Plot our line that will be used to track Food for us
plt.plot(year, us_x1, color="green", label="Feed")

# Plot our line that will be used to track a wrestler's losses over the years
plt.plot(year, us_x2, color="blue", label="Food")

# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("Feed vs Food - United States")
plt.xlabel("Years")
plt.ylabel("Percentage of production")

# Print our chart to the screen
plt.savefig("Figures/us_ratio.png")
plt.show()<jupyter_output><empty_output><jupyter_text>DF FOR PLOTING INDIA NUMBERS<jupyter_code>#filtering by India,
df_filtered1=new_index_df.loc[['India']]

#grouping by element
by_element_in=df_filtered1.groupby(["Element"]).sum()

#...Dropping the columns that are not relevant
df_element1=by_element_in.drop(columns=['Area Code', 'Item Code', 'Element Code', 'latitude', 'longitude'])

#transposing the table
df_element_in= df_element1.T

#adding relevant columns to plot.
df_element_in['Total']=df_element_in['Feed']+df_element_in['Food']
df_element_in['Feed_%']=df_element_in['Feed']/df_element_in['Total']
df_element_in['Food_%']=df_element_in['Food']/df_element_in['Total']
df_element_in['Year']=year
df_element_in['Total_bil_ton']=(df_element_in['Total']*1000)/1000000000
df_element_in['Food_mi']=df_element_in['Food']/1000000
df_element_in['Feed_mi']=df_element_in['Feed']/1000000

df_in=pd.DataFrame(df_element_in)
df_in.head()

in_x1=df_element_in['Feed_%']
in_x2=df_element_in['Food_%']
# Plot our line that will be used to track Food for us
plt.plot(year, in_x1, color="green", label="Feed")

# Plot our line that will be used to track a wrestler's losses over the years
plt.plot(year, in_x2, color="blue", label="Food")

# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("Feed vs Food - India")
plt.xlabel("Years")
plt.ylabel("Percentage of production")

# Print our chart to the screen
plt.savefig("Figures/in_ratio.png")
plt.show()<jupyter_output><empty_output><jupyter_text>DF FOR PLOTING OF GLOBAL DATA<jupyter_code>#grouping by element(food/feed), Dropping the columns that are not relevant
by_element2=new_index_df.groupby(["Element"]).sum()
df_element2=by_element2.drop(columns=['Area Code', 'Item Code', 'Element Code', 'latitude', 'longitude'])
#transposing the table and adding percentage columns
df_element_gl= df_element2.T
#adding collumns to plot
df_element_gl['Total']=df_element_gl['Feed']+df_element_gl['Food']
df_element_gl['Feed_percent']=df_element_gl['Feed']/df_element_gl['Total']
df_element_gl['Food_percent']=df_element_gl['Food']/df_element_gl['Total']
year=[]
for x in range(1961, 2014, 1):
  year.append(x)   
df_element_gl['Year']=year

df_element_gl['Total_bil_ton']=(df_element_gl['Total']*1000)/1000000000
df_element_gl['Food_mi']=df_element_gl['Food']/1000000
df_element_gl['Total_bil_ton']=(df_element_gl['Total']*1000)/1000000000
df_element_gl['Feed_mi']=df_element_gl['Feed']/1000000

df_element_gl.head()
df_global=pd.DataFrame(df_element_gl)
df_global.head()

gl_x1=df_global['Feed_percent']
gl_x2=df_global['Food_percent']
# Plot our line that will be used to track Food for us
plt.plot(year, gl_x1, color="green", label="Feed")

# Plot our line that will be used to track a wrestler's losses over the years
plt.plot(year, gl_x2, color="blue", label="Food")

# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("Feed vs Food - Global")
plt.xlabel("Years")
plt.ylabel("Percentage of production")

# Print our chart to the screen
plt.savefig("Figures/feed_food_ratio.png")
plt.show()
X=year
y=df_element_gl.Total_bil_ton.values


# Plot line of agr prod worldwide
plt.plot(X, y, marker='|', color="green", label="Agr. Production(bill/tons)")

# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("World Agricultural Production(1961-2013)", fontsize=16, fontweight="bold")
plt.xlabel("Years")
plt.ylabel("Production")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/world_agr.png")
plt.show()
gl_x2=df_global['Feed']

# Plot our line that will be used to track a wrestler's losses over the years
plt.plot(year, gl_x2, color="blue", label="Food")

# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("Feed vs Food - Global")
plt.xlabel("Years")
plt.ylabel("Percentage of production")

# Print our chart to the screen
plt.show()<jupyter_output><empty_output><jupyter_text>Applying linear regression model to Food x Year<jupyter_code>gl_food=df_global[['Food', 'Year']].copy()
gl_food.head()

X=gl_food['Year']
y=gl_food['Food']
plt.scatter(X, y)

X = gl_food.Year.values.reshape(-1, 1)
y = gl_food.Food.values.reshape(-1, 1)

from sklearn.linear_model import LinearRegression
model = LinearRegression()
model
model.fit(X, y)
print(model)

# Print the coefficient and the intercept for the model

print('Weight coefficients: ', model.coef_)
print('y-axis intercept: ', model.intercept_)<jupyter_output>Weight coefficients:  [[139831.41017578]]
y-axis intercept:  [-2.72347364e+08]<jupyter_text>Y= 0.81X - 358276.55<jupyter_code>x_min = np.array([[X.min()]])
x_max = np.array([[X.max()]])
print(f"Min X Value: {x_min}")
print(f"Max X Value: {x_max}")


# Calculate the y_min and y_max using model.predict and x_min and x_max
y_min = model.predict(x_min)
y_max = model.predict(x_max)

# Plot X and y using plt.scatter
# Plot the model fit line using [x_min[0], x_max[0]], [y_min[0], y_max[0]]
plt.scatter(X, y, c='blue')
plt.plot([x_min[0], x_max[0]], [y_min[0], y_max[0]], c='red')

food_predicted=[]
for x in range(2014, 2022, 1):
    y=(float(model.coef_)*(x))+(float(model.intercept_))
    food_predicted.append(y)   

print(food_predicted)<jupyter_output>[9273096.433236599, 9412927.84341234, 9552759.25358814, 9692590.66376394, 9832422.073939681, 9972253.484115481, 10112084.894291222, 10251916.304467022]<jupyter_text>Applying linear regression model to Feed x Year<jupyter_code>gl_feed=df_global[[ 'Feed', 'Year']].copy()
gl_feed.head()

X=gl_feed['Year']
y=gl_feed['Feed']
sns.regplot(X,y)

#plt.scatter(X, y)

X = gl_feed.Year.values.reshape(-1, 1)
y = gl_feed.Feed.values.reshape(-1, 1)

from sklearn.linear_model import LinearRegression
model = LinearRegression()
model

model.fit(X, y)
print(model)

# Print the coefficient and the intercept for the model
print('Weight coefficients: ', model.coef_)
print('y-axis intercept: ', model.intercept_)<jupyter_output>Weight coefficients:  [[31049.74600871]]
y-axis intercept:  [-60047756.43251089]<jupyter_text>Y= 0.18X + 358276<jupyter_code>#using model to make predictions
predictions = model.predict(X)
print(f"True output: {y[0]}")
print(f"Predicted output: {predictions[0]}")
print(f"Prediction Error: {predictions[0]-y[0]}")

#"Actual": y, "Error": predictions - y})[["Predicted", "Actual", "Error"]]
table=pd.DataFrame(predictions)
table["Actual"]=y
table["Eroor"]=predictions-y
table.rename(columns={0: "Prediction"}, inplace=True)
table.head()<jupyter_output><empty_output><jupyter_text>PLOTING TOTAL CROP PRODUCTION AGAINST POPULATION<jupyter_code>X=total_pop.Population_bi.values
y=df_element_gl.Total_bil_ton.values


# Plot line of agr prod worldwide
plt.plot(X, y, marker='|', color="red")

# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("World Population X Agriculture (1961-2013)", fontsize=16, fontweight="bold")
plt.xlabel("Population")
plt.ylabel("Agriculture")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/world_pop_agr.png")
plt.show()<jupyter_output>No handles with labels found to put in legend.<jupyter_text>random forest regression / decision tree<jupyter_code>X=total_pop.Population_bi.values.reshape(-1, 1)
y=df_global.Total_bil_ton.values.reshape(-1, 1)
X.shape
y.shape
# Import dependencies
from sklearn.datasets import make_regression
from sklearn.linear_model import LinearRegression

# Create a linear model
model = LinearRegression()

# Fit (Train) our model to the data
model.fit(X, y)
from sklearn.metrics import mean_squared_error, r2_score

# Use our model to predict a value
predicted = model.predict(X)

# Score the prediction with mse and r2
mse = mean_squared_error(y, predicted)
r2 = r2_score(y, predicted)

print(f"Mean Squared Error (MSE): {mse}")
print(f"R-squared (R2 ): {r2}")
model.score(X, y)
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
model.fit(X_train, y_train)
model.score(X_train, y_train)
# Print the coefficient and the intercept for the model

print('Weight coefficients: ', model.coef_)
print('y-axis intercept: ', model.intercept_)
model.score(X_test, y_test)
# Print the coefficient and the intercept for the model

print('Weight coefficients: ', model.coef_)
print('y-axis intercept: ', model.intercept_)
pred = model.predict(X)
print(f"True output: {y[0]}")
print(f"Predicted output: {predictions[0]}")
print(f"Prediction Error: {predictions[0]-y[0]}")
table=pd.DataFrame(predictions)
table["Actual"]=y
table["Error"]=predictions-y
table.rename(columns={0: "Prediction"}, inplace=True)
table.head()
                
table["Error"].mean()
# Ploting model fitting
x_min= X_train.min()
x_max = X_train.max()
y_min_actual = y.min()
y_max_actual = y.max()
y_min = -3.69657832 + 2.1346904 * x_min
y_max = -3.69657832 + 2.13469044 * x_max
print(f"Actual Min Value: {y_min_actual}")
print(f"Calculated Min Value: {y_min}")
print(f"Actual Max Value: {y_max_actual}")
print(f"Calculated Max Value: {y_max}")
plt.scatter(X, y, marker='|' , c='red', label="Actual")
plt.plot([x_min, x_max], [y_min, y_max], marker='|' , c='grey', label="Prediction")


# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("World Population X Agriculture(Model Fitting)", fontsize=16, fontweight="bold")
plt.xlabel("Population")
plt.ylabel("Agriculture")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/world_pop_agr_precision.png")
plt.show()<jupyter_output><empty_output><jupyter_text>======Ploting prediction 1960 to 2040========<jupyter_code>X_new=[7.295290765, 7.379797139, 7.464022049, 7.547858925, 7.631091248, 7.713468100, 7.794798739, 7.874965825, 7.953952567, 8.031800429, 8.108605388, 8.184437460, 8.259276737, 8.333087316, 8.405863295, 8.477660693, 8.548487400, 8.618349489, 8.687227850, 8.755083431, 8.821562661, 8.887524213, 8.952048940, 9.015437653, 9.138828468, 9.198847240]
X_fut=pd.DataFrame(X_new)
X_fut.shape
pred = model.predict(X_fut)
x_min_pred= X_fut.min()
x_max_pred = X_fut.max()
y_min_pred = -3.69657832 + 2.1346904 * x_min_pred
y_max_pred = -3.69657832 + 2.13469044 * x_max_pred
plt.scatter(X, y, marker='|' , c='red', label="Actual")
plt.plot(X_fut, pred, marker='|' , c='grey', label="Prediction")



# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("World Agriculture Prediction(1960 to 2040)", fontsize=16, fontweight="bold")
plt.xlabel("Population")
plt.ylabel("Agriculture")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/world_pop_agr_predict.png")
plt.show()<jupyter_output><empty_output><jupyter_text>FOOD AND POPULATION<jupyter_code>X=total_pop.Population_bi.values
y=df_global.Food_mi.values


# Plot line of agr prod worldwide
plt.plot(X, y, marker='|', color="blue")

# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("World Population X FOOD (1961-2013)", fontsize=16, fontweight="bold")
plt.xlabel("Population")
plt.ylabel("FOOD(mi/tons)")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/FOODVSPOP.png")
plt.show()
X=total_pop.Population_bi.values.reshape(-1, 1)
y=df_global.Food_mi.values.reshape(-1, 1)
# Import dependencies
from sklearn.datasets import make_regression
from sklearn.linear_model import LinearRegression

# Create a linear model
model = LinearRegression()

# Fit (Train) our model to the data
model.fit(X, y)
from sklearn.metrics import mean_squared_error, r2_score

# Use our model to predict a value
predicted = model.predict(X)

# Score the prediction with mse and r2
mse = mean_squared_error(y, predicted)
r2 = r2_score(y, predicted)

print(f"Mean Squared Error (MSE): {mse}")
print(f"R-squared (R2 ): {r2}")
model.score(X, y)
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
model.fit(X_train, y_train)
print('Weight coefficients: ', model.coef_)
print('y-axis intercept: ', model.intercept_)
predictions = model.predict(X)
print(f"True output: {y[0]}")
print(f"Predicted output: {predictions[0]}")
print(f"Prediction Error: {predictions[0]-y[0]}")
table=pd.DataFrame(predictions)
table["Actual"]=y
table["Error"]=predictions-y
table.rename(columns={0: "Prediction"}, inplace=True)
table.head()
              
x_min= X_train.min()
x_max = X_train.max()
y_min_actual = y.min()
y_max_actual = y.max()
y_min = -3.38558816 + 1.74980349 * x_min
y_max = -3.38558816 + 1.74980349 * x_max
print(f"Actual Min Value: {y_min_actual}")
print(f"Calculated Min Value: {y_min}")
print(f"Actual Max Value: {y_max_actual}")
print(f"Calculated Max Value: {y_max}")
plt.scatter(X, y, marker='|' , c='red', label="Actual")
plt.plot([x_min, x_max], [y_min, y_max], marker='|' , c='grey', label="Prediction")


# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("World FOOD X Population(Fitting)", fontsize=16, fontweight="bold")
plt.xlabel("Population")
plt.ylabel("FOOD")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/world_food_fit.png")
plt.show()
X_new=[7.295290765, 7.379797139, 7.464022049, 7.547858925, 7.631091248, 7.713468100, 7.794798739, 7.874965825, 7.953952567, 8.031800429, 8.108605388, 8.184437460, 8.259276737, 8.333087316, 8.405863295, 8.477660693, 8.548487400, 8.618349489, 8.687227850, 8.755083431, 8.821562661, 8.887524213, 8.952048940, 9.015437653, 9.138828468, 9.198847240]
X_fut=pd.DataFrame(X_new)
pred = model.predict(X_fut)
x_min_pred= X_fut.min()
x_max_pred = X_fut.max()
plt.scatter(X, y, marker='|' , c='red', label="Actual")
plt.plot(X_fut, pred, marker='|' , c='grey', label="Prediction")



# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("World FOOD prediction(1960 to 2040)", fontsize=16, fontweight="bold")
plt.xlabel("Population")
plt.ylabel("FOOD")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/world_FOODpredict.png")
plt.show()<jupyter_output><empty_output><jupyter_text>FEED AND POPULATION<jupyter_code>X=total_pop.Population_bi.values
y=df_global.Feed_mi.values


# Plot line of agr prod worldwide
plt.plot(X, y, marker='|', color="green")

# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("World Population X FEED (1961-2013)", fontsize=16, fontweight="bold")
plt.xlabel("Population")
plt.ylabel("FEED(mi/tons)")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/FEEDVSPOP.png")
plt.show()
X=total_pop.Population_bi.values.reshape(-1, 1)
y=df_global.Feed_mi.values.reshape(-1, 1)
# Import dependencies
from sklearn.datasets import make_regression
from sklearn.linear_model import LinearRegression

# Create a linear model
model = LinearRegression()

# Fit (Train) our model to the data
model.fit(X, y)
from sklearn.metrics import mean_squared_error, r2_score

# Use our model to predict a value
predicted = model.predict(X)

# Score the prediction with mse and r2
mse = mean_squared_error(y, predicted)
r2 = r2_score(y, predicted)

print(f"Mean Squared Error (MSE): {mse}")
print(f"R-squared (R2 ): {r2}")
model.score(X, y)
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
model.fit(X_train, y_train)
print('Weight coefficients: ', model.coef_)
print('y-axis intercept: ', model.intercept_)
predictions = model.predict(X)
print(f"True output: {y[0]}")
print(f"Predicted output: {predictions[0]}")
print(f"Prediction Error: {predictions[0]-y[0]}")
table=pd.DataFrame(predictions)
table["Actual"]=y
table["Error"]=predictions-y
table.rename(columns={0: "Prediction"}, inplace=True)
table.head()
              
x_min= X_train.min()
x_max = X_train.max()
y_min_actual = y.min()
y_max_actual = y.max()
y_min = -0.31099015 + 0.38488691 * x_min
y_max = -0.31099015 + 0.38488691 * x_max
print(f"Actual Min Value: {y_min_actual}")
print(f"Calculated Min Value: {y_min}")
print(f"Actual Max Value: {y_max_actual}")
print(f"Calculated Max Value: {y_max}")
plt.scatter(X, y, marker='|' , c='red', label="Actual")
plt.plot([x_min, x_max], [y_min, y_max], marker='|' , c='grey', label="Prediction")


# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("World FEED X Population(Model Fitting)", fontsize=16, fontweight="bold")
plt.xlabel("Population")
plt.ylabel("FEED")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/world_feed_fit.png")
plt.show()
X_new=[7.295290765, 7.379797139, 7.464022049, 7.547858925, 7.631091248, 7.713468100, 7.794798739, 7.874965825, 7.953952567, 8.031800429, 8.108605388, 8.184437460, 8.259276737, 8.333087316, 8.405863295, 8.477660693, 8.548487400, 8.618349489, 8.687227850, 8.755083431, 8.821562661, 8.887524213, 8.952048940, 9.015437653, 9.138828468, 9.198847240]
X_fut=pd.DataFrame(X_new)
pred = model.predict(X_fut)
x_min_pred= X_fut.min()
x_max_pred = X_fut.max()
plt.scatter(X, y, marker='|' , c='red', label="Actual")
plt.plot(X_fut, pred, marker='|' , c='grey', label="Prediction")



# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("World FEED prediction(1960 to 2040)", fontsize=16, fontweight="bold")
plt.xlabel("Population")
plt.ylabel("FEED")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/world_FEEDpredict.png")
plt.show()<jupyter_output><empty_output><jupyter_text>US FOOD X POP<jupyter_code>X=us_pop_filter.Population_bi.values
y=df_us.Food_mi.values
X=us_pop_filter.Population_bi.values.reshape(-1, 1)
y=df_us.Food_mi.values.reshape(-1, 1)
# Import dependencies
from sklearn.datasets import make_regression
from sklearn.linear_model import LinearRegression

# Create a linear model
model = LinearRegression()

# Fit (Train) our model to the data
model.fit(X, y)
from sklearn.metrics import mean_squared_error, r2_score

# Use our model to predict a value
predicted = model.predict(X)

# Score the prediction with mse and r2
mse = mean_squared_error(y, predicted)
r2 = r2_score(y, predicted)

print(f"Mean Squared Error (MSE): {mse}")
print(f"R-squared (R2 ): {r2}")
model.score(X, y)
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
model.fit(X_train, y_train)
print('Weight coefficients: ', model.coef_)
print('y-axis intercept: ', model.intercept_)
predictions = model.predict(X)
print(f"True output: {y[0]}")
print(f"Predicted output: {predictions[0]}")
print(f"Prediction Error: {predictions[0]-y[0]}")
table=pd.DataFrame(predictions)
table["Actual"]=y
table["Error"]=predictions-y
table.rename(columns={0: "Prediction"}, inplace=True)
table.head()
              
x_min= X_train.min()
x_max = X_train.max()
y_min_actual = y.min()
y_max_actual = y.max()
y_min = -0.16272045 + 2.66455219 * x_min
y_max = -0.16272045 + 2.66455219 * x_max
plt.scatter(X, y, marker='|' , c='red', label="Actual")
plt.plot([x_min, x_max], [y_min, y_max], marker='|' , c='grey', label="Prediction")


# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("US FOOD X Population(Model Fitting)", fontsize=16, fontweight="bold")
plt.xlabel("US Population")
plt.ylabel("FOOD")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/us_food_fit.png")
plt.show()<jupyter_output><empty_output><jupyter_text>US FEED X POP<jupyter_code>X=us_pop_filter.Population_bi.values
y=df_us.Feed_mi.values
X=us_pop_filter.Population_bi.values.reshape(-1, 1)
y=df_us.Feed_mi.values.reshape(-1, 1)
# Import dependencies
from sklearn.datasets import make_regression
from sklearn.linear_model import LinearRegression

# Create a linear model
model = LinearRegression()

# Fit (Train) our model to the data
model.fit(X, y)
from sklearn.metrics import mean_squared_error, r2_score

# Use our model to predict a value
predicted = model.predict(X)

# Score the prediction with mse and r2
mse = mean_squared_error(y, predicted)
r2 = r2_score(y, predicted)

print(f"Mean Squared Error (MSE): {mse}")
print(f"R-squared (R2 ): {r2}")
model.score(X, y)
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
model.fit(X_train, y_train)
print('Weight coefficients: ', model.coef_)
print('y-axis intercept: ', model.intercept_)
predictions = model.predict(X)
x_min= X_train.min()
x_max = X_train.max()
y_min_actual = y.min()
y_max_actual = y.max()
y_min = 0.138314995 + 0.63224198 * x_min
y_max = 0.13831499 + 0.63224198 * x_max
plt.scatter(X, y, marker='|' , c='red', label="Actual")
plt.plot([x_min, x_max], [y_min, y_max], marker='|' , c='grey', label="Prediction")


# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("US FEED X Population(Model Fitting)", fontsize=16, fontweight="bold")
plt.xlabel("US Population")
plt.ylabel("FEED")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/us_feed_fit.png")
plt.show()<jupyter_output><empty_output><jupyter_text>IN FEED VS POP<jupyter_code>X=in_pop_filter.Population_bi.values
y=df_in.Feed_mi.values
X=in_pop_filter.Population_bi.values.reshape(-1, 1)
y=df_in.Feed_mi.values.reshape(-1, 1)
# Import dependencies
from sklearn.datasets import make_regression
from sklearn.linear_model import LinearRegression

# Create a linear model
model = LinearRegression()

# Fit (Train) our model to the data
model.fit(X, y)
from sklearn.metrics import mean_squared_error, r2_score

# Use our model to predict a value
predicted = model.predict(X)

# Score the prediction with mse and r2
mse = mean_squared_error(y, predicted)
r2 = r2_score(y, predicted)

print(f"Mean Squared Error (MSE): {mse}")
print(f"R-squared (R2 ): {r2}")
model.score(X, y)
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
model.fit(X_train, y_train)
print('Weight coefficients: ', model.coef_)
print('y-axis intercept: ', model.intercept_)
predictions = model.predict(X)
x_min= X_train.min()
x_max = X_train.max()
y_min_actual = y.min()
y_max_actual = y.max()
y_min = -0.04126713 + 0.09330949 * x_min
y_max = -0.04126713 + 0.09330949 * x_max
plt.scatter(X, y, marker='|' , c='red', label="Actual")
plt.plot([x_min, x_max], [y_min, y_max], marker='|' , c='grey', label="Prediction")


# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("IN FEED X Population(Model Fitting)", fontsize=16, fontweight="bold")
plt.xlabel("IN Population")
plt.ylabel("FEED")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/in_feed_fit.png")
plt.show()<jupyter_output><empty_output><jupyter_text>IN FOOD VS POP<jupyter_code>X=in_pop_filter.Population_bi.values
y=df_in.Food_mi.values
X=in_pop_filter.Population_bi.values.reshape(-1, 1)
y=df_in.Food_mi.values.reshape(-1, 1)
# Import dependencies
from sklearn.datasets import make_regression
from sklearn.linear_model import LinearRegression

# Create a linear model
model = LinearRegression()

# Fit (Train) our model to the data
model.fit(X, y)
from sklearn.metrics import mean_squared_error, r2_score

# Use our model to predict a value
predicted = model.predict(X)

# Score the prediction with mse and r2
mse = mean_squared_error(y, predicted)
r2 = r2_score(y, predicted)

print(f"Mean Squared Error (MSE): {mse}")
print(f"R-squared (R2 ): {r2}")
model.score(X, y)
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(X, y, random_state=42)
model.fit(X_train, y_train)
print('Weight coefficients: ', model.coef_)
print('y-axis intercept: ', model.intercept_)
predictions = model.predict(X)
x_min= X_train.min()
x_max = X_train.max()
y_min_actual = y.min()
y_max_actual = y.max()
y_min = -0.25704929 + 1.08910926 * x_min
y_max = -0.25704929 + 1.08910926 * x_max
plt.scatter(X, y, marker='|' , c='red', label="Actual")
plt.plot([x_min, x_max], [y_min, y_max], marker='|' , c='grey', label="Prediction")


# Place a legend on the chart in what matplotlib believes to be the "best" location
plt.legend(loc="best")

plt.title("IN FOOD X Population(Model Fitting)", fontsize=16, fontweight="bold")
plt.xlabel("IN Population")
plt.ylabel("FOOD")
plt.grid()

# Print our chart to the screen
plt.savefig("Figures/in_food_fit.png")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1474749597254325, 'fraction_numerical': 0.05915108216011767, 'mean_word_length': 6.676075268817204, 'pattern_counts': {'":': 2, '<': 77, '<?xml version=': 0, '>': 77, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 6, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '20', 'dir': 'jupyter-structured-clean-dedup', 'id': '43717', 'license': 'MIT', 'n_tokens_mistral': 12225, 'n_tokens_neox': 10806, 'n_words': 2419, 'path': 'FeedTheWorld/ml_agriculture.ipynb', 'repo_name': 'gustavosgarofalo/Data_Analytics_Projects', 'size': '28554'}