<jupyter_start><jupyter_text>Linearna algebra<jupyter_code>import numpy as np
A = np.array([[1,2,3],
              [4,5,6]])
A
A.shape
E = np.eye(5)
E
np.zeros((2,3))
ones = np.ones((2,3))
ones
-1 * ones
5 + ones
v = np.array([1,2,3])
ones + v
np.sin(A)
B = np.array([[1,2,1],[2,2,3],[4,5,5]])
B.shape
A.shape
C = np.ones((3,3))
B + C
B * C == B
A.dot(B)
B
v = B[:,0]
v
v = np.array([1,2,3,4,5,6,7])
v[::2]
v[::-1]
from numpy import linalg as LA
LA.det(B)
LA.det(np.ones((2,2)))
LA.inv(B)
LA.norm(v)
LA.norm(B, ord='fro')
LA.cond(B)
LA.norm(B) * LA.norm(LA.inv(B))
LA.cond(np.ones((2,2)))
LA.cond(np.array([[1,1],
                  [1,0.999]]))
LA.eig(B)<jupyter_output><empty_output><jupyter_text>Linearna regresija<jupyter_code># Ax = b
A = np.array([[2,0],
              [-1,1],
              [0,2]])
b = np.array([2,0,-2])
x = LA.inv(A.T.dot(A)).dot(A.T).dot(b)
x
x, rss, _, _ = LA.lstsq(A, b, rcond=None)
x
rss<jupyter_output><empty_output><jupyter_text>Zadatak 1Odrediti koeficijente w0 i w1 tako da funkcija 𝑓(𝑥)=w0+w1𝑥 u smislu metode najmanjih kvadrata najbolje aproksimira skup tačaka (0,1.2), (0.5,2.05), (1,2.9) i (−0.5,0.1) u ravni.<jupyter_code># Aw = w0 + w1x
x = np.array([0, 0.5, 1, -0.5])
y = np.array([1.2, 2.05, 2.9, 0.1])
ones = np.ones(4)
A = np.vstack((ones, x)).T

LA.lstsq(A, y, rcond=None)<jupyter_output><empty_output><jupyter_text>Zadatak 2Odrediti vrednosti koeficijenata 𝑎 i 𝑏 tako da funkcija 𝑓(𝑥)=𝑎+𝑏sin𝑥 u smislu metode najmanjih kvadrata aproksimira skup tacaka (2,2.6), (−1.22,−1.7), (8.32,2.5) i (4.23,−1.6) u ravni. Dati ocenu greske. Prikazati skup tačaka i nacrtati rezultujucu funkciju.<jupyter_code>x = np.array([2,-1.22,8.32,4.23])
y = np.array([2.6,-1.7,2.5,-1.6])

A = np.vstack((ones, np.sin(x))).T

solution, rss, _, _ = LA.lstsq(A, y, rcond=None)
a, b = solution
print(a, b)
from matplotlib import pyplot as plt
xs = np.linspace(-5, 10, 100)
plt.plot(xs, a + b * np.sin(xs))
plt.plot(x, y, 'o')<jupyter_output><empty_output><jupyter_text>Zadatak 3U datoteci social_reach.csv se nalaze cene reklamiranja za različite demografske grupe, koje su date u hiljadama evra za 1000 pregleda. Svaka od tri kolone označava različitu platformu za reklamiranje (na primer, platforme mogu biti Facebook, Instagram ili YouTube). Svaki red označava različitu demografsku grupu, koja može npr. biti posebna država u kojoj se reklama plasira. Potrebno je odrediti iznos sredstava da se ostvari približno milion pregleda za svaku demografsku grupu, gledajući po svim platformama ukupno.<jupyter_code>import pandas as pd
df = pd.read_csv('social_reach.csv')
df
y = 1000 * np.ones(10)
y
A = df[['web1', 'web2', 'web3']]
A
LA.lstsq(A, y, rcond=None)<jupyter_output><empty_output><jupyter_text>Zadatak 4Svaki red u fajlu advertising.csv sadrži informacije o cenama u hiljadama dolara reklamnih usluga na određenom tržištu. Prva kolona se odnosi na cene reklamiranja na televiziji, druga na radiju, a treća u novinama. Četvrta kolona se odnosi na ukupnu prodaju proizvoda koji su se reklamirali na datim medijima. Kreirati linearni model koji predviđa prodaju na osnovu cena reklamiranja.<jupyter_code>from sklearn.linear_model import LinearRegression
import pandas as pd
from matplotlib import pyplot as plt
df = pd.read_csv('advertising.csv')
df.head()
tv = df['TV']
sales = df['Sales']
plt.scatter(tv, sales)
radio = df['Radio']
plt.scatter(radio, sales)
newspaper = df['Newspaper']
plt.scatter(newspaper, sales)
X = df[['TV', 'Radio', 'Newspaper']]
y = df['Sales']
model = LinearRegression()
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3)
model.fit(X_train, y_train)
model.coef_
model.intercept_
2.74 + 0.05*TV + 0.19 * Radio - 0.001 * Novine
from sklearn.metrics import mean_absolute_error, mean_squared_error, r2_score
y_pred = model.predict(X_test)
mean_squared_error(y_test, y_pred)
r2_score(y_test, y_pred)
from sklearn.model_selection import cross_val_score
scores = cross_val_score(model, X_train, y_train, cv=5)
scores.mean()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.15384615384615385, 'fraction_numerical': 0.042025067584173015, 'mean_word_length': 4.985294117647059, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '667289', 'license': 'MIT', 'n_tokens_mistral': 1938, 'n_tokens_neox': 1875, 'n_words': 430, 'path': '2021_2022/live/01_linear_regression.ipynb', 'repo_name': 'MATF-RI/Materijali-sa-vezbi', 'size': '4069'}