{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load in \n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the \"../input/\" directory.\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# Any results you write to the current directory are saved as output.","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"data = pd.read_csv('/kaggle/input/ds4g-environmental-insights-explorer/eie_data/gppd/gppd_120_pr.csv')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.columns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = data.drop(['system:index','country','wepp_id','url','.geo','country_long','geolocation_source','name','gppd_idnr'],axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.tail()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data['total_generation'] = data['generation_gwh_2013'] + data['generation_gwh_2014'] + data['generation_gwh_2015'] + data['generation_gwh_2016'] + data['generation_gwh_2017'] ","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = data.drop(['generation_gwh_2013','generation_gwh_2014','generation_gwh_2015','generation_gwh_2016','generation_gwh_2017'],axis = 1)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"import seaborn as sns","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = sns.barplot(x=\"primary_fuel\", y=\"estimated_generation_gwh\", data=data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = sns.barplot(x=\"estimated_generation_gwh\", y=\"source\", data=data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = sns.scatterplot(x=\"commissioning_year\", y=\"estimated_generation_gwh\", data=data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = sns.scatterplot(x=\"capacity_mw\", y=\"estimated_generation_gwh\", data=data)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Capasity and commissioning year is not related with estimated generation gwh"},{"metadata":{"trusted":true},"cell_type":"code","source":"the_mean = data.groupby('primary_fuel').mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.linear_model import LinearRegression\nimport matplotlib.pyplot as plot","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = the_mean['capacity_mw']\ny = the_mean['estimated_generation_gwh']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"reg = LinearRegression()\nreg = reg.fit(X.values.reshape(-1,1),y)\npredictions = reg.predict(X.values.reshape(-1,1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot.scatter(X, y, color = 'red')\nplot.plot(X, predictions, color = 'blue')\nplot.title('RelationShip between Capasity and expected gwh')\nplot.xlabel('Capasity')\nplot.ylabel('Expected gwh')\nplot.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import r2_score\nr2_score(y,predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"the_mean = the_mean.sort_values('capacity_mw')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"the_mean['weights'] = list(the_mean['estimated_generation_gwh'] / the_mean['capacity_mw'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"the_mean['weights'] = the_mean['weights'] / the_mean['weights'].sum() * 1000","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"the_mean['the_adjusted_ratio'] = the_mean['weights'] * the_mean['capacity_mw']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"the_mean","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"X = the_mean['the_adjusted_ratio']\ny = the_mean['estimated_generation_gwh']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"reg = LinearRegression()\nreg = reg.fit(X.values.reshape(-1,1),y)\npredictions = reg.predict(X.values.reshape(-1,1))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"plot.scatter(X, y, color = 'red')\nplot.plot(X, predictions, color = 'blue')\nplot.title('RelationShip between Capasity and expected gwh')\nplot.xlabel('Capasity')\nplot.ylabel('Expected gwh')\nplot.show()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import r2_score\nr2_score(y,predictions)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dictionary_of_coefficients = dict(zip(the_mean.index, the_mean['the_adjusted_ratio']))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dictionary_of_coefficients","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"We have coefficents for every type of energy power plant. We will use them for calculating every expected gwh as the formula:\n\nexp_gwh = coefficient_of_fuel * capasity"},{"metadata":{"trusted":true},"cell_type":"code","source":"data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for index,row in data.iterrows():\n    for name in data.primary_fuel.unique():\n        if row['primary_fuel'] == name:\n            data.loc[index,'predicted_gwh'] = (dictionary_of_coefficients[name] * row['capacity_mw'])\n            \nfrom sklearn.metrics import mean_squared_error\nmean_squared_error(data['estimated_generation_gwh'], data['predicted_gwh'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"ax = sns.scatterplot(x=\"estimated_generation_gwh\", y=\"predicted_gwh\", data=data)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import r2_score\nr2_score(data['estimated_generation_gwh'], data['predicted_gwh'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"In the end with testing data, we optimize the coefficients and the final coefficients for fuel source"},{"metadata":{"trusted":true},"cell_type":"code","source":"dictionary_of_coefficients","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"the_mean = data.groupby('primary_fuel').mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"the_mean['new_ratios'] = the_mean['predicted_gwh'] / the_mean['estimated_generation_gwh']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"the_mean","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dictionary_of_coefficients","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for name in the_mean.index:\n    dictionary_of_coefficients[name] =  dictionary_of_coefficients[name] / the_mean.loc[name,'new_ratios']","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"dictionary_of_coefficients","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for index,row in data.iterrows():\n    for name in data.primary_fuel.unique():\n        if row['primary_fuel'] == name:\n            data.loc[index,'predicted_gwh'] = (dictionary_of_coefficients[name] * row['capacity_mw'])\n            \nfrom sklearn.metrics import mean_squared_error\nmean_squared_error(data['estimated_generation_gwh'], data['predicted_gwh'])","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"from sklearn.metrics import r2_score\nr2_score(data['estimated_generation_gwh'], data['predicted_gwh'])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"I regulate the coefficients for fuel with repetative methodolgy. After that the error and r_square is very significant. Thus, we can totally predict the real expected gwh from capasity and type of fuel. Here, I create a methodology to extract information from two features. This methodology can be used for another related features and machine learning stages could be improved\n\nexp_gwh = coefficient_of_fuel * capasity"},{"metadata":{},"cell_type":"markdown","source":"# Global Power Plants Annual Consumption"},{"metadata":{"trusted":true},"cell_type":"code","source":"data","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"","_uuid":"","trusted":true},"cell_type":"code","source":"import pandas as pd\nglobal_power_plant_database = pd.read_csv(\"../input/global-power-plant-database/global_power_plant_database.csv\")","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"emmission_annual = pd.read_excel('/kaggle/input/emission-annual/emission_annual.xls')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"emmission_mean = emmission_annual.groupby('Energy Source').mean()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = pd.concat([global_power_plant_database[['capacity_mw','fuel1']],data])","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we have all ht plants for capacity and have the coefficients for their fuel type. "},{"metadata":{"trusted":true},"cell_type":"code","source":"for name in dictionary_of_coefficients:\n    data.loc[data['fuel1'] == name,'predicted_gwh'] = (dictionary_of_coefficients[name] * data.loc[data['fuel1'] == name,'capacity_mw'])\n    ","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"Now we have all the predicted annual gwh for all power plants. We can transfor these to the emissions."},{"metadata":{},"cell_type":"markdown","source":"https://blueskymodel.org/kilowatt-hour we can use this data for calculations of average co2 emission as gram"},{"metadata":{"trusted":true},"cell_type":"code","source":"dictionary_of_coefficients","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"average_co2_emissions = {'Coal':909,'Hydro':4,'Solar':105,'Wind':13,'Gas':465,'Oil':821}","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"for name in dictionary_of_coefficients:\n    data.loc[data['fuel1'] == name,'co2_emissions'] = (average_co2_emissions[name] * data.loc[data['fuel1'] == name,'predicted_gwh']) * 1000000","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.head(30)","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"# Total Esmitated Co2 Emission In the World"},{"metadata":{"trusted":true},"cell_type":"code","source":"data['co2_emissions'].plot(figsize = (15,10))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data.sort_values('co2_emissions')['co2_emissions'].plot(use_index=False,figsize=(15,10))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data['co2_emissions'].sum() / 1000000","execution_count":null,"outputs":[]},{"metadata":{},"cell_type":"markdown","source":"1683217333372.0256 ton co2 emmission is calculated in the world per year"},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat":4,"nbformat_minor":1}