Esempio n. 1
0
from sklearn.linear_model import LinearRegression
lin_reg = LinearRegression()
lin_reg.fit(X, y)

#Fitting polynomial regression to the dataset
from sklearn.preprocessing import PolynomialFeatures
poly_reg = PolynomialFeatures(degree = 4)
X_poly = poly_reg.fit_transform(X)
lin_reg2 = LinearRegression()
lin_reg2.fit(X_poly, y)

plt.scatter(X, y, color='red')
plt.plot(X, lin_reg.predict(X), color='blue')
plt.title('Truth of bluff (Linear regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

X_grid = np.arange(min(X), max(X), 0.1)
X_grid = X_grid.reshape((len(X_grid), 1))
plt.scatter(X, y, color='red')
plt.plot(X_grid, lin_reg2.predict(poly_reg.fit_transform(X_grid)), color='blue')
plt.title('Truth of bluff (Linear regression)')
plt.xlabel('Position level')
plt.ylabel('Salary')
plt.show()

#Predicting a new result with linear regression
lin_reg.predict(array.reshape(1, 6.5))

#Predicting a new result with polynomial regression
Esempio n. 2
0
x = numpy.zeros(7, int)
print(x)

x = numpy.ones(7)
print(x)

arr = numpy.array([[3, 4, 5, 5], [6, 7, 8, 8], [5, 6, 7, 3]])
print(arr)
arr2 = arr + 2
print(arr2)
arr3 = arr + arr2
print(arr3)
arr4 = arr.flatten()  #multi dimensional to single dimensional
print(arr4)

arr.reshape(3, 2, 2)  # to multi dimentionsl matrix
arr = numpy.array([[3, 4, 5], [6, 7, 8], [5, 6, 7]])
print(arr)
m1 = matrix(arr)  #matxi
print(m1)
m1 = numpy.matrix('3,4,5;5,6,7;7,8,9')
print(m1)
m1.dtype
print(diagonal(arr))
#######################MULTITHREADING######################
from threading import *
from time import sleep


class Hello(Thread):
    def run(self):
Esempio n. 3
0
print(arr1 + arr2)  #adds corresponding elemnets or arrays
arr = arr1 + 5  #adds 5 to all elements of array
print(arr)
print(concatenate([arr1, arr2]))
arr = arr1.view()  #copies the array with changes made
print(arr)
arr1[1] = 19
print(arr)
arr = arr1.copy()  #deep copy
arr1[1] = 10
print(arr)

from numpy import *
arr = array([[1, 2, 3, 4, 5, 6], [6, 7, 8, 9, 0, 1]])
print(arr.flatten())  #converts to one dimensional
print(arr.reshape(2, 2, 3))  #convert array into 2*2*3 dimensions

a = [
    [1, 2, 3],  #list as matrix and function for multiplication of matrix
    [4, 5, 6]
]
b = [[10, 11, 12, 13], [12, 13, 14, 15], [14, 15, 16, 17]]


def mml(x, y):
    if len(x[0]) == len(y):  #check matrix conpatibility
        a = len(x)
        b = len(x[0])
        c = len(y[0])
        z = [[], []]
        sum = 0
def _sframe_to_nparray(sf):
    """
    Converts a numeric SFrame to a numpy Array.
    Every column in the SFrame must be of numeric (integer, float) or
    array.array type. The resultant Numpy array the same number of rows as the
    input SFrame, and with each row merged into a single array.

    Missing values in integer columns are converted to 0, and missing values
    in float columns are converted to NaN.

    Example:

    >>> sf = gl.SFrame({'a':[1,1,None],
    >>>                 'b':[2.0,2.0,None],
    >>>                 'c':[[3.0,3.0],[3.0,3.0],[3.0,3.0]]})
    >>> sf
    Columns:
            a	int
            b	float
            c	array
    Rows: 3
    Data:
    +------+------+------------+
    |  a   |  b   |     c      |
    +------+------+------------+
    |  1   | 2.0  | [3.0, 3.0] |
    |  1   | 2.0  | [3.0, 3.0] |
    | None | None | [3.0, 3.0] |
    +------+------+------------+
    [3 rows x 3 columns]
    >>> n = gl.numpy.sframe_to_nparray(sf)
    >>> n
    array([[ 1.,  2.,  3.,  3.],
           [ 1.,  2.,  3.,  3.],
           [ 0., nan,  3.,  3.]])
    """
    _mt._get_metric_tracker().track('snumpy.sframe_to_np')
    if not numpy_activation_successful():
        raise RuntimeError(
            "This function cannot be used if Scalable Numpy activation failed")
    import graphlab
    import graphlab.util
    import ctypes
    import numpy as np
    import graphlab.cython.pointer_to_ndarray
    import array
    if not all(d in [int, float, array.array] for d in sf.dtype()):
        raise TypeError(
            "Only integer, float or array column types are supported")
    temp_dir = graphlab.util._make_temp_directory("numpy_convert_")
    # only sframes have save_reference
    sf._save_reference(temp_dir)
    unity_dll = _get_unity_dll()
    if unity_dll == None:
        raise RuntimeError("fast Converter not loaded")
    if all(d in [int] for d in sf.dtype()):
        ptr = unity_dll.pointer_from_sframe(temp_dir.encode(), True)
        if not ptr:
            raise RuntimeError("Unable to convert to numpy array")
        arrlen = unity_dll.pointer_length(ptr) // 8
        ArrayType = ctypes.c_int64 * arrlen
        addr = ctypes.addressof(ptr.contents)
        array = np.frombuffer(ArrayType.from_address(addr), np.int64)
        graphlab.cython.pointer_to_ndarray.numpy_own_array(array)
        width = arrlen / len(sf)
        if width > 1:
            return array.reshape(len(sf), width)
        else:
            return array
    else:
        unity_dll.pointer_from_sframe.restype = ctypes.POINTER(ctypes.c_double)
        ptr = unity_dll.pointer_from_sframe(temp_dir.encode(), True)
        if not ptr:
            raise RuntimeError("Unable to convert to numpy array")
        arrlen = unity_dll.pointer_length(ptr) // 8
        ArrayType = ctypes.c_double * arrlen
        addr = ctypes.addressof(ptr.contents)
        array = np.frombuffer(ArrayType.from_address(addr), np.double)
        graphlab.cython.pointer_to_ndarray.numpy_own_array(array)
        width = arrlen / len(sf)
        if width > 1:
            return array.reshape(len(sf), arrlen / len(sf))
        else:
            return array
Esempio n. 5
0
def _sframe_to_nparray(sf):
    """
    Converts a numeric SFrame to a numpy Array.
    Every column in the SFrame must be of numeric (integer, float) or 
    array.array type. The resultant Numpy array the same number of rows as the
    input SFrame, and with each row merged into a single array.

    Missing values in integer columns are converted to 0, and missing values
    in float columns are converted to NaN.

    Example:

    >>> sf = gl.SFrame({'a':[1,1,None],
    >>>                 'b':[2.0,2.0,None],
    >>>                 'c':[[3.0,3.0],[3.0,3.0],[3.0,3.0]]})
    >>> sf
    Columns:
            a	int
            b	float
            c	array
    Rows: 3
    Data:
    +------+------+------------+
    |  a   |  b   |     c      |
    +------+------+------------+
    |  1   | 2.0  | [3.0, 3.0] |
    |  1   | 2.0  | [3.0, 3.0] |
    | None | None | [3.0, 3.0] |
    +------+------+------------+
    [3 rows x 3 columns]
    >>> n = gl.numpy.sframe_to_nparray(sf)
    >>> n
    array([[ 1.,  2.,  3.,  3.],
           [ 1.,  2.,  3.,  3.],
           [ 0., nan,  3.,  3.]])
    """
    _mt._get_metric_tracker().track('snumpy.sframe_to_np')
    if not numpy_activation_successful():
        raise RuntimeError("This function cannot be used if Scalable Numpy activation failed")
    import graphlab
    import graphlab.util
    import ctypes
    import numpy as np
    import graphlab.cython.pointer_to_ndarray
    import array
    if not all(d in [int, float, array.array] for d in sf.dtype()):
        raise TypeError("Only integer, float or array column types are supported")
    temp_dir = graphlab.util._make_temp_directory("numpy_convert_")
    # only sframes have save_reference
    sf._save_reference(temp_dir)
    unity_dll = _get_unity_dll()
    if unity_dll == None:
        raise RuntimeError("fast Converter not loaded")
    if all(d in [int] for d in sf.dtype()):
        ptr = unity_dll.pointer_from_sframe(temp_dir, True)
        if not ptr:
            raise RuntimeError("Unable to convert to numpy array")
        arrlen = unity_dll.pointer_length(ptr) / 8
        ArrayType = ctypes.c_int64 * arrlen
        addr = ctypes.addressof(ptr.contents)
        array = np.frombuffer(ArrayType.from_address(addr), np.int64)
        graphlab.cython.pointer_to_ndarray.numpy_own_array(array)
        width = arrlen / len(sf)
        if width > 1:
            return array.reshape(len(sf), width)
        else:
            return array
    else:
        unity_dll.pointer_from_sframe.restype = ctypes.POINTER(ctypes.c_double)
        ptr = unity_dll.pointer_from_sframe(temp_dir, True)
        if not ptr:
            raise RuntimeError("Unable to convert to numpy array")
        arrlen = unity_dll.pointer_length(ptr) / 8
        ArrayType = ctypes.c_double * arrlen
        addr = ctypes.addressof(ptr.contents)
        array = np.frombuffer(ArrayType.from_address(addr), np.double)
        graphlab.cython.pointer_to_ndarray.numpy_own_array(array)
        width = arrlen / len(sf)
        if width > 1:
            return array.reshape(len(sf), arrlen / len(sf))
        else:
            return array
Esempio n. 6
0
 def get_image(self, array):
     img = array.reshape((28, 28))
     img = Image.fromarray(img)
     return img