zajecia3 i zajecia4

This commit is contained in:
kubapok 2024-12-07 11:54:47 +01:00
parent bcc0d7b698
commit 5ceed1d6b5
11 changed files with 6791 additions and 2724 deletions

View File

@ -30,13 +30,14 @@ Do nauki można wykorzystać wiele tutoriali internetowych python (w wersji pyth
- Zajęcia 2 - Wprowadzenie do python 2/2
- Zajęcia 3 - pandas
- Zajęcia 4 - numpy
- Zajęcia 5 - scikit-learn
- Zajęcia 6 - przetwarzanie tekstu w python
- Zajęcia 7 - przetwarzanie obrazów w python
- Zajęcia 8 - zajęcia z analizy wizualizacji danych
- Zajęcia 5 - scikit-learn 1
- Zajęcia 6 - scikit-learn 2
- Zajęcia 7 - przetwarzanie tekstu w python
- Zajęcia 8 - przetwarzanie obrazów w python
- Zajęcia 9 - zajęcia z analizy wizualizacji danych
- Zajęcia 10 - zajęcia z analizy wizualizacji danych
- Zajęcia 11 - Zaliczenie
- Zajęcia 11 - zajęcia z analizy wizualizacji danych
- Zaliczenie - Zaliczenie przedmiotu 8 luty 14:30-16.45
## Zaliczenie przedmiotu

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

Binary file not shown.

2280
zajecia3/1.ipynb Normal file

File diff suppressed because it is too large Load Diff

311
zajecia3/1_odpowiedzi.ipynb Normal file
View File

@ -0,0 +1,311 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "23ed41a0-7a05-493e-a640-4bfb10c42164",
"metadata": {},
"outputs": [],
"source": [
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "fa3799c5-d3a0-4967-98d4-a340d19dbfc6",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10 11 12 13 14 15 16 17 18 19 20]\n",
"(11,)\n",
"<class 'numpy.ndarray'>\n"
]
}
],
"source": [
"#Zadanie 1.1\n",
"# Tworzenie tablicy jednowymiarowej\n",
"arr = np.array([10,11,12,13,14,15,16,17,18,19,20])\n",
"print(arr)\n",
"print(arr.shape)\n",
"print(type(arr))\n"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "b6b4fa7d-7ee5-416c-8060-39057b49d77b",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[10 20]\n",
" [30 40]\n",
" [50 60]]\n",
"(3, 2)\n",
"<class 'numpy.ndarray'>\n"
]
}
],
"source": [
"# Zadanie 1.2\n",
"arr = np.array([[10, 20], [30, 40], [50, 60]])\n",
"\n",
"print(arr)\n",
"\n",
"print(arr.shape)\n",
"\n",
"print(type(arr))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "f96f774c-d6cd-440f-b6bf-a2d373404de3",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[1 2 3]\n",
" [4 5 6]\n",
" [7 8 9]]\n",
"8\n",
"[[7 8 9]]\n",
"[3 6 9]\n",
"[[5 6]\n",
" [8 9]]\n"
]
}
],
"source": [
"# Zadanie 2\n",
"# Tworzenie dwuwymiarowej tablicy\n",
"arr = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])\n",
"\n",
"print(arr)\n",
"\n",
"\n",
"print(arr[2, 1])\n",
"\n",
"print(arr[2:])\n",
"\n",
"\n",
"print(arr[:,2])\n",
"\n",
"print(arr[1:,1:])\n"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "43216855-9d5d-4d03-9512-557f4d228571",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[10 20 30 40]\n",
"int64\n",
"[10. 20. 30. 40.]\n",
"float32\n",
"['Python' 'NumPy' 'Coding']\n",
"<U6\n"
]
}
],
"source": [
"# Zadanie 3\n",
"\n",
"# Punkt 1\n",
"arr = np.array([10, 20, 30, 40])\n",
"print(arr)\n",
"print(arr.dtype)\n",
"\n",
"# Punkt 2\n",
"arr = arr.astype('float32')\n",
"print(arr)\n",
"print(arr.dtype)\n",
"\n",
"# Punkt 3\n",
"arr = np.array([\"Python\", \"NumPy\", \"Coding\"])\n",
"print(arr)\n",
"print(arr.dtype)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "0a700a92-fb6e-498d-bf2f-d5f9758d0147",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[50 2 3 4 5]\n",
"[50 2 3 4 5]\n",
"[50 2 3 4 5]\n",
"[1 2 3 4 5]\n",
"[50 2 3 4 5]\n",
"[[1 2 3 4]]\n",
"[1 2 3 4]\n"
]
}
],
"source": [
"### Zadanie 4\n",
"import copy\n",
"\n",
"\n",
"# Punkt 1 - Przypisanie do zmiennej\n",
"arr = np.array([1, 2, 3, 4, 5])\n",
"x = arr\n",
"arr[0] = 50\n",
"print(arr) # Tablica arr po zmianie\n",
"print(x) # Tablica x po zmianie\n",
"\n",
"# Punkt 2 - Kopia tablicy\n",
"arr = np.array([1, 2, 3, 4, 5])\n",
"x = arr.copy()\n",
"arr[0] = 50\n",
"print(arr) # Tablica arr po zmianie\n",
"print(x) # Tablica x po kopii\n",
"\n",
"# Punkt 3 - Głęboka kopia\n",
"arr = np.array([1, 2, 3, 4, 5])\n",
"x = copy.deepcopy(arr)\n",
"arr[0] = 50\n",
"print(arr) # Tablica arr po zmianie\n",
"\n",
"arr2 = np.array([1, 2, 3, 4], ndmin=2)\n",
"print(arr2)\n",
"\n",
"# Zmiana wymiaru na jednowymiarowy\n",
"arr_squeezed = arr2.squeeze()\n",
"print(arr_squeezed)\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "fbf7f5ee-aace-47f6-9a73-14fdf7595ff4",
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"array([[[[ 1],\n",
" [ 2]]],\n",
"\n",
"\n",
" [[[ 3],\n",
" [ 4]]],\n",
"\n",
"\n",
" [[[ 5],\n",
" [ 6]]],\n",
"\n",
"\n",
" [[[ 7],\n",
" [ 8]]],\n",
"\n",
"\n",
" [[[ 9],\n",
" [10]]]])"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"## Zadanie 5\n",
"\n",
"\n",
"arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])\n",
"\n",
"arr\n",
"\n",
"arr.reshape(5,2)\n",
"\n",
"arr.reshape(10,1)\n",
"\n",
"arr.reshape(5,-1)\n",
"\n",
"arr.reshape(5,1,2,1)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "5afcad53-ce4a-408d-bc44-f33fd7b8e276",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[53 65 77]\n",
"[13 15 17]\n",
"[15 25 35]\n",
"[[ 3 5 7]\n",
" [ 8 10 12]\n",
" [10 10 10]]\n",
"[119 135 151]\n",
"940\n"
]
}
],
"source": [
"### Zadanie 6\n",
"\n",
"\n",
"x = np.array([3, 5, 7])\n",
"y = np.array([50, 60, 70])\n",
"\n",
"print(x + y)\n",
"print(x + 10)\n",
"print(x * 5)\n",
"z = np.array([[3, 5, 7], [8, 10, 12], [10,10,10]])\n",
"print(z)\n",
"\n",
"print(x.dot(z))\n",
"print(x.dot(y))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.8 KiB

176
zajecia4/gapminder.csv Normal file
View File

@ -0,0 +1,176 @@
Country,female_BMI,male_BMI,gdp,population,under5mortality,life_expectancy,fertility
Afghanistan,21.07402,20.62058,1311.0,26528741.0,110.4,52.8,6.2
Albania,25.65726,26.44657,8644.0,2968026.0,17.9,76.8,1.76
Algeria,26.368409999999997,24.5962,12314.0,34811059.0,29.5,75.5,2.73
Angola,23.48431,22.25083,7103.0,19842251.0,192.0,56.7,6.43
Antigua and Barbuda,27.50545,25.76602,25736.0,85350.0,10.9,75.5,2.16
Argentina,27.46523,27.5017,14646.0,40381860.0,15.4,75.4,2.24
Armenia,27.1342,25.355420000000002,7383.0,2975029.0,20.0,72.3,1.4
Australia,26.87777,27.56373,41312.0,21370348.0,5.2,81.6,1.96
Austria,25.09414,26.467409999999997,43952.0,8331465.0,4.6,80.4,1.41
Azerbaijan,27.50879,25.65117,14365.0,8868713.0,43.3,69.2,1.99
Bahamas,29.13948,27.24594,24373.0,348587.0,14.5,72.2,1.89
Bahrain,28.790940000000003,27.83721,42507.0,1115777.0,9.4,77.6,2.23
Bangladesh,20.54531,20.39742,2265.0,148252473.0,55.9,68.3,2.38
Barbados,29.221690000000002,26.384390000000003,16075.0,277315.0,15.4,75.3,1.83
Belarus,26.641859999999998,26.16443,14488.0,9526453.0,7.2,70.0,1.42
Belgium,25.1446,26.75915,41641.0,10779155.0,4.7,79.6,1.82
Belize,29.81663,27.02255,8293.0,306165.0,20.1,70.7,2.91
Benin,23.74026,22.41835,1646.0,8973525.0,116.3,59.7,5.27
Bhutan,22.88243,22.8218,5663.0,694990.0,48.1,70.7,2.51
Bolivia,26.8633,24.43335,5066.0,9599916.0,52.0,71.2,3.48
Bosnia and Herzegovina,26.35874,26.611629999999998,9316.0,3839749.0,8.1,77.5,1.22
Botswana,26.09156,22.129839999999998,13858.0,1967866.0,63.8,53.2,2.86
Brazil,25.99113,25.78623,13906.0,194769696.0,18.6,73.2,1.9
Brunei,22.892310000000002,24.18179,72351.0,380786.0,9.0,76.9,2.1
Bulgaria,25.51574,26.542859999999997,15368.0,7513646.0,13.7,73.2,1.43
Burkina Faso,21.63031,21.27157,1358.0,14709011.0,130.4,58.0,6.04
Burundi,21.27927,21.50291,723.0,8821795.0,108.6,59.1,6.48
Cambodia,21.69608,20.80496,2442.0,13933660.0,51.5,66.1,3.05
Cameroon,24.9527,23.681729999999998,2571.0,19570418.0,113.8,56.6,5.17
Canada,26.698290000000004,27.4521,41468.0,33363256.0,5.8,80.8,1.68
Cape Verde,24.96136,23.515220000000003,6031.0,483824.0,28.4,70.4,2.57
Chad,21.95424,21.485689999999998,1753.0,11139740.0,168.0,54.3,6.81
Chile,27.92807,27.015420000000002,18698.0,16645940.0,8.9,78.5,1.89
China,22.91041,22.92176,7880.0,1326690636.0,18.5,73.4,1.53
Colombia,26.22529,24.94041,10489.0,44901660.0,19.7,76.2,2.43
Comoros,22.444329999999997,22.06131,1440.0,665414.0,91.2,67.1,5.05
"Congo, The Democratic Republic of the",21.6677,19.86692,607.0,61809278.0,124.5,57.5,6.45
"Congo",23.10824,21.87134,5022.0,3832771.0,72.6,58.8,5.1
Costa Rica,27.03497,26.47897,12219.0,4429506.0,10.3,79.8,1.91
Ivory Coast,23.82088,22.56469,2854.0,19261647.0,116.9,55.4,4.91
Croatia,25.17882,26.596290000000003,21873.0,4344151.0,5.9,76.2,1.43
Cuba,26.576140000000002,25.06867,17765.0,11290239.0,6.3,77.6,1.5
Cyprus,25.92587,27.41899,35828.0,1077010.0,4.2,80.0,1.49
Denmark,25.106270000000002,26.13287,45017.0,5495302.0,4.3,78.9,1.89
Djibouti,24.38177,23.38403,2502.0,809639.0,81.0,61.8,3.76
Ecuador,27.062690000000003,25.58841,9244.0,14447600.0,26.8,74.7,2.73
Egypt,30.099970000000003,26.732429999999997,9974.0,78976122.0,31.4,70.2,2.95
El Salvador,27.84092,26.36751,7450.0,6004199.0,21.6,73.7,2.32
Equatorial Guinea,24.528370000000002,23.7664,40143.0,686223.0,118.4,57.5,5.31
Eritrea,21.082320000000003,20.885089999999998,1088.0,4500638.0,60.4,60.1,5.16
Estonia,25.185979999999997,26.264459999999996,24743.0,1339941.0,5.5,74.2,1.62
Ethiopia,20.71463,20.247,931.0,83079608.0,86.9,60.0,5.19
Fiji,29.339409999999997,26.53078,7129.0,843206.0,24.0,64.9,2.74
Finland,25.58418,26.733390000000004,42122.0,5314170.0,3.3,79.6,1.85
France,24.82949,25.853289999999998,37505.0,62309529.0,4.3,81.1,1.97
Gabon,25.95121,24.0762,15800.0,1473741.0,68.0,61.7,4.28
Gambia,24.82101,21.65029,1566.0,1586749.0,87.4,65.7,5.8
Georgia,26.45014,25.54942,5900.0,4343290.0,19.3,71.8,1.79
Germany,25.73903,27.165090000000003,41199.0,80665906.0,4.4,80.0,1.37
Ghana,24.33014,22.842470000000002,2907.0,23115919.0,79.9,62.0,4.19
Greece,24.92026,26.33786,32197.0,11161755.0,4.9,80.2,1.46
Grenada,27.31948,25.179879999999997,12116.0,103934.0,13.5,70.8,2.28
Guatemala,26.84324,25.29947,6960.0,14106687.0,36.9,71.2,4.12
Guinea,22.45206,22.52449,1230.0,10427356.0,121.0,57.1,5.34
Guinea-Bissau,22.92809,21.64338,1326.0,1561293.0,127.6,53.6,5.25
Guyana,26.470190000000002,23.68465,5208.0,748096.0,41.9,65.0,2.74
Haiti,23.27785,23.66302,1600.0,9705130.0,83.3,61.0,3.5
Honduras,26.73191,25.10872,4391.0,7259470.0,26.5,71.8,3.27
"Hong Kong",23.71046,25.057470000000002,46635.0,6910384.0,3.06,82.49,1.04
Hungary,25.97839,27.115679999999998,23334.0,10050699.0,7.2,73.9,1.33
Iceland,26.02599,27.206870000000002,42294.0,310033.0,2.7,82.4,2.12
India,21.31478,20.95956,3901.0,1197070109.0,65.6,64.7,2.64
Indonesia,22.986929999999997,21.85576,7856.0,235360765.0,36.2,69.4,2.48
Iran,27.236079999999998,25.310029999999998,15955.0,72530693.0,21.4,73.1,1.88
Iraq,28.411170000000002,26.71017,11616.0,29163327.0,38.3,66.6,4.34
Ireland,26.62176,27.65325,47713.0,4480145.0,4.5,80.1,2.0
Israel,27.301920000000003,27.13151,28562.0,7093808.0,4.9,80.6,2.92
Italy,24.79289,26.4802,37475.0,59319234.0,4.1,81.5,1.39
Jamaica,27.22601,24.00421,8951.0,2717344.0,18.9,75.1,2.39
Japan,21.87088,23.50004,34800.0,127317900.0,3.4,82.5,1.34
Jordan,29.218009999999996,27.47362,10897.0,6010035.0,22.1,76.9,3.59
Kazakhstan,26.65065,26.290779999999998,18797.0,15915966.0,25.9,67.1,2.51
Kenya,23.06181,21.592579999999998,2358.0,38244442.0,71.0,60.8,4.76
Kiribati,31.30769,29.2384,1803.0,98437.0,64.5,61.5,3.13
Kuwait,31.161859999999997,29.172109999999996,91966.0,2705290.0,11.3,77.3,2.68
Latvia,25.615129999999997,26.45693,20977.0,2144215.0,10.5,72.4,1.5
Lebanon,27.70471,27.20117,14158.0,4109389.0,11.3,77.8,1.57
Lesotho,26.780520000000003,21.90157,2041.0,1972194.0,114.2,44.5,3.34
Liberia,23.21679,21.89537,588.0,3672782.0,100.9,59.9,5.19
Libya,29.19874,26.54164,29853.0,6123022.0,18.8,75.6,2.64
Lithuania,26.01424,26.86102,23223.0,3219802.0,8.2,72.1,1.42
Luxembourg,26.09326,27.434040000000003,95001.0,485079.0,2.8,81.0,1.63
Macao,24.895039999999998,25.713820000000002,80191.0,507274.0,6.72,79.32,0.94
Macedonia,25.37646,26.34473,10872.0,2055266.0,11.8,74.5,1.47
Madagascar,20.73501,21.403470000000002,1528.0,19926798.0,66.7,62.2,4.79
Malawi,22.91455,22.034679999999998,674.0,13904671.0,101.1,52.4,5.78
Malaysia,25.448320000000002,24.73069,19968.0,27197419.0,8.0,74.5,2.05
Maldives,26.4132,23.219910000000002,12029.0,321026.0,16.0,78.5,2.38
Mali,23.07655,21.78881,1602.0,14223403.0,148.3,58.5,6.82
Malta,27.04993,27.683609999999998,27872.0,406392.0,6.6,80.7,1.38
Mauritania,26.26476,22.62295,3356.0,3414552.0,103.0,67.9,4.94
Mauritius,26.09824,25.15669,14615.0,1238013.0,15.8,72.9,1.58
Mexico,28.737509999999997,27.42468,15826.0,114972821.0,17.9,75.4,2.35
Micronesia,31.28402,28.10315,3197.0,104472.0,43.1,68.0,3.59
Moldova,27.05617,24.2369,3890.0,4111168.0,17.6,70.4,1.49
Mongolia,25.71375,24.88385,7563.0,2629666.0,34.8,64.8,2.37
Montenegro,25.70186,26.55412,14183.0,619740.0,8.1,76.0,1.72
Morocco,26.223090000000003,25.63182,6091.0,31350544.0,35.8,73.3,2.44
Mozambique,23.317339999999998,21.93536,864.0,22994867.0,114.4,54.0,5.54
Myanmar,22.47733,21.44932,2891.0,51030006.0,87.2,59.4,2.05
Namibia,25.14988,22.65008,8169.0,2115703.0,62.2,59.1,3.36
Nepal,20.72814,20.76344,1866.0,26325183.0,50.7,68.4,2.9
Netherlands,25.47269,26.01541,47388.0,16519862.0,4.8,80.3,1.77
New Zealand,27.36642,27.768929999999997,32122.0,4285380.0,6.4,80.3,2.12
Nicaragua,27.57259,25.77291,4060.0,5594524.0,28.1,77.0,2.72
Niger,21.95958,21.21958,843.0,15085130.0,141.3,58.0,7.59
Nigeria,23.674020000000002,23.03322,4684.0,151115683.0,140.9,59.2,6.02
Norway,25.73772,26.934240000000003,65216.0,4771633.0,3.6,80.8,1.96
Oman,26.66535,26.241090000000003,47799.0,2652281.0,11.9,76.2,2.89
Pakistan,23.44986,22.299139999999998,4187.0,163096985.0,95.5,64.1,3.58
Panama,27.67758,26.26959,14033.0,3498679.0,21.0,77.3,2.61
Papua New Guinea,25.77189,25.015060000000002,1982.0,6540267.0,69.7,58.6,4.07
Paraguay,25.90523,25.54223,6684.0,6047131.0,25.7,74.0,3.06
Peru,25.98511,24.770410000000002,9249.0,28642048.0,23.2,78.2,2.58
Philippines,23.4671,22.872629999999997,5332.0,90297115.0,33.4,69.8,3.26
Poland,25.918870000000002,26.6738,19996.0,38525752.0,6.7,75.4,1.33
Portugal,26.183020000000003,26.68445,27747.0,10577458.0,4.1,79.4,1.36
Puerto Rico,30.2212,28.378040000000002,35855.0,3728126.0,8.78,77.0,1.69
Qatar,28.912509999999997,28.13138,126076.0,1388962.0,9.5,77.9,2.2
Romania,25.22425,25.41069,18032.0,20741669.0,16.1,73.2,1.34
Russia,27.21272,26.01131,22506.0,143123163.0,13.5,67.9,1.49
Rwanda,22.07156,22.55453,1173.0,9750314.0,78.3,64.1,5.06
Samoa,33.659079999999996,30.42475,5731.0,183440.0,18.8,72.3,4.43
Sao Tome and Principe,24.88216,23.51233,2673.0,163595.0,61.0,66.0,4.41
Saudi Arabia,29.598779999999998,27.884320000000002,44189.0,26742842.0,18.1,78.3,2.97
Senegal,24.30968,21.927429999999998,2162.0,12229703.0,75.8,63.5,5.11
Serbia,25.669970000000003,26.51495,12522.0,9109535.0,8.0,74.3,1.41
Seychelles,27.973740000000003,25.56236,20065.0,91634.0,14.2,72.9,2.28
Sierra Leone,23.93364,22.53139,1289.0,5521838.0,179.1,53.6,5.13
Singapore,22.86642,23.83996,65991.0,4849641.0,2.8,80.6,1.28
Slovak Republic,26.323729999999998,26.92717,24670.0,5396710.0,8.8,74.9,1.31
Slovenia,26.582140000000003,27.43983,30816.0,2030599.0,3.7,78.7,1.43
Solomon Islands,28.8762,27.159879999999998,1835.0,503410.0,33.1,62.3,4.36
Somalia,22.66607,21.969170000000002,615.0,9132589.0,168.5,52.6,7.06
South Africa,29.4803,26.85538,12263.0,50348811.0,66.1,53.4,2.54
Spain,26.30554,27.49975,34676.0,45817016.0,5.0,81.1,1.42
Sri Lanka,23.11717,21.96671,6907.0,19949553.0,11.7,74.0,2.32
Sudan,23.16132,22.40484,3246.0,34470138.0,84.7,65.5,4.79
Suriname,27.749859999999998,25.49887,13470.0,506657.0,26.4,70.2,2.41
Swaziland,28.448859999999996,23.16969,5887.0,1153750.0,112.2,45.1,3.7
Sweden,25.1466,26.37629,43421.0,9226333.0,3.2,81.1,1.92
Switzerland,24.07242,26.20195,55020.0,7646542.0,4.7,82.0,1.47
Syria,28.87418,26.919690000000003,6246.0,20097057.0,16.5,76.1,3.17
Tajikistan,23.84799,23.77966,2001.0,7254072.0,56.2,69.6,3.7
Tanzania,23.0843,22.47792,2030.0,42844744.0,72.4,60.4,5.54
Thailand,24.38577,23.008029999999998,12216.0,66453255.0,15.6,73.9,1.48
Togo,22.73858,21.87875,1219.0,6052937.0,96.4,57.5,4.88
Tonga,34.25969,30.99563,4748.0,102816.0,17.0,70.3,4.01
Trinidad and Tobago,28.27587,26.396690000000003,30875.0,1315372.0,24.9,71.7,1.8
Tunisia,27.93706,25.15699,9938.0,10408091.0,19.4,76.8,2.04
Turkey,28.247490000000003,26.703709999999997,16454.0,70344357.0,22.2,77.8,2.15
Turkmenistan,24.66154,25.24796,8877.0,4917541.0,63.9,67.2,2.48
Uganda,22.48126,22.35833,1437.0,31014427.0,89.3,56.0,6.34
Ukraine,26.23317,25.42379,8762.0,46028476.0,12.9,67.8,1.38
United Arab Emirates,29.614009999999997,28.053590000000003,73029.0,6900142.0,9.1,75.6,1.95
United Kingdom,26.944490000000002,27.392490000000002,37739.0,61689620.0,5.6,79.7,1.87
United States,28.343590000000003,28.456979999999998,50384.0,304473143.0,7.7,78.3,2.07
Uruguay,26.593040000000002,26.39123,15317.0,3350832.0,13.0,76.0,2.11
Uzbekistan,25.43432,25.32054,3733.0,26952719.0,49.2,69.6,2.46
Vanuatu,28.458759999999998,26.78926,2944.0,225335.0,28.2,63.4,3.61
Venezuela,28.134079999999997,27.445,17911.0,28116716.0,17.1,74.2,2.53
Vietnam,21.065,20.9163,4085.0,86589342.0,26.2,74.1,1.86
Palestine,29.026429999999998,26.5775,3564.0,3854667.0,24.7,74.1,4.38
Zambia,23.05436,20.68321,3039.0,13114579.0,94.9,51.1,5.88
Zimbabwe,24.645220000000002,22.0266,1286.0,13495462.0,98.3,47.3,3.85
1 Country female_BMI male_BMI gdp population under5mortality life_expectancy fertility
2 Afghanistan 21.07402 20.62058 1311.0 26528741.0 110.4 52.8 6.2
3 Albania 25.65726 26.44657 8644.0 2968026.0 17.9 76.8 1.76
4 Algeria 26.368409999999997 24.5962 12314.0 34811059.0 29.5 75.5 2.73
5 Angola 23.48431 22.25083 7103.0 19842251.0 192.0 56.7 6.43
6 Antigua and Barbuda 27.50545 25.76602 25736.0 85350.0 10.9 75.5 2.16
7 Argentina 27.46523 27.5017 14646.0 40381860.0 15.4 75.4 2.24
8 Armenia 27.1342 25.355420000000002 7383.0 2975029.0 20.0 72.3 1.4
9 Australia 26.87777 27.56373 41312.0 21370348.0 5.2 81.6 1.96
10 Austria 25.09414 26.467409999999997 43952.0 8331465.0 4.6 80.4 1.41
11 Azerbaijan 27.50879 25.65117 14365.0 8868713.0 43.3 69.2 1.99
12 Bahamas 29.13948 27.24594 24373.0 348587.0 14.5 72.2 1.89
13 Bahrain 28.790940000000003 27.83721 42507.0 1115777.0 9.4 77.6 2.23
14 Bangladesh 20.54531 20.39742 2265.0 148252473.0 55.9 68.3 2.38
15 Barbados 29.221690000000002 26.384390000000003 16075.0 277315.0 15.4 75.3 1.83
16 Belarus 26.641859999999998 26.16443 14488.0 9526453.0 7.2 70.0 1.42
17 Belgium 25.1446 26.75915 41641.0 10779155.0 4.7 79.6 1.82
18 Belize 29.81663 27.02255 8293.0 306165.0 20.1 70.7 2.91
19 Benin 23.74026 22.41835 1646.0 8973525.0 116.3 59.7 5.27
20 Bhutan 22.88243 22.8218 5663.0 694990.0 48.1 70.7 2.51
21 Bolivia 26.8633 24.43335 5066.0 9599916.0 52.0 71.2 3.48
22 Bosnia and Herzegovina 26.35874 26.611629999999998 9316.0 3839749.0 8.1 77.5 1.22
23 Botswana 26.09156 22.129839999999998 13858.0 1967866.0 63.8 53.2 2.86
24 Brazil 25.99113 25.78623 13906.0 194769696.0 18.6 73.2 1.9
25 Brunei 22.892310000000002 24.18179 72351.0 380786.0 9.0 76.9 2.1
26 Bulgaria 25.51574 26.542859999999997 15368.0 7513646.0 13.7 73.2 1.43
27 Burkina Faso 21.63031 21.27157 1358.0 14709011.0 130.4 58.0 6.04
28 Burundi 21.27927 21.50291 723.0 8821795.0 108.6 59.1 6.48
29 Cambodia 21.69608 20.80496 2442.0 13933660.0 51.5 66.1 3.05
30 Cameroon 24.9527 23.681729999999998 2571.0 19570418.0 113.8 56.6 5.17
31 Canada 26.698290000000004 27.4521 41468.0 33363256.0 5.8 80.8 1.68
32 Cape Verde 24.96136 23.515220000000003 6031.0 483824.0 28.4 70.4 2.57
33 Chad 21.95424 21.485689999999998 1753.0 11139740.0 168.0 54.3 6.81
34 Chile 27.92807 27.015420000000002 18698.0 16645940.0 8.9 78.5 1.89
35 China 22.91041 22.92176 7880.0 1326690636.0 18.5 73.4 1.53
36 Colombia 26.22529 24.94041 10489.0 44901660.0 19.7 76.2 2.43
37 Comoros 22.444329999999997 22.06131 1440.0 665414.0 91.2 67.1 5.05
38 Congo, The Democratic Republic of the 21.6677 19.86692 607.0 61809278.0 124.5 57.5 6.45
39 Congo 23.10824 21.87134 5022.0 3832771.0 72.6 58.8 5.1
40 Costa Rica 27.03497 26.47897 12219.0 4429506.0 10.3 79.8 1.91
41 Ivory Coast 23.82088 22.56469 2854.0 19261647.0 116.9 55.4 4.91
42 Croatia 25.17882 26.596290000000003 21873.0 4344151.0 5.9 76.2 1.43
43 Cuba 26.576140000000002 25.06867 17765.0 11290239.0 6.3 77.6 1.5
44 Cyprus 25.92587 27.41899 35828.0 1077010.0 4.2 80.0 1.49
45 Denmark 25.106270000000002 26.13287 45017.0 5495302.0 4.3 78.9 1.89
46 Djibouti 24.38177 23.38403 2502.0 809639.0 81.0 61.8 3.76
47 Ecuador 27.062690000000003 25.58841 9244.0 14447600.0 26.8 74.7 2.73
48 Egypt 30.099970000000003 26.732429999999997 9974.0 78976122.0 31.4 70.2 2.95
49 El Salvador 27.84092 26.36751 7450.0 6004199.0 21.6 73.7 2.32
50 Equatorial Guinea 24.528370000000002 23.7664 40143.0 686223.0 118.4 57.5 5.31
51 Eritrea 21.082320000000003 20.885089999999998 1088.0 4500638.0 60.4 60.1 5.16
52 Estonia 25.185979999999997 26.264459999999996 24743.0 1339941.0 5.5 74.2 1.62
53 Ethiopia 20.71463 20.247 931.0 83079608.0 86.9 60.0 5.19
54 Fiji 29.339409999999997 26.53078 7129.0 843206.0 24.0 64.9 2.74
55 Finland 25.58418 26.733390000000004 42122.0 5314170.0 3.3 79.6 1.85
56 France 24.82949 25.853289999999998 37505.0 62309529.0 4.3 81.1 1.97
57 Gabon 25.95121 24.0762 15800.0 1473741.0 68.0 61.7 4.28
58 Gambia 24.82101 21.65029 1566.0 1586749.0 87.4 65.7 5.8
59 Georgia 26.45014 25.54942 5900.0 4343290.0 19.3 71.8 1.79
60 Germany 25.73903 27.165090000000003 41199.0 80665906.0 4.4 80.0 1.37
61 Ghana 24.33014 22.842470000000002 2907.0 23115919.0 79.9 62.0 4.19
62 Greece 24.92026 26.33786 32197.0 11161755.0 4.9 80.2 1.46
63 Grenada 27.31948 25.179879999999997 12116.0 103934.0 13.5 70.8 2.28
64 Guatemala 26.84324 25.29947 6960.0 14106687.0 36.9 71.2 4.12
65 Guinea 22.45206 22.52449 1230.0 10427356.0 121.0 57.1 5.34
66 Guinea-Bissau 22.92809 21.64338 1326.0 1561293.0 127.6 53.6 5.25
67 Guyana 26.470190000000002 23.68465 5208.0 748096.0 41.9 65.0 2.74
68 Haiti 23.27785 23.66302 1600.0 9705130.0 83.3 61.0 3.5
69 Honduras 26.73191 25.10872 4391.0 7259470.0 26.5 71.8 3.27
70 Hong Kong 23.71046 25.057470000000002 46635.0 6910384.0 3.06 82.49 1.04
71 Hungary 25.97839 27.115679999999998 23334.0 10050699.0 7.2 73.9 1.33
72 Iceland 26.02599 27.206870000000002 42294.0 310033.0 2.7 82.4 2.12
73 India 21.31478 20.95956 3901.0 1197070109.0 65.6 64.7 2.64
74 Indonesia 22.986929999999997 21.85576 7856.0 235360765.0 36.2 69.4 2.48
75 Iran 27.236079999999998 25.310029999999998 15955.0 72530693.0 21.4 73.1 1.88
76 Iraq 28.411170000000002 26.71017 11616.0 29163327.0 38.3 66.6 4.34
77 Ireland 26.62176 27.65325 47713.0 4480145.0 4.5 80.1 2.0
78 Israel 27.301920000000003 27.13151 28562.0 7093808.0 4.9 80.6 2.92
79 Italy 24.79289 26.4802 37475.0 59319234.0 4.1 81.5 1.39
80 Jamaica 27.22601 24.00421 8951.0 2717344.0 18.9 75.1 2.39
81 Japan 21.87088 23.50004 34800.0 127317900.0 3.4 82.5 1.34
82 Jordan 29.218009999999996 27.47362 10897.0 6010035.0 22.1 76.9 3.59
83 Kazakhstan 26.65065 26.290779999999998 18797.0 15915966.0 25.9 67.1 2.51
84 Kenya 23.06181 21.592579999999998 2358.0 38244442.0 71.0 60.8 4.76
85 Kiribati 31.30769 29.2384 1803.0 98437.0 64.5 61.5 3.13
86 Kuwait 31.161859999999997 29.172109999999996 91966.0 2705290.0 11.3 77.3 2.68
87 Latvia 25.615129999999997 26.45693 20977.0 2144215.0 10.5 72.4 1.5
88 Lebanon 27.70471 27.20117 14158.0 4109389.0 11.3 77.8 1.57
89 Lesotho 26.780520000000003 21.90157 2041.0 1972194.0 114.2 44.5 3.34
90 Liberia 23.21679 21.89537 588.0 3672782.0 100.9 59.9 5.19
91 Libya 29.19874 26.54164 29853.0 6123022.0 18.8 75.6 2.64
92 Lithuania 26.01424 26.86102 23223.0 3219802.0 8.2 72.1 1.42
93 Luxembourg 26.09326 27.434040000000003 95001.0 485079.0 2.8 81.0 1.63
94 Macao 24.895039999999998 25.713820000000002 80191.0 507274.0 6.72 79.32 0.94
95 Macedonia 25.37646 26.34473 10872.0 2055266.0 11.8 74.5 1.47
96 Madagascar 20.73501 21.403470000000002 1528.0 19926798.0 66.7 62.2 4.79
97 Malawi 22.91455 22.034679999999998 674.0 13904671.0 101.1 52.4 5.78
98 Malaysia 25.448320000000002 24.73069 19968.0 27197419.0 8.0 74.5 2.05
99 Maldives 26.4132 23.219910000000002 12029.0 321026.0 16.0 78.5 2.38
100 Mali 23.07655 21.78881 1602.0 14223403.0 148.3 58.5 6.82
101 Malta 27.04993 27.683609999999998 27872.0 406392.0 6.6 80.7 1.38
102 Mauritania 26.26476 22.62295 3356.0 3414552.0 103.0 67.9 4.94
103 Mauritius 26.09824 25.15669 14615.0 1238013.0 15.8 72.9 1.58
104 Mexico 28.737509999999997 27.42468 15826.0 114972821.0 17.9 75.4 2.35
105 Micronesia 31.28402 28.10315 3197.0 104472.0 43.1 68.0 3.59
106 Moldova 27.05617 24.2369 3890.0 4111168.0 17.6 70.4 1.49
107 Mongolia 25.71375 24.88385 7563.0 2629666.0 34.8 64.8 2.37
108 Montenegro 25.70186 26.55412 14183.0 619740.0 8.1 76.0 1.72
109 Morocco 26.223090000000003 25.63182 6091.0 31350544.0 35.8 73.3 2.44
110 Mozambique 23.317339999999998 21.93536 864.0 22994867.0 114.4 54.0 5.54
111 Myanmar 22.47733 21.44932 2891.0 51030006.0 87.2 59.4 2.05
112 Namibia 25.14988 22.65008 8169.0 2115703.0 62.2 59.1 3.36
113 Nepal 20.72814 20.76344 1866.0 26325183.0 50.7 68.4 2.9
114 Netherlands 25.47269 26.01541 47388.0 16519862.0 4.8 80.3 1.77
115 New Zealand 27.36642 27.768929999999997 32122.0 4285380.0 6.4 80.3 2.12
116 Nicaragua 27.57259 25.77291 4060.0 5594524.0 28.1 77.0 2.72
117 Niger 21.95958 21.21958 843.0 15085130.0 141.3 58.0 7.59
118 Nigeria 23.674020000000002 23.03322 4684.0 151115683.0 140.9 59.2 6.02
119 Norway 25.73772 26.934240000000003 65216.0 4771633.0 3.6 80.8 1.96
120 Oman 26.66535 26.241090000000003 47799.0 2652281.0 11.9 76.2 2.89
121 Pakistan 23.44986 22.299139999999998 4187.0 163096985.0 95.5 64.1 3.58
122 Panama 27.67758 26.26959 14033.0 3498679.0 21.0 77.3 2.61
123 Papua New Guinea 25.77189 25.015060000000002 1982.0 6540267.0 69.7 58.6 4.07
124 Paraguay 25.90523 25.54223 6684.0 6047131.0 25.7 74.0 3.06
125 Peru 25.98511 24.770410000000002 9249.0 28642048.0 23.2 78.2 2.58
126 Philippines 23.4671 22.872629999999997 5332.0 90297115.0 33.4 69.8 3.26
127 Poland 25.918870000000002 26.6738 19996.0 38525752.0 6.7 75.4 1.33
128 Portugal 26.183020000000003 26.68445 27747.0 10577458.0 4.1 79.4 1.36
129 Puerto Rico 30.2212 28.378040000000002 35855.0 3728126.0 8.78 77.0 1.69
130 Qatar 28.912509999999997 28.13138 126076.0 1388962.0 9.5 77.9 2.2
131 Romania 25.22425 25.41069 18032.0 20741669.0 16.1 73.2 1.34
132 Russia 27.21272 26.01131 22506.0 143123163.0 13.5 67.9 1.49
133 Rwanda 22.07156 22.55453 1173.0 9750314.0 78.3 64.1 5.06
134 Samoa 33.659079999999996 30.42475 5731.0 183440.0 18.8 72.3 4.43
135 Sao Tome and Principe 24.88216 23.51233 2673.0 163595.0 61.0 66.0 4.41
136 Saudi Arabia 29.598779999999998 27.884320000000002 44189.0 26742842.0 18.1 78.3 2.97
137 Senegal 24.30968 21.927429999999998 2162.0 12229703.0 75.8 63.5 5.11
138 Serbia 25.669970000000003 26.51495 12522.0 9109535.0 8.0 74.3 1.41
139 Seychelles 27.973740000000003 25.56236 20065.0 91634.0 14.2 72.9 2.28
140 Sierra Leone 23.93364 22.53139 1289.0 5521838.0 179.1 53.6 5.13
141 Singapore 22.86642 23.83996 65991.0 4849641.0 2.8 80.6 1.28
142 Slovak Republic 26.323729999999998 26.92717 24670.0 5396710.0 8.8 74.9 1.31
143 Slovenia 26.582140000000003 27.43983 30816.0 2030599.0 3.7 78.7 1.43
144 Solomon Islands 28.8762 27.159879999999998 1835.0 503410.0 33.1 62.3 4.36
145 Somalia 22.66607 21.969170000000002 615.0 9132589.0 168.5 52.6 7.06
146 South Africa 29.4803 26.85538 12263.0 50348811.0 66.1 53.4 2.54
147 Spain 26.30554 27.49975 34676.0 45817016.0 5.0 81.1 1.42
148 Sri Lanka 23.11717 21.96671 6907.0 19949553.0 11.7 74.0 2.32
149 Sudan 23.16132 22.40484 3246.0 34470138.0 84.7 65.5 4.79
150 Suriname 27.749859999999998 25.49887 13470.0 506657.0 26.4 70.2 2.41
151 Swaziland 28.448859999999996 23.16969 5887.0 1153750.0 112.2 45.1 3.7
152 Sweden 25.1466 26.37629 43421.0 9226333.0 3.2 81.1 1.92
153 Switzerland 24.07242 26.20195 55020.0 7646542.0 4.7 82.0 1.47
154 Syria 28.87418 26.919690000000003 6246.0 20097057.0 16.5 76.1 3.17
155 Tajikistan 23.84799 23.77966 2001.0 7254072.0 56.2 69.6 3.7
156 Tanzania 23.0843 22.47792 2030.0 42844744.0 72.4 60.4 5.54
157 Thailand 24.38577 23.008029999999998 12216.0 66453255.0 15.6 73.9 1.48
158 Togo 22.73858 21.87875 1219.0 6052937.0 96.4 57.5 4.88
159 Tonga 34.25969 30.99563 4748.0 102816.0 17.0 70.3 4.01
160 Trinidad and Tobago 28.27587 26.396690000000003 30875.0 1315372.0 24.9 71.7 1.8
161 Tunisia 27.93706 25.15699 9938.0 10408091.0 19.4 76.8 2.04
162 Turkey 28.247490000000003 26.703709999999997 16454.0 70344357.0 22.2 77.8 2.15
163 Turkmenistan 24.66154 25.24796 8877.0 4917541.0 63.9 67.2 2.48
164 Uganda 22.48126 22.35833 1437.0 31014427.0 89.3 56.0 6.34
165 Ukraine 26.23317 25.42379 8762.0 46028476.0 12.9 67.8 1.38
166 United Arab Emirates 29.614009999999997 28.053590000000003 73029.0 6900142.0 9.1 75.6 1.95
167 United Kingdom 26.944490000000002 27.392490000000002 37739.0 61689620.0 5.6 79.7 1.87
168 United States 28.343590000000003 28.456979999999998 50384.0 304473143.0 7.7 78.3 2.07
169 Uruguay 26.593040000000002 26.39123 15317.0 3350832.0 13.0 76.0 2.11
170 Uzbekistan 25.43432 25.32054 3733.0 26952719.0 49.2 69.6 2.46
171 Vanuatu 28.458759999999998 26.78926 2944.0 225335.0 28.2 63.4 3.61
172 Venezuela 28.134079999999997 27.445 17911.0 28116716.0 17.1 74.2 2.53
173 Vietnam 21.065 20.9163 4085.0 86589342.0 26.2 74.1 1.86
174 Palestine 29.026429999999998 26.5775 3564.0 3854667.0 24.7 74.1 4.38
175 Zambia 23.05436 20.68321 3039.0 13114579.0 94.9 51.1 5.88
176 Zimbabwe 24.645220000000002 22.0266 1286.0 13495462.0 98.3 47.3 3.85

BIN
zajecia4/logistic.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

File diff suppressed because one or more lines are too long

View File

@ -0,0 +1,430 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Kkolejna część zajęć będzie wprowadzeniem do szeroko używanej biblioteki w Pythonie: `sklearn`. Zajęcia będą miały charaktere case-study poprzeplatane zadaniami do wykonania. Zacznijmy od załadowania odpowiednich bibliotek."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"# ! pip install matplotlib"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import matplotlib.pyplot as plt\n",
"\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Zacznijmy od załadowania danych. Na dzisiejszych zajęciach będziemy korzystać z danych z portalu [gapminder.org](https://www.gapminder.org/data/)."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df = pd.read_csv('gapminder.csv', index_col=0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Dane zawierają różne informacje z większość państw świata (z roku 2008). Poniżej znajduje się opis kolumn:\n",
" * female_BMI - średnie BMI u kobiet\n",
" * male_BMI - średnie BMI u mężczyzn\n",
" * gdp - PKB na obywatela\n",
" * population - wielkość populacji\n",
" * under5mortality - wskaźnik śmiertelności dzieni pon. 5 roku życia (na 1000 urodzonych dzieci)\n",
" * life_expectancy - średnia długość życia\n",
" * fertility - wskaźnik dzietności"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 1**\n",
"Na podstawie danych zawartych w `df` odpowiedz na następujące pytania:\n",
" * Jaki był współczynniki dzietności w Polsce w 2018?\n",
" * W którym kraju ludzie żyją najdłużej?\n",
" * Z ilu krajów zostały zebrane dane?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 2** Stwórz kolumnę `gdp_log`, która powstanie z kolumny `gdp` poprzez zastowanie funkcji `log` (logarytm). \n",
"\n",
"Hint 1: Wykorzystaj funkcję `apply` (https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.apply.html#pandas.Series.apply).\n",
"\n",
"Hint 2: Wykorzystaj fukcję `log` z pakietu `np`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Naszym zadaniem będzie oszacowanie długości życia (kolumna `life_expectancy`) na podstawie pozostałych zmiennych. Na samym początku, zastosujemy regresje jednowymiarową na `fertility`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = df['life_expectancy'].values\n",
"X = df['fertility'].values\n",
"\n",
"print(\"Y shape:\", y.shape)\n",
"print(\"X shape:\", X.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Będziemy korzystać z gotowej implementacji regreji liniowej z pakietu sklearn. Żeby móc wykorzystać, musimy napierw zmienić shape na dwuwymiarowy."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"y = y.reshape(-1, 1)\n",
"X = X.reshape(-1, 1)\n",
"\n",
"print(\"Y shape:\", y.shape)\n",
"print(\"X shape:\", X.shape)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Jeszcze przed właściwą analizą, narysujmy wykres i zobaczny czy istnieje \"wizualny\" związek pomiędzy kolumnami."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"df.plot.scatter('fertility', 'life_expectancy')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 3** Zaimportuj `LinearRegression` z pakietu `sklearn.linear_model`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tworzymy obiekt modelu regresji liniowej."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model = LinearRegression()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Trening modelu ogranicza się do wywołania metodu `fit`, która przyjmuje dwa argumenty:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model.fit(X, y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Współczynniki modelu:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"print(\"Wyraz wolny (bias):\", model.intercept_)\n",
"print(\"Współczynniki cech:\", model.coef_)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 4** Wytrenuj nowy model `model2`, który będzie jako X przyjmie kolumnę `gdp_log`. Wyświetl parametry nowego modelu."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Mając wytrenowany model możemy wykorzystać go do predykcji. Wystarczy wywołać metodę `predict`."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X_test = X[:5,:]\n",
"y_test = y[:5,:]\n",
"output = model.predict(X_test)\n",
"\n",
"for i in range(5):\n",
" print(\"input: {}\\t predicted: {}\\t expected: {}\".format(X_test[i,0], output[i,0], y_test[i,0]))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Sprawdzenie jakości modelu - metryki: $MSE$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Istnieją 3 metryki, które określają jak dobry jest nasz model:\n",
" * $MSE$: [błąd średnio-kwadratowy](https://pl.wikipedia.org/wiki/B%C5%82%C4%85d_%C5%9Bredniokwadratowy) \n",
" * $RMSE = \\sqrt{MSE}$"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from sklearn.metrics import mean_squared_error\n",
"\n",
"rmse = np.sqrt(mean_squared_error(y, model.predict(X)))\n",
"print(\"Root Mean Squared Error: {}\".format(rmse))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Import necessary modules\n",
"from sklearn.linear_model import LinearRegression\n",
"from sklearn.metrics import mean_squared_error\n",
"from sklearn.model_selection import train_test_split\n",
"\n",
"# Create training and test sets\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30, random_state=42)\n",
"\n",
"# Create the regressor: reg_all\n",
"reg_all = LinearRegression()\n",
"\n",
"# Fit the regressor to the training data\n",
"reg_all.fit(X_train, y_train)\n",
"\n",
"# Predict on the test data: y_pred\n",
"y_pred = reg_all.predict(X_test)\n",
"\n",
"# Compute and print R^2 and RMSE\n",
"print(\"R^2: {}\".format(reg_all.score(X_test, y_test)))\n",
"rmse = np.sqrt(mean_squared_error(y_test, y_pred))\n",
"print(\"Root Mean Squared Error: {}\".format(rmse))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Regresja wielu zmiennych"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Model regresji liniowej wielu zmiennych nie różni się istotnie od modelu jednej zmiennej. Np. chcąc zbudować model oparty o dwie kolumny: `fertility` i `gdp` wystarczy zmienić X (cechy wejściowe):"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"X = df[['fertility', 'gdp']]\n",
"X_train, X_test, y_train, y_test = train_test_split(X, y, test_size = 0.30, random_state=42)\n",
"\n",
"print(X.shape)\n",
"\n",
"model_mv = LinearRegression()\n",
"model_mv.fit(X_train, y_train)\n",
"\n",
"print(\"Wyraz wolny (bias):\", model_mv.intercept_)\n",
"print(\"Współczynniki cech:\", model_mv.coef_)\n",
"\n",
"y_pred = model_mv.predict(X_test)\n",
"\n",
"rmse = np.sqrt(mean_squared_error(y_test, y_pred))\n",
"print(\"Root Mean Squared Error: {}\".format(rmse))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 6** \n",
" * Zbuduj model regresji liniowej, która oszacuje wartność kolumny `life_expectancy` na podstawie pozostałych kolumn.\n",
" * Wyświetl współczynniki modelu.\n",
" * Oblicz wartości metryki rmse na zbiorze trenującym.\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**zad. 7**\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" Zaimplementuj metrykę $RMSE$ jako fukcję rmse (szablon poniżej). Fukcja rmse przyjmuje dwa parametry typu list i ma zwrócić wartość metryki $RMSE$ ."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def rmse(expected, predicted):\n",
" \"\"\"\n",
" argumenty:\n",
" expected (type: list): poprawne wartości\n",
" predicted (type: list): oszacowanie z modelu\n",
" \"\"\"\n",
" pass\n",
" \n",
"\n",
"y = df['life_expectancy'].values\n",
"X = df[['fertility', 'gdp']].values\n",
"\n",
"test_model = LinearRegression()\n",
"test_model.fit(X, y)\n",
"\n",
"predicted = list(test_model.predict(X))\n",
"expected = list(y)\n",
"\n",
"print(rmse(predicted,expected))\n",
"print(np.sqrt(mean_squared_error(predicted, expected)))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}